-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponse_reader_test.go
62 lines (50 loc) · 1.22 KB
/
response_reader_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
package rclient
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReadJSONResponse(t *testing.T) {
resp := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("\"body\"")),
}
var v string
if err := ReadJSONResponse(resp, &v); err != nil {
t.Fatal(err)
}
assert.Equal(t, "body", v)
}
func TestReadJSONResponseNilV(t *testing.T) {
resp := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("\"body\"")),
}
if err := ReadJSONResponse(resp, nil); err != nil {
t.Fatal(err)
}
}
func TestReadJSONResponseError_statusCode(t *testing.T) {
codes := []int{0, 199, 300, 399, 400, 499, 500, 599}
for _, c := range codes {
resp := &http.Response{
StatusCode: c,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}
if err := ReadJSONResponse(resp, nil); err == nil {
t.Fatalf("%d: Error was nil!", c)
}
}
}
func TestReadJSONResponseError_invalidJSON(t *testing.T) {
resp := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("some_invalid_json")),
}
var p person
if err := ReadJSONResponse(resp, &p); err == nil {
t.Fatalf("Error was nil!")
}
}