Skip to content

Commit 5ff38b4

Browse files
test: add buildHeaders function for http sync tests (#1804)
## This PR The previous way of building HTTP headers with raw map is not reliable in some edge cases. An example is case-sensitive issue for `ETag` header which requires `Etag` (lower case `t`) to be the map key when building the response with a raw map, in order to receive it via `resp.Header.Get("ETag")` on the other end. Having a `buildHeaders` function to utilize `Set` built-in function of `http.Header` would solve this concern. ### How to test ```bash go test ./core/pkg/sync/http/... ``` --------- Signed-off-by: Zhiwei Liang <[email protected]>
1 parent fcd19b3 commit 5ff38b4

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

core/pkg/sync/http/http_sync_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ import (
1616
"go.uber.org/mock/gomock"
1717
)
1818

19+
func buildHeaders(m map[string][]string) http.Header {
20+
h := http.Header{}
21+
for k, v := range m {
22+
for _, val := range v {
23+
h.Add(k, val)
24+
}
25+
}
26+
return h
27+
}
28+
1929
func TestSimpleSync(t *testing.T) {
2030
ctrl := gomock.NewController(t)
2131
mockCron := synctesting.NewMockCron(ctrl)
@@ -27,7 +37,7 @@ func TestSimpleSync(t *testing.T) {
2737
mockClient := syncmock.NewMockClient(ctrl)
2838
responseBody := "test response"
2939
resp := &http.Response{
30-
Header: map[string][]string{"Content-Type": {"application/json"}},
40+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
3141
Body: io.NopCloser(strings.NewReader(responseBody)),
3242
StatusCode: http.StatusOK,
3343
}
@@ -70,7 +80,7 @@ func TestExtensionWithQSSync(t *testing.T) {
7080
mockClient := syncmock.NewMockClient(ctrl)
7181
responseBody := "test response"
7282
resp := &http.Response{
73-
Header: map[string][]string{"Content-Type": {"application/json"}},
83+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
7484
Body: io.NopCloser(strings.NewReader(responseBody)),
7585
StatusCode: http.StatusOK,
7686
}
@@ -117,7 +127,7 @@ func TestHTTPSync_Fetch(t *testing.T) {
117127
"success": {
118128
setup: func(_ *testing.T, client *syncmock.MockClient) {
119129
client.EXPECT().Do(gomock.Any()).Return(&http.Response{
120-
Header: map[string][]string{"Content-Type": {"application/json"}},
130+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
121131
Body: io.NopCloser(strings.NewReader("test response")),
122132
StatusCode: http.StatusOK,
123133
}, nil)
@@ -144,7 +154,7 @@ func TestHTTPSync_Fetch(t *testing.T) {
144154
"update last body sha": {
145155
setup: func(_ *testing.T, client *syncmock.MockClient) {
146156
client.EXPECT().Do(gomock.Any()).Return(&http.Response{
147-
Header: map[string][]string{"Content-Type": {"application/json"}},
157+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
148158
Body: io.NopCloser(strings.NewReader("test response")),
149159
StatusCode: http.StatusOK,
150160
}, nil)
@@ -173,7 +183,7 @@ func TestHTTPSync_Fetch(t *testing.T) {
173183
t.Fatalf("expected Authorization header to be 'Bearer %s', got %s", expectedToken, actualAuthHeader)
174184
}
175185
return &http.Response{
176-
Header: map[string][]string{"Content-Type": {"application/json"}},
186+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
177187
Body: io.NopCloser(strings.NewReader("test response")),
178188
StatusCode: http.StatusOK,
179189
}, nil
@@ -204,7 +214,7 @@ func TestHTTPSync_Fetch(t *testing.T) {
204214
t.Fatalf("expected Authorization header to be '%s', got %s", expectedHeader, actualAuthHeader)
205215
}
206216
return &http.Response{
207-
Header: map[string][]string{"Content-Type": {"application/json"}},
217+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
208218
Body: io.NopCloser(strings.NewReader("test response")),
209219
StatusCode: http.StatusOK,
210220
}, nil
@@ -229,7 +239,7 @@ func TestHTTPSync_Fetch(t *testing.T) {
229239
"unauthorized request": {
230240
setup: func(_ *testing.T, client *syncmock.MockClient) {
231241
client.EXPECT().Do(gomock.Any()).Return(&http.Response{
232-
Header: map[string][]string{"Content-Type": {"application/json"}},
242+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
233243
Body: io.NopCloser(strings.NewReader("test response")),
234244
StatusCode: http.StatusUnauthorized,
235245
}, nil)
@@ -250,7 +260,7 @@ func TestHTTPSync_Fetch(t *testing.T) {
250260
t.Fatalf("expected If-None-Match header to be '%s', got %s", expectedIfNoneMatch, actualIfNoneMatch)
251261
}
252262
return &http.Response{
253-
Header: map[string][]string{"ETag": {expectedIfNoneMatch}},
263+
Header: buildHeaders(map[string][]string{"ETag": {expectedIfNoneMatch}}),
254264
Body: io.NopCloser(strings.NewReader("")),
255265
StatusCode: http.StatusNotModified,
256266
}, nil
@@ -290,10 +300,10 @@ func TestHTTPSync_Fetch(t *testing.T) {
290300
newETag := `"c2e01ce63d90109c4c7f4f6dcea97ed1bb2b51e3647f36caf5acbe27413a24bb"`
291301

292302
return &http.Response{
293-
Header: map[string][]string{
303+
Header: buildHeaders(map[string][]string{
294304
"Content-Type": {"application/json"},
295-
"Etag": {newETag},
296-
},
305+
"ETag": {newETag},
306+
}),
297307
Body: io.NopCloser(strings.NewReader(newContent)),
298308
StatusCode: http.StatusOK,
299309
}, nil
@@ -365,6 +375,7 @@ func TestSync_Init(t *testing.T) {
365375
}
366376
})
367377
}
378+
368379
}
369380

370381
func TestHTTPSync_Resync(t *testing.T) {
@@ -384,7 +395,7 @@ func TestHTTPSync_Resync(t *testing.T) {
384395
"success": {
385396
setup: func(_ *testing.T, client *syncmock.MockClient) {
386397
client.EXPECT().Do(gomock.Any()).Return(&http.Response{
387-
Header: map[string][]string{"Content-Type": {"application/json"}},
398+
Header: buildHeaders(map[string][]string{"Content-Type": {"application/json"}}),
388399
Body: io.NopCloser(strings.NewReader(emptyFlagData)),
389400
StatusCode: http.StatusOK,
390401
}, nil)

0 commit comments

Comments
 (0)