|
| 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/deploymenttheory/go-api-http-client/logger" |
| 11 | + "github.com/deploymenttheory/go-api-http-client/mocklogger" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | +) |
| 15 | + |
| 16 | +func TestSetAuthorization(t *testing.T) { |
| 17 | + req := httptest.NewRequest(http.MethodGet, "http://example.com", nil) |
| 18 | + mockLog := mocklogger.NewMockLogger() |
| 19 | + mockLog.On("Debug", mock.Anything, mock.Anything).Once() |
| 20 | + |
| 21 | + token := "test-token" |
| 22 | + authTokenHandler := &authenticationhandler.AuthTokenHandler{Token: token} |
| 23 | + |
| 24 | + headerHandler := NewHeaderHandler(req, mockLog, nil, authTokenHandler) |
| 25 | + headerHandler.SetAuthorization() |
| 26 | + |
| 27 | + assert.Equal(t, "Bearer "+token, req.Header.Get("Authorization"), "Authorization header should be correctly set") |
| 28 | + mockLog.AssertExpectations(t) |
| 29 | +} |
| 30 | + |
| 31 | +func TestSetContentType(t *testing.T) { |
| 32 | + req := httptest.NewRequest(http.MethodGet, "http://example.com", nil) |
| 33 | + mockLog := mocklogger.NewMockLogger() |
| 34 | + |
| 35 | + contentType := "application/json" |
| 36 | + headerHandler := NewHeaderHandler(req, mockLog, nil, nil) |
| 37 | + headerHandler.SetContentType(contentType) |
| 38 | + |
| 39 | + assert.Equal(t, contentType, req.Header.Get("Content-Type"), "Content-Type header should be correctly set") |
| 40 | +} |
| 41 | + |
| 42 | +func TestLogHeaders(t *testing.T) { |
| 43 | + 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) |
| 47 | + |
| 48 | + headerHandler := NewHeaderHandler(req, mockLog, nil, nil) |
| 49 | + headerHandler.LogHeaders(true) |
| 50 | + |
| 51 | + mockLog.AssertExpectations(t) |
| 52 | +} |
0 commit comments