-
Notifications
You must be signed in to change notification settings - Fork 70
/
request_test.go
211 lines (203 loc) · 6.15 KB
/
request_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package session
import (
"crypto/tls"
"crypto/x509"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegrid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testStruct struct {
A string `json:"a"`
B int `json:"b"`
}
type testInvalid struct {
Invalid func()
}
func TestSession_Exec(t *testing.T) {
tests := map[string]struct {
request *http.Request
out testStruct
in []interface{}
responseBody string
responseStatus int
expectedContentType string
expectedAccept string
expectedUserAgent string
expectedMethod string
expectedPath string
expected interface{}
withError error
}{
"GET request, use default values for request": {
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "/test/path", nil)
require.NoError(t, err)
return req
}(),
out: testStruct{},
responseBody: `{"a":"text","b":1}`,
responseStatus: http.StatusOK,
expectedMethod: http.MethodGet,
expectedPath: "/test/path",
expected: testStruct{
A: "text",
B: 1,
},
},
"GET request, escape query": {
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "/test/path?param1=some param", nil)
require.NoError(t, err)
return req
}(),
out: testStruct{},
responseBody: `{"a":"text","b":1}`,
responseStatus: http.StatusOK,
expectedMethod: http.MethodGet,
expectedPath: "/test/path?param1=some+param",
expected: testStruct{
A: "text",
B: 1,
},
},
"GET request, custom content type, accept and user agent": {
request: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "/test/path", nil)
require.NoError(t, err)
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("User-Agent", "other user agent")
req.Header.Set("Accept", "text/html")
return req
}(),
out: testStruct{},
responseBody: `{"a":"text","b":1}`,
responseStatus: http.StatusOK,
expectedMethod: http.MethodGet,
expectedPath: "/test/path",
expectedContentType: "text/plain",
expectedAccept: "text/html",
expectedUserAgent: "other user agent",
expected: testStruct{
A: "text",
B: 1,
},
},
"POST request, custom content type, accept and user agent": {
request: func() *http.Request {
req, err := http.NewRequest(http.MethodPost, "/test/path", nil)
require.NoError(t, err)
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("User-Agent", "other user agent")
req.Header.Set("Accept", "text/html")
return req
}(),
in: []interface{}{&testStruct{
A: "text",
B: 1,
}},
out: testStruct{},
responseBody: `{"a":"text","b":1}`,
responseStatus: http.StatusCreated,
expectedMethod: http.MethodPost,
expectedPath: "/test/path",
expectedContentType: "text/plain",
expectedUserAgent: "other user agent",
expectedAccept: "text/html",
expected: testStruct{
A: "text",
B: 1,
},
},
"POST request, invalid body": {
request: func() *http.Request {
req, err := http.NewRequest(http.MethodPost, "/test/path", nil)
require.NoError(t, err)
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("User-Agent", "other user agent")
return req
}(),
in: []interface{}{&testInvalid{func() {}}},
out: testStruct{},
withError: ErrMarshaling,
},
"POST request, unmarshaling error": {
request: func() *http.Request {
req, err := http.NewRequest(http.MethodPost, "/test/path", nil)
require.NoError(t, err)
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("User-Agent", "other user agent")
return req
}(),
in: []interface{}{&testStruct{
A: "text",
B: 1,
}},
out: testStruct{},
responseBody: `{"a":1,"b":1}`,
responseStatus: http.StatusCreated,
expectedMethod: http.MethodPost,
expectedPath: "/test/path",
expectedContentType: "text/plain",
expectedUserAgent: "other user agent",
withError: ErrUnmarshaling,
},
"invalid number of input parameters": {
in: []interface{}{testStruct{}, testStruct{}},
withError: ErrInvalidArgument,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
assert.Equal(t, test.expectedMethod, r.Method)
if test.expectedContentType == "" {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
} else {
assert.Equal(t, test.expectedContentType, r.Header.Get("Content-Type"))
}
if test.expectedUserAgent == "" {
assert.Equal(t, "test user agent", r.Header.Get("User-Agent"))
} else {
assert.Equal(t, test.expectedUserAgent, r.Header.Get("User-Agent"))
}
if test.expectedAccept == "" {
assert.Equal(t, "application/json", r.Header.Get("Accept"))
} else {
assert.Equal(t, test.expectedAccept, r.Header.Get("Accept"))
}
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
certPool := x509.NewCertPool()
certPool.AddCert(mockServer.Certificate())
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
},
}
serverURL, err := url.Parse(mockServer.URL)
require.NoError(t, err)
s, err := New(WithSigner(&edgegrid.Config{
Host: serverURL.Host,
RequestLimit: 10,
}), WithClient(httpClient), WithUserAgent("test user agent"), WithHTTPTracing(true))
require.NoError(t, err)
_, err = s.Exec(test.request, &test.out, test.in...)
if test.withError != nil {
assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
return
}
require.NoError(t, err)
assert.Equal(t, test.expected, test.out)
})
}
}