|
| 1 | +package response |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/deploymenttheory/go-api-http-client/mocklogger" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | +) |
| 12 | + |
| 13 | +// MockLogger is a mock type for the Logger interface, useful for testing without needing a real logger implementation. |
| 14 | +type MockLogger struct { |
| 15 | + mock.Mock |
| 16 | +} |
| 17 | + |
| 18 | +func (m *MockLogger) Error(msg string, fields ...interface{}) error { |
| 19 | + args := m.Called(msg, fields) |
| 20 | + return args.Error(0) |
| 21 | +} |
| 22 | + |
| 23 | +// TestHandleAPIErrorResponse tests the handling of various API error responses. |
| 24 | +func TestHandleAPIErrorResponse(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + responseStatus int |
| 28 | + responseBody string |
| 29 | + expectedAPIError *APIError |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "structured JSON error", |
| 33 | + responseStatus: http.StatusBadRequest, |
| 34 | + responseBody: `{"error": {"code": "400", "message": "Bad Request"}}`, |
| 35 | + expectedAPIError: &APIError{ |
| 36 | + StatusCode: http.StatusBadRequest, |
| 37 | + Type: "APIError", |
| 38 | + Message: "An error occurred", |
| 39 | + Raw: `{"error": {"code": "400", "message": "Bad Request"}}`, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "generic JSON error", |
| 44 | + responseStatus: http.StatusInternalServerError, |
| 45 | + responseBody: `{"message": "Internal Server Error", "detail": "Server crashed"}`, |
| 46 | + expectedAPIError: &APIError{ |
| 47 | + StatusCode: http.StatusInternalServerError, |
| 48 | + Type: "APIError", |
| 49 | + Message: "An error occurred", |
| 50 | + Raw: `{"message": "Internal Server Error", "detail": "Server crashed"}`, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "non-JSON error", |
| 55 | + responseStatus: http.StatusServiceUnavailable, |
| 56 | + responseBody: `<html><body>Service Unavailable</body></html>`, |
| 57 | + expectedAPIError: &APIError{ |
| 58 | + StatusCode: http.StatusServiceUnavailable, |
| 59 | + Type: "APIError", |
| 60 | + Message: "An error occurred", |
| 61 | + Raw: `<html><body>Service Unavailable</body></html>`, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + // Mock HTTP response |
| 69 | + responseRecorder := httptest.NewRecorder() |
| 70 | + responseRecorder.WriteHeader(tt.responseStatus) |
| 71 | + responseRecorder.WriteString(tt.responseBody) |
| 72 | + |
| 73 | + // Create a dummy request and associate it with the response |
| 74 | + dummyReq := httptest.NewRequest("GET", "http://example.com", nil) |
| 75 | + response := responseRecorder.Result() |
| 76 | + response.Request = dummyReq |
| 77 | + |
| 78 | + // Use the centralized MockLogger from the mocklogger package |
| 79 | + mockLogger := mocklogger.NewMockLogger() |
| 80 | + |
| 81 | + // Set up expectations for LogError |
| 82 | + mockLogger.On("LogError", |
| 83 | + mock.AnythingOfType("string"), // event |
| 84 | + mock.AnythingOfType("string"), // method |
| 85 | + mock.AnythingOfType("string"), // url |
| 86 | + mock.AnythingOfType("int"), // statusCode |
| 87 | + mock.AnythingOfType("string"), // status |
| 88 | + mock.Anything, // error |
| 89 | + mock.AnythingOfType("string"), // raw response |
| 90 | + ).Return() |
| 91 | + |
| 92 | + // Call HandleAPIErrorResponse |
| 93 | + result := HandleAPIErrorResponse(response, mockLogger) |
| 94 | + |
| 95 | + // Assert |
| 96 | + assert.Equal(t, tt.expectedAPIError.StatusCode, result.StatusCode) |
| 97 | + assert.Equal(t, tt.expectedAPIError.Type, result.Type) |
| 98 | + assert.Equal(t, tt.expectedAPIError.Raw, result.Raw) |
| 99 | + |
| 100 | + // Assert that all expectations were met |
| 101 | + mockLogger.AssertExpectations(t) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments