forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
147 lines (121 loc) · 3.37 KB
/
response_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package goproxy
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSetResponseCacheControlHeader(t *testing.T) {
rec := httptest.NewRecorder()
assert.Empty(t, rec.Header().Get("Cache-Control"))
setResponseCacheControlHeader(rec, 60)
assert.Equal(t, "public, max-age=60", rec.Header().Get("Cache-Control"))
setResponseCacheControlHeader(rec, -1)
assert.Equal(
t,
"must-revalidate, no-cache, no-store",
rec.Header().Get("Cache-Control"),
)
}
func TestResponseString(t *testing.T) {
rec := httptest.NewRecorder()
responseString(rec, http.StatusOK, "Foobar")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "Foobar", rec.Body.String())
}
func TestResponseJSON(t *testing.T) {
rec := httptest.NewRecorder()
responseJSON(rec, http.StatusOK, []byte(`{"Foo":"Bar"}`))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(
t,
"application/json; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, `{"Foo":"Bar"}`, rec.Body.String())
}
func TestResponseNotFound(t *testing.T) {
rec := httptest.NewRecorder()
responseNotFound(rec)
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "not found", rec.Body.String())
rec = httptest.NewRecorder()
responseNotFound(rec, "foobar")
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "not found: foobar", rec.Body.String())
rec = httptest.NewRecorder()
responseNotFound(rec, "bad request: foobar")
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "not found: foobar", rec.Body.String())
rec = httptest.NewRecorder()
responseNotFound(rec, "not found: foobar")
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "not found: foobar", rec.Body.String())
rec = httptest.NewRecorder()
responseNotFound(rec, "gone: foobar")
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "not found: foobar", rec.Body.String())
}
func TestResponseMethodNotAllowed(t *testing.T) {
rec := httptest.NewRecorder()
responseMethodNotAllowed(rec)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "method not allowed", rec.Body.String())
}
func TestResponseInternalServerError(t *testing.T) {
rec := httptest.NewRecorder()
responseInternalServerError(rec)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "internal server error", rec.Body.String())
}
func TestResponseBadGateway(t *testing.T) {
rec := httptest.NewRecorder()
responseBadGateway(rec)
assert.Equal(t, http.StatusBadGateway, rec.Code)
assert.Equal(
t,
"text/plain; charset=utf-8",
rec.HeaderMap.Get("Content-Type"),
)
assert.Equal(t, "bad gateway", rec.Body.String())
}