-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport_test.go
131 lines (99 loc) · 3.11 KB
/
transport_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
package oaichecker
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type mockTransport struct {
mock.Mock
}
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
args := t.Called(req)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*http.Response), args.Error(1)
}
func resBody(t *testing.T, res *http.Response) string {
body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
return string(body)
}
func newServer(handlerFunc func(http.ResponseWriter, *http.Request)) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(handlerFunc))
}
func Test_Transport_implements_RoundTripper(t *testing.T) {
assert.Implements(t, (*http.RoundTripper)(nil), &Transport{})
}
func Test_Transport_with_a_valid_request(t *testing.T) {
ts := newServer(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("[]"))
require.NoError(t, err)
})
defer ts.Close()
specs, err := NewSpecsFromFile("./dataset/petstore_minimal.json")
require.NoError(t, err)
client := http.Client{
Transport: NewTransport(specs),
}
res, err := client.Get(ts.URL + "/pets")
assert.NoError(t, err)
assert.JSONEq(t, `[]`, resBody(t, res))
}
func Test_Transport_with_a_transport_error(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore_minimal.json")
require.NoError(t, err)
mockInnerTransport := new(mockTransport)
mockInnerTransport.On("RoundTrip", mock.Anything).Return(nil, errors.New("some-error")).Once()
checkerTransport := NewTransport(specs)
checkerTransport.Transport = mockInnerTransport
client := http.Client{
Transport: checkerTransport,
}
res, err := client.Get("http://foobar/pets")
assert.Nil(t, res)
assert.EqualError(t, err, "Get http://foobar/pets: some-error")
mockInnerTransport.AssertExpectations(t)
}
func Test_Transport_with_an_analyzer_error(t *testing.T) {
ts := newServer(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("some-response"))
require.NoError(t, err)
})
defer ts.Close()
specs, err := NewSpecsFromFile("./dataset/petstore_minimal.json")
require.NoError(t, err)
client := http.Client{
Transport: NewTransport(specs),
}
res, err := client.Get(ts.URL + "/invalid-path")
assert.Nil(t, res)
assert.EqualError(t, err, fmt.Sprintf("Get %s/invalid-path: operation not defined inside the specs", ts.URL))
}
func Test_Transport_with_a_body(t *testing.T) {
ts := newServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
_, err := w.Write([]byte(""))
require.NoError(t, err)
})
defer ts.Close()
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)
client := http.Client{
Transport: NewTransport(specs),
}
res, err := client.Post(ts.URL+"/pet", "application/json", strings.NewReader(`{
"name": "foobar",
"photoUrls": ["some-url"]
}`))
assert.NoError(t, err)
assert.Equal(t, http.StatusCreated, res.StatusCode)
assert.Equal(t, "", resBody(t, res))
}