|
| 1 | +package httpclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/mock" |
| 9 | +) |
| 10 | + |
| 11 | +// MockAPIHandler is a mock type for the APIHandler interface |
| 12 | +type MockAPIHandler struct { |
| 13 | + mock.Mock |
| 14 | +} |
| 15 | + |
| 16 | +// GetAPIRequestHeaders mocks the GetAPIRequestHeaders method of the APIHandler |
| 17 | +func (_m *MockAPIHandler) GetAPIRequestHeaders(endpoint string) map[string]string { |
| 18 | + ret := _m.Called(endpoint) |
| 19 | + |
| 20 | + var r0 map[string]string |
| 21 | + if rf, ok := ret.Get(0).(func(string) map[string]string); ok { |
| 22 | + r0 = rf(endpoint) |
| 23 | + } else { |
| 24 | + r0 = ret.Get(0).(map[string]string) |
| 25 | + } |
| 26 | + |
| 27 | + return r0 |
| 28 | +} |
| 29 | + |
| 30 | +// TestSetAuthorization tests the SetAuthorization method |
| 31 | +func TestSetAuthorization(t *testing.T) { |
| 32 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 33 | + logger := NewMockLogger() // Assuming you have a mock logger |
| 34 | + hm := NewHeaderManager(req, logger, nil, "") |
| 35 | + |
| 36 | + // Test without Bearer prefix |
| 37 | + hm.SetAuthorization("token123") |
| 38 | + assert.Equal(t, "Bearer token123", req.Header.Get("Authorization")) |
| 39 | + |
| 40 | + // Test with Bearer prefix |
| 41 | + hm.SetAuthorization("Bearer token123") |
| 42 | + assert.Equal(t, "Bearer token123", req.Header.Get("Authorization")) |
| 43 | +} |
| 44 | + |
| 45 | +// TestSetContentType tests the SetContentType method |
| 46 | +func TestSetContentType(t *testing.T) { |
| 47 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 48 | + logger := NewMockLogger() // Assuming you have a mock logger |
| 49 | + hm := NewHeaderManager(req, logger, nil, "") |
| 50 | + |
| 51 | + hm.SetContentType("application/json") |
| 52 | + assert.Equal(t, "application/json", req.Header.Get("Content-Type")) |
| 53 | +} |
| 54 | + |
| 55 | +// TestSetAccept tests the SetAccept method |
| 56 | +func TestSetAccept(t *testing.T) { |
| 57 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 58 | + logger := NewMockLogger() // Assuming you have a mock logger |
| 59 | + hm := NewHeaderManager(req, logger, nil, "") |
| 60 | + |
| 61 | + hm.SetAccept("application/json") |
| 62 | + assert.Equal(t, "application/json", req.Header.Get("Accept")) |
| 63 | +} |
| 64 | + |
| 65 | +// TestSetUserAgent tests the SetUserAgent method |
| 66 | +func TestSetUserAgent(t *testing.T) { |
| 67 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 68 | + logger := NewMockLogger() // Assuming you have a mock logger |
| 69 | + hm := NewHeaderManager(req, logger, nil, "") |
| 70 | + |
| 71 | + hm.SetUserAgent("CustomUserAgent/1.0") |
| 72 | + assert.Equal(t, "CustomUserAgent/1.0", req.Header.Get("User-Agent")) |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | +// TestSetRequestHeaders tests the SetRequestHeaders method |
| 77 | +func TestSetRequestHeaders(t *testing.T) { |
| 78 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 79 | + logger := NewMockLogger() // Assuming you have a mock logger |
| 80 | + mockAPIHandler := new(MockAPIHandler) |
| 81 | + hm := NewHeaderManager(req, logger, mockAPIHandler, "token123") |
| 82 | +
|
| 83 | + // Setup expectations |
| 84 | + mockAPIHandler.On("GetAPIRequestHeaders", "testEndpoint").Return(map[string]string{ |
| 85 | + "Authorization": "", |
| 86 | + "X-Custom-Header": "CustomValue", |
| 87 | + }) |
| 88 | +
|
| 89 | + hm.SetRequestHeaders("testEndpoint") |
| 90 | +
|
| 91 | + // Assertions |
| 92 | + assert.Equal(t, "Bearer token123", req.Header.Get("Authorization")) |
| 93 | + assert.Equal(t, "CustomValue", req.Header.Get("X-Custom-Header")) |
| 94 | + mockAPIHandler.AssertExpectations(t) |
| 95 | +} |
| 96 | +*/ |
| 97 | +// TestSetCacheControlHeader tests the SetCacheControlHeader function |
| 98 | +func TestSetCacheControlHeader(t *testing.T) { |
| 99 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 100 | + |
| 101 | + SetCacheControlHeader(req, "no-cache") |
| 102 | + assert.Equal(t, "no-cache", req.Header.Get("Cache-Control")) |
| 103 | +} |
| 104 | + |
| 105 | +// TestSetConditionalHeaders tests the SetConditionalHeaders function |
| 106 | +func TestSetConditionalHeaders(t *testing.T) { |
| 107 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 108 | + |
| 109 | + SetConditionalHeaders(req, "Wed, 21 Oct 2015 07:28:00 GMT", "etagValue") |
| 110 | + assert.Equal(t, "Wed, 21 Oct 2015 07:28:00 GMT", req.Header.Get("If-Modified-Since")) |
| 111 | + assert.Equal(t, "etagValue", req.Header.Get("If-None-Match")) |
| 112 | +} |
| 113 | + |
| 114 | +// TestSetAcceptEncodingHeader tests the SetAcceptEncodingHeader function |
| 115 | +func TestSetAcceptEncodingHeader(t *testing.T) { |
| 116 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 117 | + |
| 118 | + SetAcceptEncodingHeader(req, "gzip, deflate") |
| 119 | + assert.Equal(t, "gzip, deflate", req.Header.Get("Accept-Encoding")) |
| 120 | +} |
| 121 | + |
| 122 | +// TestSetRefererHeader tests the SetRefererHeader function |
| 123 | +func TestSetRefererHeader(t *testing.T) { |
| 124 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 125 | + |
| 126 | + SetRefererHeader(req, "http://referrer.example.com") |
| 127 | + assert.Equal(t, "http://referrer.example.com", req.Header.Get("Referer")) |
| 128 | +} |
| 129 | + |
| 130 | +// TestSetXForwardedForHeader tests the SetXForwardedForHeader function |
| 131 | +func TestSetXForwardedForHeader(t *testing.T) { |
| 132 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 133 | + |
| 134 | + SetXForwardedForHeader(req, "client1, proxy1, proxy2") |
| 135 | + assert.Equal(t, "client1, proxy1, proxy2", req.Header.Get("X-Forwarded-For")) |
| 136 | +} |
| 137 | + |
| 138 | +// TestSetCustomHeader tests the ability to set arbitrary headers |
| 139 | +func TestSetCustomHeader(t *testing.T) { |
| 140 | + req, _ := http.NewRequest("GET", "http://example.com", nil) |
| 141 | + |
| 142 | + SetCustomHeader(req, "X-Custom-Header", "CustomValue") |
| 143 | + assert.Equal(t, "CustomValue", req.Header.Get("X-Custom-Header")) |
| 144 | +} |
0 commit comments