-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_test.go
105 lines (98 loc) · 2.29 KB
/
adapter_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
package jh
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/pkg/errors"
)
func TestAdapter(t *testing.T) {
ts := httptest.NewServer(newServer())
c := ts.Client()
tests := []struct {
label string
route string
code int
chk func(io.Reader) error
}{
{
label: "nominal",
route: "/ok/",
code: http.StatusOK,
chk: func(body io.Reader) error {
j := map[string]int{}
if err := json.NewDecoder(body).Decode(&j); err != nil {
errors.Wrap(err, "json decode")
}
if got, want := j, resp(); !reflect.DeepEqual(got, want) {
return fmt.Errorf("%v != %v", got, want)
}
return nil
},
},
{
label: "created",
route: "/created/",
code: http.StatusCreated,
chk: func(body io.Reader) error {
j := map[string]int{}
if err := json.NewDecoder(body).Decode(&j); err != nil {
errors.Wrap(err, "json decode")
}
if got, want := j, resp(); !reflect.DeepEqual(got, want) {
return fmt.Errorf("%v != %v", got, want)
}
return nil
},
},
{
label: "conflict",
route: "/conflict/",
code: http.StatusConflict,
chk: func(body io.Reader) error {
j := map[string]string{}
if err := json.NewDecoder(body).Decode(&j); err != nil {
errors.Wrap(err, "json decode")
}
if got, want := j["error"], "context: reason"; got != want {
return fmt.Errorf("got %q, want %q", got, want)
}
return nil
},
},
{
label: "default error",
route: "/ise/",
code: http.StatusInternalServerError,
chk: func(body io.Reader) error {
j := map[string]string{}
if err := json.NewDecoder(body).Decode(&j); err != nil {
errors.Wrap(err, "json decode")
}
if got, want := j["error"], "should get default code"; got != want {
return fmt.Errorf("got %q, want %q", got, want)
}
return nil
},
},
}
for _, test := range tests {
t.Run(test.label, func(t *testing.T) {
resp, err := c.Get(ts.URL + test.route)
if err != nil {
t.Fatalf("get: %v", err)
}
if got, want := resp.StatusCode, test.code; got != want {
t.Fatalf("unexpected status code; got %v, want %v", got, want)
}
if test.chk != nil {
if err := test.chk(resp.Body); err != nil {
t.Fatalf("resp check: %v", err)
}
}
})
}
}