-
Notifications
You must be signed in to change notification settings - Fork 8
/
context_test.go
124 lines (92 loc) · 3.28 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package yarf
import (
"net/http"
"net/http/httptest"
"testing"
)
func createRequestResponse() (request *http.Request, response *httptest.ResponseRecorder) {
// Create a dummy request.
request, _ = http.NewRequest(
"GET",
"http://127.0.0.1:8080/",
nil,
)
request.RemoteAddr = "200.201.202.203"
request.Header.Set("User-Agent", "yarf/1.0")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response = httptest.NewRecorder()
return
}
func TestNewContext(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
if c.Request != req {
t.Error("Request object provided to NewContext() wasn't set correctly on Context object")
}
if c.Response != res {
t.Error("Response object provided to NewContext() wasn't set correctly on Context object")
}
}
func TestStatus(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.Status(201)
if c.Response.(*httptest.ResponseRecorder).Code != 201 {
t.Errorf("Status %d set to Status() method, %d found", 201, c.Response.(*httptest.ResponseRecorder).Code)
}
}
func TestParam(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.Params.Set("name", "Joe")
if c.Param("name") != "Joe" {
t.Errorf("Param 'name' set to '%s', '%s' retrieved.", "Joe", c.Param("name"))
}
}
func TestGetClientIP(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
if c.GetClientIP() != req.RemoteAddr {
t.Errorf("IP %s set to request, %s retrieved by GetClientIP()", req.RemoteAddr, c.GetClientIP())
}
}
func TestRender(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.Render("TEST")
if c.Response.(*httptest.ResponseRecorder).Body.String() != "TEST" {
t.Errorf("'%s' sent to Render() method, '%s' found on Response object", "TEST", c.Response.(*httptest.ResponseRecorder).Body.String())
}
}
func TestRenderJSON(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.RenderJSON("TEST")
if c.Response.(*httptest.ResponseRecorder).Body.String() != "\"TEST\"" {
t.Errorf("'%s' sent to RenderJSON() method, '%s' found on Response object", "TEST", c.Response.(*httptest.ResponseRecorder).Body.String())
}
}
func TestRenderJSONIndent(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.RenderJSONIndent("TEST")
if c.Response.(*httptest.ResponseRecorder).Body.String() != "\"TEST\"" {
t.Errorf("'%s' sent to RenderJSONIndent() method, '%s' found on Response object", "TEST", c.Response.(*httptest.ResponseRecorder).Body.String())
}
}
func TestRenderXML(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.RenderXML("TEST")
if c.Response.(*httptest.ResponseRecorder).Body.String() != "<string>TEST</string>" {
t.Errorf("'%s' sent to RenderXML() method, '%s' found on Response object", "TEST", c.Response.(*httptest.ResponseRecorder).Body.String())
}
}
func TestRenderXMLIndent(t *testing.T) {
req, res := createRequestResponse()
c := NewContext(req, res)
c.RenderXMLIndent("TEST")
if c.Response.(*httptest.ResponseRecorder).Body.String() != "<string>TEST</string>" {
t.Errorf("'%s' sent to RenderXMLIndent() method, '%s' found on Response object", "TEST", c.Response.(*httptest.ResponseRecorder).Body.String())
}
}