|
| 1 | +// httpclient_error_response_test.go |
| 2 | +// This package provides utility functions and structures for handling and categorizing HTTP error responses. |
| 3 | +package httpclient |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +// TestHandleAPIErrorResponse tests the handleAPIErrorResponse function with different types of error responses. |
| 15 | +func TestHandleAPIErrorResponse(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + response *http.Response |
| 19 | + expectedAPIErr *APIError |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "Structured JSON Error", |
| 23 | + response: &http.Response{ |
| 24 | + StatusCode: 400, |
| 25 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 26 | + Body: io.NopCloser(bytes.NewBufferString(`{"error":{"code":"INVALID","message":"Invalid request"}}`)), |
| 27 | + }, |
| 28 | + expectedAPIErr: &APIError{ |
| 29 | + StatusCode: 400, |
| 30 | + Type: "APIError", |
| 31 | + Message: "Invalid request", |
| 32 | + Detail: "", |
| 33 | + Errors: nil, |
| 34 | + Raw: `{"error":{"code":"INVALID","message":"Invalid request"}}`, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Generic JSON Error", |
| 39 | + response: &http.Response{ |
| 40 | + StatusCode: 500, |
| 41 | + Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 42 | + Body: io.NopCloser(bytes.NewBufferString(`{"message":"Internal server error","detail":"Error details"}`)), |
| 43 | + }, |
| 44 | + expectedAPIErr: &APIError{ |
| 45 | + StatusCode: 500, |
| 46 | + Type: "APIError", |
| 47 | + Message: "Internal server error", |
| 48 | + Detail: "Error details", |
| 49 | + Errors: nil, |
| 50 | + Raw: `{"message":"Internal server error","detail":"Error details"}`, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Non-JSON Error", |
| 55 | + response: &http.Response{ |
| 56 | + StatusCode: 404, |
| 57 | + Header: http.Header{"Content-Type": []string{"text/html"}}, |
| 58 | + Body: io.NopCloser(bytes.NewBufferString("<html>Not Found</html>")), |
| 59 | + }, |
| 60 | + expectedAPIErr: &APIError{ |
| 61 | + StatusCode: 404, |
| 62 | + Type: "APIError", |
| 63 | + Message: "An error occurred", |
| 64 | + Detail: "", |
| 65 | + Errors: nil, |
| 66 | + Raw: "<html>Not Found</html>", |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + mockLog := NewMockLogger() |
| 74 | + apiError := handleAPIErrorResponse(tt.response, mockLog) |
| 75 | + |
| 76 | + assert.Equal(t, tt.expectedAPIErr.StatusCode, apiError.StatusCode) |
| 77 | + assert.Equal(t, tt.expectedAPIErr.Message, apiError.Message) |
| 78 | + assert.Equal(t, tt.expectedAPIErr.Detail, apiError.Detail) |
| 79 | + assert.Equal(t, tt.expectedAPIErr.Raw, apiError.Raw) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments