Skip to content

Commit 9a0056e

Browse files
committed
Added unit test cases for context
1 parent 0f02450 commit 9a0056e

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

context_test.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*!
2+
* rest-api-framework
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package rest
7+
8+
import (
9+
"errors"
10+
"net/http/httptest"
11+
"testing"
12+
)
13+
14+
var ctx Context
15+
16+
func TestContext_Set(t *testing.T) {
17+
ctx.init()
18+
ctx.Set("foo", "bar")
19+
20+
if ctx.data["foo"] != "bar" {
21+
t.Error("Set is not working")
22+
}
23+
}
24+
25+
func TestContext_Get1(t *testing.T) {
26+
bar, exists := ctx.Get("foo")
27+
28+
if !exists && bar != "bar" {
29+
t.Error("Get is not working")
30+
}
31+
}
32+
33+
func TestContext_Get2(t *testing.T) {
34+
_, exists := ctx.Get("bar")
35+
36+
if exists {
37+
t.Error("Get is not working")
38+
}
39+
}
40+
41+
func TestContext_Status(t *testing.T) {
42+
_ctx := ctx.Status(200)
43+
44+
if ctx.status != 200 {
45+
t.Error("Status is not working")
46+
}
47+
48+
if _ctx != &ctx {
49+
t.Error("Status is not returning ctx object")
50+
}
51+
}
52+
53+
func TestContext_Throw(t *testing.T) {
54+
err := errors.New("test error")
55+
ctx.Throw(err)
56+
57+
if ctx.err != err {
58+
t.Error("Throw is not working")
59+
}
60+
61+
ctx.err = nil
62+
}
63+
64+
func TestContext_GetError(t *testing.T) {
65+
err := errors.New("test error")
66+
ctx.Throw(err)
67+
68+
if ctx.GetError() != err {
69+
t.Error("GetError is not working")
70+
}
71+
72+
ctx.err = nil
73+
}
74+
75+
func TestContext_End(t *testing.T) {
76+
ctx.End()
77+
78+
if !ctx.end {
79+
t.Error("End is not working")
80+
}
81+
82+
ctx.end = false
83+
}
84+
85+
func TestContext_Write1(t *testing.T) {
86+
ctx.init()
87+
ctx.Response = httptest.NewRecorder()
88+
data := []byte("Hello World")
89+
ctx.Write(data)
90+
91+
if ctx.err != nil {
92+
t.Error("Write is not working")
93+
}
94+
95+
if !ctx.end {
96+
t.Error("Write is no executing successfully")
97+
}
98+
ctx.destroy()
99+
}
100+
101+
func TestContext_Write2(t *testing.T) {
102+
ctx.init()
103+
ctx.Response = httptest.NewRecorder()
104+
ctx.end = true
105+
data := []byte("Hello World")
106+
ctx.Write(data)
107+
108+
if ctx.err != nil {
109+
t.Error("Write is not working")
110+
}
111+
112+
if !ctx.end {
113+
t.Error("Write is no executing successfully")
114+
}
115+
ctx.destroy()
116+
}
117+
118+
func TestContext_JSON1(t *testing.T) {
119+
ctx.init()
120+
ctx.Response = httptest.NewRecorder()
121+
ctx.JSON([]string{"Hello", "World"})
122+
123+
if ctx.err != nil {
124+
t.Error("JSON is not working")
125+
}
126+
127+
if !ctx.end {
128+
t.Error("JSON is no executing successfully")
129+
}
130+
ctx.destroy()
131+
}
132+
133+
func TestContext_JSON2(t *testing.T) {
134+
ctx.init()
135+
ctx.Response = httptest.NewRecorder()
136+
ctx.JSON("Hello World")
137+
138+
if ctx.err.Error() != "INVALID_JSON_RESPONSE" {
139+
t.Error("JSON is not working")
140+
}
141+
142+
if !ctx.end {
143+
t.Error("JSON is no executing successfully")
144+
}
145+
ctx.destroy()
146+
}
147+
148+
func TestContext_Text(t *testing.T) {
149+
ctx.init()
150+
ctx.Response = httptest.NewRecorder()
151+
ctx.Text("Hello World")
152+
153+
if ctx.err != nil {
154+
t.Error("Text is not working")
155+
}
156+
157+
if !ctx.end {
158+
t.Error("Text is no executing successfully")
159+
}
160+
ctx.destroy()
161+
}

0 commit comments

Comments
 (0)