This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
137 lines (107 loc) · 3.7 KB
/
middleware_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
package bourbon
import (
"github.com/bmizerany/assert"
"github.com/codegangsta/inject"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestMiddlewareContentTypeHandlerBlank(t *testing.T) {
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://example.com", nil)
code, encodeable := ContentTypeHandler(rw, r)
contentType := "application/json; charset=utf-8"
assert.Equal(t, 0, code)
assert.Equal(t, nil, encodeable)
assert.Equal(t, contentType, rw.Header().Get("Content-Type"))
}
func TestMiddlewareContentTypeHandlerWithJSON(t *testing.T) {
contentType := "application/json; charset=utf-8"
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("Content-Type", contentType)
code, encodeable := ContentTypeHandler(rw, r)
assert.Equal(t, 0, code)
assert.Equal(t, nil, encodeable)
assert.Equal(t, contentType, rw.Header().Get("Content-Type"))
}
func TestMiddlewareContentTypeHandlerWithVndJSON(t *testing.T) {
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("Content-Type", "application/vnd.example+json; param=2")
code, encodeable := ContentTypeHandler(rw, r)
contentType := "application/json; charset=utf-8"
assert.Equal(t, 0, code)
assert.Equal(t, nil, encodeable)
assert.Equal(t, contentType, rw.Header().Get("Content-Type"))
}
func TestMiddlewareContentTypeHandlerWithHTML(t *testing.T) {
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Set("Content-Type", "text/html")
code, encodeable := ContentTypeHandler(rw, r)
contentType := "application/json; charset=utf-8"
message := Message{
Code: 415,
Message: http.StatusText(415),
Errors: []string{`"text/html" is not a supported Content-Type`},
}
assert.Equal(t, 415, code)
assert.Equal(t, message, encodeable)
assert.Equal(t, contentType, rw.Header().Get("Content-Type"))
}
func TestMiddlewareDecodeHandlerWithJSON(t *testing.T) {
type example struct {
Name string `json:"name"`
}
reader := strings.NewReader(`{"name":"example"}`)
fn := func(e example) {}
r, _ := http.NewRequest("GET", "http://example.com", reader)
c := createTestContext(fn, r)
code, encodeable := DecodeHandler(c, r)
assert.Equal(t, 0, code)
assert.Equal(t, nil, encodeable)
}
func TestMiddlewareDecodeHandlerWithMalformedJSON(t *testing.T) {
type example struct {
Name string `json:"name"`
}
reader := strings.NewReader(`{"name":""boom}`) // incorrect quoting
fn := func(e example) {}
r, _ := http.NewRequest("GET", "http://example.com", reader)
c := createTestContext(fn, r)
message := Message{
Code: 400,
Message: http.StatusText(400),
Errors: []string{"invalid character 'b' after object key:value pair"},
}
code, encodeable := DecodeHandler(c, r)
assert.Equal(t, 400, code)
assert.Equal(t, message, encodeable)
}
func TestMiddlewareDecodeHandlerWithBlankBody(t *testing.T) {
type example struct {
Name string `json:"name"`
}
reader := strings.NewReader("") // empty string
fn := func(e example) {}
r, _ := http.NewRequest("GET", "http://example.com", reader)
c := createTestContext(fn, r)
code, encodeable := DecodeHandler(c, r)
assert.Equal(t, 0, code)
assert.Equal(t, nil, encodeable)
}
func TestMiddlewareDecodeHandlerWithContentLengthZero(t *testing.T) {
fn := func() {}
r, _ := http.NewRequest("GET", "http://example.com", nil)
r.Header.Add("Content-Length", "0")
c := createTestContext(fn, r)
code, encodeable := DecodeHandler(c, r)
assert.Equal(t, 0, code)
assert.Equal(t, nil, encodeable)
}
func createTestContext(fn Handler, r *http.Request) *context {
rt := &route{new(bourbon), "GET", "/", fn, nil}
return &context{inject.New(), fn, nil, rt, nil, r}
}