Skip to content

Commit 213db84

Browse files
committed
Refactor HeaderHandler struct and add new methods***
***Add tests for SetAuthorization, SetContentType, SetAccept, SetUserAgent, and SetCacheControlHeader methods
1 parent d91af09 commit 213db84

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
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: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,74 @@ import (
77
"testing"
88

99
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
10-
"github.com/deploymenttheory/go-api-http-client/logger"
11-
"github.com/deploymenttheory/go-api-http-client/mocklogger"
1210
"github.com/stretchr/testify/assert"
13-
"github.com/stretchr/testify/mock"
1411
)
1512

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.
1616
func TestSetAuthorization(t *testing.T) {
1717
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
18-
mockLog := mocklogger.NewMockLogger()
19-
mockLog.On("Debug", mock.Anything, mock.Anything).Once()
2018

2119
token := "test-token"
2220
authTokenHandler := &authenticationhandler.AuthTokenHandler{Token: token}
2321

24-
headerHandler := NewHeaderHandler(req, mockLog, nil, authTokenHandler)
22+
// Create HeaderHandler without a mock logger since logging is not being tested
23+
headerHandler := NewHeaderHandler(req, nil, nil, authTokenHandler)
2524
headerHandler.SetAuthorization()
2625

27-
assert.Equal(t, "Bearer "+token, req.Header.Get("Authorization"), "Authorization header should be correctly set")
28-
mockLog.AssertExpectations(t)
26+
expectedHeaderValue := "Bearer " + token
27+
assert.Equal(t, expectedHeaderValue, req.Header.Get("Authorization"), "Authorization header should be correctly set")
2928
}
3029

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.
3133
func TestSetContentType(t *testing.T) {
3234
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
33-
mockLog := mocklogger.NewMockLogger()
3435

3536
contentType := "application/json"
36-
headerHandler := NewHeaderHandler(req, mockLog, nil, nil)
37+
// Create HeaderHandler without a mock logger since logging is not being tested
38+
headerHandler := NewHeaderHandler(req, nil, nil, nil)
3739
headerHandler.SetContentType(contentType)
3840

3941
assert.Equal(t, contentType, req.Header.Get("Content-Type"), "Content-Type header should be correctly set")
4042
}
4143

42-
func TestLogHeaders(t *testing.T) {
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) {
4348
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
44-
mockLog := mocklogger.NewMockLogger()
45-
mockLog.On("Debug", mock.Anything, mock.Anything).Once()
46-
mockLog.SetLevel(logger.LogLevelDebug)
4749

48-
headerHandler := NewHeaderHandler(req, mockLog, nil, nil)
49-
headerHandler.LogHeaders(true)
50+
acceptHeader := "application/json"
51+
headerHandler := NewHeaderHandler(req, nil, nil, nil)
52+
headerHandler.SetAccept(acceptHeader)
5053

51-
mockLog.AssertExpectations(t)
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")
5280
}

0 commit comments

Comments
 (0)