Skip to content

Commit 1c31982

Browse files
committed
Add header-related unit tests
1 parent 213db84 commit 1c31982

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

headers/headers_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,107 @@ func TestSetCacheControlHeader(t *testing.T) {
7878

7979
assert.Equal(t, cacheControlValue, req.Header.Get("Cache-Control"), "Cache-Control header should be correctly set")
8080
}
81+
82+
func TestSetConditionalHeaders(t *testing.T) {
83+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
84+
ifModifiedSince := "Wed, 21 Oct 2015 07:28:00 GMT"
85+
ifNoneMatch := `"etag-value"`
86+
87+
SetConditionalHeaders(req, ifModifiedSince, ifNoneMatch)
88+
89+
assert.Equal(t, ifModifiedSince, req.Header.Get("If-Modified-Since"), "If-Modified-Since header should be correctly set")
90+
assert.Equal(t, ifNoneMatch, req.Header.Get("If-None-Match"), "If-None-Match header should be correctly set")
91+
}
92+
93+
func TestSetAcceptEncodingHeader(t *testing.T) {
94+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
95+
acceptEncodingValue := "gzip, deflate"
96+
97+
SetAcceptEncodingHeader(req, acceptEncodingValue)
98+
99+
assert.Equal(t, acceptEncodingValue, req.Header.Get("Accept-Encoding"), "Accept-Encoding header should be correctly set")
100+
}
101+
102+
func TestSetRefererHeader(t *testing.T) {
103+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
104+
refererValue := "http://previous-page.com"
105+
106+
SetRefererHeader(req, refererValue)
107+
108+
assert.Equal(t, refererValue, req.Header.Get("Referer"), "Referer header should be correctly set")
109+
}
110+
111+
func TestSetXForwardedForHeader(t *testing.T) {
112+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
113+
xForwardedForValue := "123.45.67.89"
114+
115+
SetXForwardedForHeader(req, xForwardedForValue)
116+
117+
assert.Equal(t, xForwardedForValue, req.Header.Get("X-Forwarded-For"), "X-Forwarded-For header should be correctly set")
118+
}
119+
120+
func TestSetCustomHeader(t *testing.T) {
121+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
122+
headerName := "X-Custom-Header"
123+
headerValue := "CustomValue"
124+
125+
SetCustomHeader(req, headerName, headerValue)
126+
127+
assert.Equal(t, headerValue, req.Header.Get(headerName), "Custom header should be correctly set")
128+
}
129+
130+
// TestSetRequestHeaders verifies that standard headers, including a custom Authorization header,
131+
// are set correctly on the HTTP request based on headers provided by a mock APIHandler.
132+
// TODO need to implement MockAPIHandler
133+
// func TestSetRequestHeaders(t *testing.T) {
134+
// req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
135+
// mockAPIHandler := new(MockAPIHandler) // Assume you've implemented MockAPIHandler
136+
137+
// // Mock APIHandler to return a set of standard headers
138+
// standardHeaders := map[string]string{
139+
// "Content-Type": "application/json",
140+
// "Custom-Header": "custom-value",
141+
// }
142+
// mockAPIHandler.On("GetAPIRequestHeaders", "test-endpoint").Return(standardHeaders)
143+
144+
// authTokenHandler := &authenticationhandler.AuthTokenHandler{Token: "test-token"}
145+
// headerHandler := NewHeaderHandler(req, nil, nil, authTokenHandler)
146+
// headerHandler.SetRequestHeaders("test-endpoint")
147+
148+
// assert.Equal(t, "Bearer test-token", req.Header.Get("Authorization"), "Authorization header should be correctly set with Bearer token")
149+
// assert.Equal(t, "application/json", req.Header.Get("Content-Type"), "Content-Type header should be set to application/json")
150+
// assert.Equal(t, "custom-value", req.Header.Get("Custom-Header"), "Custom-Header should be set to custom-value")
151+
152+
// mockAPIHandler.AssertExpectations(t)
153+
// }
154+
155+
// TestHeadersToString verifies that the HeadersToString function correctly formats
156+
// HTTP headers into a string, with each header on a new line.
157+
func TestHeadersToString(t *testing.T) {
158+
headers := http.Header{
159+
"Content-Type": []string{"application/json"},
160+
"Accept": []string{"application/xml"},
161+
}
162+
163+
expected := "Content-Type: application/json\nAccept: application/xml"
164+
result := HeadersToString(headers)
165+
166+
assert.Equal(t, expected, result, "Headers should be correctly formatted into a string")
167+
}
168+
169+
// TestCheckDeprecationHeader verifies that the CheckDeprecationHeader function
170+
// can detect the presence of a Deprecation header in the HTTP response.
171+
// TODO need to implement MockLogger
172+
// func TestCheckDeprecationHeader(t *testing.T) {
173+
// resp := &http.Response{
174+
// Header: make(http.Header),
175+
// }
176+
// deprecationDate := "Fri, 01 Jan 2100 00:00:00 GMT"
177+
// resp.Header.Set("Deprecation", deprecationDate)
178+
179+
// // Normally, you would check for a log entry here, but we're skipping logging.
180+
// // This test will simply ensure the function can run without error.
181+
// CheckDeprecationHeader(resp, nil) // Passing nil as logger since we're not testing logging
182+
183+
// assert.Equal(t, deprecationDate, resp.Header.Get("Deprecation"), "Deprecation header should be detected")
184+
// }

0 commit comments

Comments
 (0)