Skip to content

Commit e80143b

Browse files
authored
Merge pull request #122 from deploymenttheory/dev
Added unit tests for headers package
2 parents 04a4881 + 1c31982 commit e80143b

File tree

4 files changed

+220
-182
lines changed

4 files changed

+220
-182
lines changed

headers/headers.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ import (
1616

1717
// HeaderHandler is responsible for managing and setting headers on HTTP requests.
1818
type HeaderHandler struct {
19-
req *http.Request // The http.Request for which headers are being managed
20-
log logger.Logger // The logger to use for logging headers
21-
apiHandler apihandler.APIHandler // The APIHandler to use for retrieving standard headers
22-
token string // The token to use for setting the Authorization header
23-
authTokenHandler *authenticationhandler.AuthTokenHandler
19+
req *http.Request // The http.Request for which headers are being managed
20+
log logger.Logger // The logger to use for logging headers
21+
apiHandler apihandler.APIHandler // The APIHandler to use for retrieving standard headers
22+
authTokenHandler *authenticationhandler.AuthTokenHandler // The token to use for setting the Authorization header
2423
}
2524

2625
// NewHeaderHandler creates a new instance of HeaderHandler for a given http.Request, logger, and APIHandler.
@@ -33,7 +32,6 @@ func NewHeaderHandler(req *http.Request, log logger.Logger, apiHandler apihandle
3332
}
3433
}
3534

36-
// SetAuthorization sets the Authorization header for the request.
3735
// func (h *HeaderHandler) SetAuthorization(token string) {
3836
// // Ensure the token is prefixed with "Bearer " only once
3937
// if !strings.HasPrefix(token, "Bearer ") {
@@ -42,6 +40,7 @@ func NewHeaderHandler(req *http.Request, log logger.Logger, apiHandler apihandle
4240
// h.req.Header.Set("Authorization", token)
4341
// }
4442

43+
// SetAuthorization sets the Authorization header for the request.
4544
func (h *HeaderHandler) SetAuthorization() {
4645
token := h.authTokenHandler.Token
4746
if !strings.HasPrefix(token, "Bearer ") {

headers/headers_test.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// headers/headers_test.go
2+
package headers
3+
4+
import (
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// TestSetAuthorization verifies that the SetAuthorization method correctly sets
14+
// the "Authorization" header of the HTTP request. The header should be prefixed
15+
// with "Bearer " followed by the token provided by the authTokenHandler.
16+
func TestSetAuthorization(t *testing.T) {
17+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
18+
19+
token := "test-token"
20+
authTokenHandler := &authenticationhandler.AuthTokenHandler{Token: token}
21+
22+
// Create HeaderHandler without a mock logger since logging is not being tested
23+
headerHandler := NewHeaderHandler(req, nil, nil, authTokenHandler)
24+
headerHandler.SetAuthorization()
25+
26+
expectedHeaderValue := "Bearer " + token
27+
assert.Equal(t, expectedHeaderValue, req.Header.Get("Authorization"), "Authorization header should be correctly set")
28+
}
29+
30+
// TestSetContentType verifies that the SetContentType method correctly sets
31+
// the "Content-Type" header of the HTTP request. This header should reflect
32+
// the content type passed to the method.
33+
func TestSetContentType(t *testing.T) {
34+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
35+
36+
contentType := "application/json"
37+
// Create HeaderHandler without a mock logger since logging is not being tested
38+
headerHandler := NewHeaderHandler(req, nil, nil, nil)
39+
headerHandler.SetContentType(contentType)
40+
41+
assert.Equal(t, contentType, req.Header.Get("Content-Type"), "Content-Type header should be correctly set")
42+
}
43+
44+
// TestSetAccept verifies that the SetAccept method correctly sets the "Accept"
45+
// header of the HTTP request. This header indicates the media types that the
46+
// client is willing to receive from the server.
47+
func TestSetAccept(t *testing.T) {
48+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
49+
50+
acceptHeader := "application/json"
51+
headerHandler := NewHeaderHandler(req, nil, nil, nil)
52+
headerHandler.SetAccept(acceptHeader)
53+
54+
assert.Equal(t, acceptHeader, req.Header.Get("Accept"), "Accept header should be correctly set")
55+
}
56+
57+
// TestSetUserAgent verifies that the SetUserAgent method correctly sets the
58+
// "User-Agent" header of the HTTP request. This header should reflect the user
59+
// agent string passed to the method.
60+
func TestSetUserAgent(t *testing.T) {
61+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
62+
63+
userAgent := "MyCustomUserAgent/1.0"
64+
headerHandler := NewHeaderHandler(req, nil, nil, nil)
65+
headerHandler.SetUserAgent(userAgent)
66+
67+
assert.Equal(t, userAgent, req.Header.Get("User-Agent"), "User-Agent header should be correctly set")
68+
}
69+
70+
// TestSetCacheControlHeader verifies that the SetCacheControlHeader function
71+
// correctly sets the "Cache-Control" header of the HTTP request. This header
72+
// contains directives for caching mechanisms in requests and responses.
73+
func TestSetCacheControlHeader(t *testing.T) {
74+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
75+
76+
cacheControlValue := "no-cache"
77+
SetCacheControlHeader(req, cacheControlValue)
78+
79+
assert.Equal(t, cacheControlValue, req.Header.Get("Cache-Control"), "Cache-Control header should be correctly set")
80+
}
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+
// }

headers/headers_test.go.TODO

Lines changed: 0 additions & 176 deletions
This file was deleted.

0 commit comments

Comments
 (0)