-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_response_test.go
121 lines (114 loc) · 3.11 KB
/
http_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gdk
import (
"bytes"
"compress/gzip"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"testing"
)
// named-return, to avoid side effects of defer opt
func gzipFast(a []byte) (b bytes.Buffer) {
gz := gzip.NewWriter(&b)
defer gz.Close()
if _, err := gz.Write(a); err != nil {
panic(err)
}
gz.Flush()
return b
}
// badgzip
func gzipBad(a []byte) []byte {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
defer gz.Close()
if _, err := gz.Write(a); err != nil {
panic(err)
}
gz.Flush()
return b.Bytes()
}
func httpResponse(statusCode int, headers map[string]string, body []byte) *http.Response {
header := http.Header{}
for name, value := range headers {
header.Set(name, value)
}
return &http.Response{
Status: strconv.Itoa(statusCode) + " " + http.StatusText(statusCode),
StatusCode: statusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
TransferEncoding: []string{},
Close: false,
Uncompressed: false,
Trailer: nil,
TLS: nil,
}
}
func TestResponseRecorder(t *testing.T) {
type args struct {
resp *http.Response
}
jsonb, _ := json.Marshal(map[string]interface{}{"name": "mike"})
gzipb := gzipFast(jsonb)
gzipbad := gzipBad(jsonb)
tests := []struct {
name string
args args
wantBody []byte
wantIsGzip bool
wantErr bool
}{
{name: "nil response", args: args{resp: nil}, wantBody: []byte{}, wantIsGzip: false, wantErr: true},
{name: "json response 200", args: args{resp: httpResponse(
http.StatusOK,
map[string]string{"Content-Type": "application/json"},
jsonb,
)}, wantBody: []byte("{\"name\":\"mike\"}"), wantIsGzip: false, wantErr: false},
{name: "gzip response 200", args: args{resp: httpResponse(
http.StatusOK,
map[string]string{
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
gzipb.Bytes(),
)}, wantBody: []byte("{\"name\":\"mike\"}"), wantIsGzip: true, wantErr: false},
{name: "gzip NewReader error", args: args{resp: httpResponse(
http.StatusOK,
map[string]string{
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
//gzipb.Bytes(),
jsonb,
)}, wantBody: []byte{}, wantIsGzip: true, wantErr: true},
{name: "ioutil ReadAll error", args: args{resp: httpResponse(
http.StatusOK,
map[string]string{
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
gzipbad,
)}, wantBody: []byte("{\"name\":\"mike\"}"), wantIsGzip: true, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bodyBytes, isGzip, err := ReadResponse(tt.args.resp)
if (err != nil) != tt.wantErr {
t.Errorf("ReadResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(bodyBytes) != string(tt.wantBody) {
t.Errorf("ReadResponse() = %s, want %s", bodyBytes, tt.wantBody)
}
if isGzip != tt.wantIsGzip {
t.Errorf("ReadResponse() isGizp=%v, want %v", isGzip, tt.wantIsGzip)
}
})
}
}