-
Notifications
You must be signed in to change notification settings - Fork 34
/
response_writer_test.go
82 lines (62 loc) · 2.03 KB
/
response_writer_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
package traffic
import (
assert "github.com/pilu/miniassert"
"net/http"
"net/http/httptest"
"testing"
)
func buildTestResponseWriter(globalEnv *map[string]interface{}) (*responseWriter, *httptest.ResponseRecorder) {
recorder := httptest.NewRecorder()
rw := newResponseWriter(
recorder,
globalEnv,
)
return rw, recorder
}
func newTestResponseWriter(globalEnv *map[string]interface{}) *responseWriter {
rw, _ := buildTestResponseWriter(globalEnv)
return rw
}
func TestResponseWriter(t *testing.T) {
routerEnv := make(map[string]interface{})
rw := newTestResponseWriter(&routerEnv)
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.Equal(t, 0, len(rw.env))
assert.Equal(t, &routerEnv, rw.routerEnv)
assert.Equal(t, 0, len(rw.beforeWriteHandlers))
}
func TestResponseWriter_SetVar(t *testing.T) {
globalEnv := make(map[string]interface{})
rw := newTestResponseWriter(&globalEnv)
rw.SetVar("foo", "bar")
assert.Equal(t, "bar", rw.env["foo"])
}
func TestResponseWriter_GetVar(t *testing.T) {
resetGlobalEnv()
routerEnv := map[string]interface{}{}
rw := newTestResponseWriter(&routerEnv)
env["global-foo"] = "global-bar"
assert.Equal(t, "global-bar", rw.GetVar("global-foo"))
routerEnv["global-foo"] = "router-bar"
assert.Equal(t, "router-bar", rw.GetVar("global-foo"))
rw.env["global-foo"] = "local-bar"
assert.Equal(t, "local-bar", rw.GetVar("global-foo"))
resetGlobalEnv()
}
func TestResponseWriter_Write(t *testing.T) {
routerEnv := make(map[string]interface{})
rw, recorder := buildTestResponseWriter(&routerEnv)
assert.False(t, rw.Written())
rw.Write([]byte("foo"))
assert.True(t, rw.Written())
assert.Equal(t, []byte("foo"), recorder.Body.Bytes())
}
func TestResponseWriter_WriteHeader(t *testing.T) {
routerEnv := make(map[string]interface{})
rw, recorder := buildTestResponseWriter(&routerEnv)
assert.False(t, rw.Written())
rw.WriteHeader(http.StatusUnauthorized)
assert.True(t, rw.Written())
assert.Equal(t, http.StatusUnauthorized, rw.StatusCode())
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
}