-
Notifications
You must be signed in to change notification settings - Fork 72
/
response_test.go
107 lines (88 loc) · 2.66 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
package gateway
import (
"bytes"
"testing"
"github.com/tj/assert"
)
func Test_JSON_isTextMime(t *testing.T) {
assert.Equal(t, isTextMime("application/json"), true)
assert.Equal(t, isTextMime("application/json; charset=utf-8"), true)
assert.Equal(t, isTextMime("Application/JSON"), true)
}
func Test_XML_isTextMime(t *testing.T) {
assert.Equal(t, isTextMime("application/xml"), true)
assert.Equal(t, isTextMime("application/xml; charset=utf-8"), true)
assert.Equal(t, isTextMime("ApPlicaTion/xMl"), true)
}
func TestResponseWriter_Header(t *testing.T) {
w := NewResponse()
w.Header().Set("Foo", "bar")
w.Header().Set("Bar", "baz")
var buf bytes.Buffer
w.header.Write(&buf)
assert.Equal(t, "Bar: baz\r\nFoo: bar\r\n", buf.String())
}
func TestResponseWriter_multiHeader(t *testing.T) {
w := NewResponse()
w.Header().Set("Foo", "bar")
w.Header().Set("Bar", "baz")
w.Header().Add("X-APEX", "apex1")
w.Header().Add("X-APEX", "apex2")
var buf bytes.Buffer
w.header.Write(&buf)
assert.Equal(t, "Bar: baz\r\nFoo: bar\r\nX-Apex: apex1\r\nX-Apex: apex2\r\n", buf.String())
}
func TestResponseWriter_Write_text(t *testing.T) {
types := []string{
"text/x-custom",
"text/plain",
"text/plain; charset=utf-8",
"application/json",
"application/json; charset=utf-8",
"application/xml",
"image/svg+xml",
}
for _, kind := range types {
t.Run(kind, func(t *testing.T) {
w := NewResponse()
w.Header().Set("Content-Type", kind)
w.Write([]byte("hello world\n"))
e := w.End()
assert.Equal(t, 200, e.StatusCode)
assert.Equal(t, "hello world\n", e.Body)
assert.Equal(t, kind, e.Headers["Content-Type"])
assert.False(t, e.IsBase64Encoded)
assert.True(t, <-w.CloseNotify())
})
}
}
func TestResponseWriter_Write_binary(t *testing.T) {
w := NewResponse()
w.Header().Set("Content-Type", "image/png")
w.Write([]byte("data"))
e := w.End()
assert.Equal(t, 200, e.StatusCode)
assert.Equal(t, "ZGF0YQ==", e.Body)
assert.Equal(t, "image/png", e.Headers["Content-Type"])
assert.True(t, e.IsBase64Encoded)
}
func TestResponseWriter_Write_gzip(t *testing.T) {
w := NewResponse()
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Encoding", "gzip")
w.Write([]byte("data"))
e := w.End()
assert.Equal(t, 200, e.StatusCode)
assert.Equal(t, "ZGF0YQ==", e.Body)
assert.Equal(t, "text/plain", e.Headers["Content-Type"])
assert.True(t, e.IsBase64Encoded)
}
func TestResponseWriter_WriteHeader(t *testing.T) {
w := NewResponse()
w.WriteHeader(404)
w.Write([]byte("Not Found\n"))
e := w.End()
assert.Equal(t, 404, e.StatusCode)
assert.Equal(t, "Not Found\n", e.Body)
assert.Equal(t, "text/plain; charset=utf8", e.Headers["Content-Type"])
}