| 1 | package signalgrid_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "testing/synctest" |
| 8 | |
| 9 | "github.com/stretchr/testify/mock" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | ) |
| 12 | |
| 13 | func TestSendWithHTTPError(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | synctest.Test(t, func(t *testing.T) { |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | statusCode int |
| 19 | response string |
| 20 | }{ |
| 21 | {"bad request", http.StatusBadRequest, "invalid"}, |
| 22 | {"unauthorized", http.StatusUnauthorized, "denied"}, |
| 23 | {"forbidden", http.StatusForbidden, "forbidden"}, |
| 24 | {"not found", http.StatusNotFound, "missing"}, |
| 25 | {"internal server error", http.StatusInternalServerError, "boom"}, |
| 26 | } |
| 27 | |
| 28 | for _, tt := range tests { |
| 29 | mockClient := &MockHTTPClient{} |
| 30 | service := createTestService(t, validSignalgridURL, mockClient) |
| 31 | |
| 32 | mockClient.On("Do", mock.Anything). |
| 33 | Return(createMockResponse(tt.statusCode, tt.response), nil). |
| 34 | Once() |
| 35 | |
| 36 | err := service.Send("Test message", nil) |
| 37 | require.Error(t, err, "Expected error for %s", tt.name) |
| 38 | |
| 39 | mockClient.AssertExpectations(t) |
| 40 | } |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | func TestSendWithNetworkError(t *testing.T) { |
| 45 | t.Parallel() |
| 46 | synctest.Test(t, func(t *testing.T) { |
| 47 | mockClient := &MockHTTPClient{} |
| 48 | service := createTestService(t, validSignalgridURL, mockClient) |
| 49 | |
| 50 | mockClient.On("Do", mock.Anything). |
| 51 | Return(nil, errors.New("connection refused")). |
| 52 | Once() |
| 53 | |
| 54 | err := service.Send("Test message", nil) |
| 55 | require.Error(t, err) |
| 56 | |
| 57 | mockClient.AssertExpectations(t) |
| 58 | }) |
| 59 | } |