-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer_test.go
190 lines (156 loc) · 5.21 KB
/
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package kumi_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/cristiangraz/kumi"
)
func TestWriter_Status(t *testing.T) {
var ran bool
k := kumi.New(&Router{})
k.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
ran = true
if rw, ok := w.(kumi.ResponseWriter); !ok {
t.Fatalf("unexpected writer: %T", w)
} else if rw.Status() != http.StatusPreconditionFailed { // middleware should have access to status
t.Fatalf("unexpected status code: %d", rw.Status())
}
})
})
k.Get("/", func(w http.ResponseWriter, r *http.Request) {
rw, _ := w.(kumi.ResponseWriter)
if rw.Status() != http.StatusOK { // No status should return 200
t.Fatalf("Expected %d when status not sent, got %d", http.StatusOK, rw.Status())
}
w.WriteHeader(http.StatusPreconditionFailed)
if rw.Status() != http.StatusPreconditionFailed { // Ensure writer is using pointer and status is accessible
t.Fatalf("Expected %d when status not sent, got %d", http.StatusPreconditionFailed, rw.Status())
}
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
k.ServeHTTP(w, r)
if ran != true {
t.Fatalf("handler did not run")
} else if w.Code != http.StatusPreconditionFailed {
t.Fatalf("unexpected status code: %d", w.Code)
}
}
func TestWriter_Written(t *testing.T) {
var ran bool
k := kumi.New(&Router{})
k.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("marker"))
ran = true
if rw, ok := w.(kumi.ResponseWriter); !ok {
t.Fatalf("unexpected writer: %T", w)
} else if rw.Written() != 6 {
t.Fatalf("unexpected written value: %d", rw.Written())
}
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
k.ServeHTTP(w, r)
if ran != true {
t.Fatalf("handler did not run")
} else if w.Code != http.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
}
// BodylessResponseWriter should not write body or send a Content-Type header.
func TestWriter_NoContentUsesBodylessWriter(t *testing.T) {
k := kumi.New(&Router{})
var invoked bool
k.Get("/", func(w http.ResponseWriter, r *http.Request) {
invoked = true
w.Header().Set("Content-Type", "application/html")
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("writing content")) // send body to verify it's not written
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
k.ServeHTTP(w, r)
if !invoked {
t.Fatalf("handler did not run")
} else if w.Code != http.StatusNoContent {
t.Fatalf("unexpected status code: %d", w.Code)
} else if w.Body.Len() > 0 { // no body should be written
t.Fatalf("expected no response body: %s", w.Body.String())
} else if ct := w.Header().Get("Content-Type"); ct != "" { // content-type should be stripped
t.Fatalf("unexpected content-type: %s", ct)
}
}
func TestWriter_BodylessResponseWriter_Written(t *testing.T) {
k := kumi.New(&Router{})
var invoked bool
k.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("writing content"))
invoked = true
if rw, ok := w.(kumi.ResponseWriter); !ok {
t.Fatalf("expected kumi.ResponseWriter: %T", w)
} else if rw.Written() > 0 {
t.Fatalf("expected no bytes to be written with BodylessResponseWriter, wrote %d", rw.Written())
}
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
k.ServeHTTP(w, r)
if !invoked {
t.Fatalf("handler was not invoked")
} else if w.Body.Len() != 0 {
t.Fatalf("unexpected bytes written: %s", w.Body.String())
}
}
func TestWriter_BodylessResponseWriter_Status(t *testing.T) {
k := kumi.New(&Router{})
var invoked bool
k.Get("/", func(w http.ResponseWriter, r *http.Request) {
w = &kumi.BodylessResponseWriter{ResponseWriter: w}
w.WriteHeader(http.StatusConflict)
w.Write([]byte("writing content"))
invoked = true
if rw, ok := w.(kumi.ResponseWriter); !ok {
t.Fatalf("expected kumi.ResponseWriter: %T", w)
} else if rw.Status() > http.StatusConflict {
t.Fatalf("expected no bytes to be written with BodylessResponseWriter, wrote %d", rw.Written())
}
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
k.ServeHTTP(w, r)
if !invoked {
t.Fatalf("handler was not invoked")
} else if w.Body.Len() != 0 {
t.Fatalf("unexpected bytes written: %s", w.Body.String())
}
}
func TestWriter_SetsContentType(t *testing.T) {
var ran bool
k := kumi.New(&Router{})
k.Get("/", func(w http.ResponseWriter, r *http.Request) {
ran = true
w.Write([]byte("writing content"))
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
k.ServeHTTP(w, r)
if ran != true {
t.Fatalf("handler did not run")
} else if ct := w.Header().Get("Content-Type"); ct != "text/plain" {
t.Fatalf("unexpected content-type: %s", ct)
}
}
func TestBodyLessResponseWriter_Write(t *testing.T) {
w := httptest.NewRecorder()
bw := &kumi.BodylessResponseWriter{ResponseWriter: w}
if n, err := bw.Write([]byte("hi")); err != nil {
t.Fatalf("unexpected error: %v", err)
} else if n != 0 {
t.Fatalf("unexpected number of bytes written: %d", n)
} else if w.Body.Len() > 0 {
t.Fatalf("expected no bytes to be written: %s", w.Body.String())
}
}