-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
46 lines (40 loc) · 941 Bytes
/
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
package hyper
import (
"context"
"net/http"
"strings"
"testing"
)
func TestSuccess(t *testing.T) {
var ctx = context.Background()
var rw = &fakeResponseWriter{}
var s = struct {
Field1 string
Field2 int
}{
Field1: "test",
Field2: 12,
}
Success(ctx, rw, s)
var expected = `{"data":{"Field1":"test","Field2":12}}`
if !strings.Contains(string(rw.data), expected) {
t.Errorf("ResponseWriter data should contain %s, instead of %s", expected, string(rw.data))
}
if rw.statusCode != 200 {
t.Errorf("ResponseWriter status code should be 200, instead of %d", rw.statusCode)
}
}
type fakeResponseWriter struct {
data []byte
statusCode int
}
func (f *fakeResponseWriter) Header() http.Header {
return map[string][]string{}
}
func (f *fakeResponseWriter) Write(bytes []byte) (int, error) {
f.data = bytes
return 0, nil
}
func (f *fakeResponseWriter) WriteHeader(statusCode int) {
f.statusCode = statusCode
}