| 1 | package discord_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | "testing/synctest" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/mock" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 14 | ) |
| 15 | |
| 16 | func TestHTTPMethodCompliance(t *testing.T) { |
| 17 | t.Parallel() |
| 18 | synctest.Test(t, func(t *testing.T) { |
| 19 | mockClient := &MockHTTPClient{} |
| 20 | service := createTestService( |
| 21 | t, |
| 22 | "discord://test-token@test-webhook", |
| 23 | mockClient, |
| 24 | ) |
| 25 | |
| 26 | mockClient.On("Do", mock.Anything). |
| 27 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 28 | Once() |
| 29 | |
| 30 | err := service.Send("Test message", nil) |
| 31 | |
| 32 | require.NoError(t, err) |
| 33 | assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 34 | return req.Method == http.MethodPost |
| 35 | }, "POST method") |
| 36 | |
| 37 | mockClient.AssertExpectations(t) |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | func TestHTTPTimeoutConfiguration(t *testing.T) { |
| 42 | t.Parallel() |
| 43 | synctest.Test(t, func(t *testing.T) { |
| 44 | mockClient := &MockHTTPClient{} |
| 45 | service := createTestService( |
| 46 | t, |
| 47 | "discord://test-token@test-webhook", |
| 48 | mockClient, |
| 49 | ) |
| 50 | |
| 51 | mockClient.On("Do", mock.Anything). |
| 52 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 53 | Once() |
| 54 | |
| 55 | start := time.Now() |
| 56 | err := service.Send("Test message", nil) |
| 57 | duration := time.Since(start) |
| 58 | |
| 59 | require.NoError(t, err) |
| 60 | // Should complete relatively quickly (timeout is configured) |
| 61 | assert.Less(t, duration, 10*time.Second) |
| 62 | |
| 63 | mockClient.AssertExpectations(t) |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | func TestHTTPHeaders(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | synctest.Test(t, func(t *testing.T) { |
| 70 | mockClient := &MockHTTPClient{} |
| 71 | service := createTestService( |
| 72 | t, |
| 73 | "discord://test-token@test-webhook", |
| 74 | mockClient, |
| 75 | ) |
| 76 | |
| 77 | mockClient.On("Do", mock.Anything). |
| 78 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 79 | Once() |
| 80 | |
| 81 | err := service.Send("Test message", nil) |
| 82 | |
| 83 | require.NoError(t, err) |
| 84 | assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 85 | contentType := req.Header.Get("Content-Type") |
| 86 | userAgent := req.Header.Get("User-Agent") |
| 87 | |
| 88 | return contentType == "application/json" && userAgent != "" |
| 89 | }, "headers") |
| 90 | |
| 91 | mockClient.AssertExpectations(t) |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | func TestHTTPURLConstruction(t *testing.T) { |
| 96 | t.Parallel() |
| 97 | synctest.Test(t, func(t *testing.T) { |
| 98 | mockClient := &MockHTTPClient{} |
| 99 | |
| 100 | tests := []struct { |
| 101 | name string |
| 102 | webhookURL string |
| 103 | expectedURL string |
| 104 | }{ |
| 105 | { |
| 106 | name: "basic webhook", |
| 107 | webhookURL: "discord://token@webhook", |
| 108 | expectedURL: "https://discord.com/api/webhooks/webhook/token", |
| 109 | }, |
| 110 | { |
| 111 | name: "webhook with thread", |
| 112 | webhookURL: "discord://token@webhook?thread_id=123", |
| 113 | expectedURL: "https://discord.com/api/webhooks/webhook/token?thread_id=123", |
| 114 | }, |
| 115 | { |
| 116 | name: "numeric webhook", |
| 117 | webhookURL: "discord://123@456", |
| 118 | expectedURL: "https://discord.com/api/webhooks/456/123", |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | for _, tt := range tests { |
| 123 | mockClient.On("Do", mock.Anything). |
| 124 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 125 | Once() |
| 126 | |
| 127 | testService := createTestService(t, tt.webhookURL, mockClient) |
| 128 | |
| 129 | err := testService.Send("Test", nil) |
| 130 | |
| 131 | require.NoError(t, err) |
| 132 | assertRequestMade(t, mockClient, tt.expectedURL) |
| 133 | } |
| 134 | |
| 135 | mockClient.AssertExpectations(t) |
| 136 | }) |
| 137 | } |
| 138 | |
| 139 | func TestHTTPRetryMechanism(t *testing.T) { |
| 140 | t.Parallel() |
| 141 | synctest.Test(t, func(t *testing.T) { |
| 142 | mockClient := &MockHTTPClient{} |
| 143 | service := createTestService( |
| 144 | t, |
| 145 | "discord://test-token@test-webhook", |
| 146 | mockClient, |
| 147 | ) |
| 148 | |
| 149 | // Test retry on rate limit |
| 150 | mockClient.On("Do", mock.Anything). |
| 151 | Return(createMockResponse(http.StatusTooManyRequests, `{"retry_after": 0.1}`), nil). |
| 152 | Once() |
| 153 | mockClient.On("Do", mock.Anything). |
| 154 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 155 | Once() |
| 156 | |
| 157 | err := service.Send("Test message", nil) |
| 158 | |
| 159 | require.NoError(t, err) |
| 160 | mockClient.AssertNumberOfCalls(t, "Do", 2) |
| 161 | |
| 162 | mockClient.AssertExpectations(t) |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | func TestHTTPRetryOnNetworkError(t *testing.T) { |
| 167 | t.Parallel() |
| 168 | synctest.Test(t, func(t *testing.T) { |
| 169 | mockClient := &MockHTTPClient{} |
| 170 | service := createTestService( |
| 171 | t, |
| 172 | "discord://test-token@test-webhook", |
| 173 | mockClient, |
| 174 | ) |
| 175 | |
| 176 | // Use a timeout error which is considered transient |
| 177 | timeoutErr := timeoutError{} |
| 178 | mockClient.On("Do", mock.Anything). |
| 179 | Return(nil, timeoutErr). |
| 180 | Once() |
| 181 | mockClient.On("Do", mock.Anything). |
| 182 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 183 | Once() |
| 184 | |
| 185 | err := service.Send("Test message", nil) |
| 186 | |
| 187 | require.NoError(t, err) |
| 188 | mockClient.AssertNumberOfCalls(t, "Do", 2) |
| 189 | |
| 190 | mockClient.AssertExpectations(t) |
| 191 | }) |
| 192 | } |
| 193 | |
| 194 | func TestHTTPMaxRetries(t *testing.T) { |
| 195 | t.Parallel() |
| 196 | |
| 197 | mockClient := &MockHTTPClient{} |
| 198 | service := createTestService( |
| 199 | t, |
| 200 | "discord://test-token@test-webhook", |
| 201 | mockClient, |
| 202 | ) |
| 203 | |
| 204 | // Use noOpSleeper to avoid real sleeps in tests |
| 205 | service.Sleeper = &noOpSleeper{} |
| 206 | |
| 207 | // Test that it doesn't retry indefinitely |
| 208 | mockClient.On("Do", mock.Anything). |
| 209 | Return(createMockResponse(http.StatusTooManyRequests, `{"retry_after": 0.001}`), nil). |
| 210 | Times(6) |
| 211 | // Max retries + 1 initial attempt |
| 212 | |
| 213 | err := service.Send("Test message", nil) |
| 214 | |
| 215 | require.Error(t, err) |
| 216 | assert.Contains(t, err.Error(), "failed to send discord notification") |
| 217 | |
| 218 | mockClient.AssertExpectations(t) |
| 219 | } |
| 220 | |
| 221 | func TestHTTPContextCancellation(t *testing.T) { |
| 222 | t.Parallel() |
| 223 | synctest.Test(t, func(t *testing.T) { |
| 224 | mockClient := &MockHTTPClient{} |
| 225 | service := createTestService( |
| 226 | t, |
| 227 | "discord://test-token@test-webhook", |
| 228 | mockClient, |
| 229 | ) |
| 230 | |
| 231 | // This is harder to test directly, but we can verify timeout behavior |
| 232 | mockClient.On("Do", mock.Anything). |
| 233 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 234 | Once() |
| 235 | |
| 236 | err := service.Send("Test message", nil) |
| 237 | |
| 238 | require.NoError(t, err) |
| 239 | // Context should be properly set with timeout |
| 240 | |
| 241 | mockClient.AssertExpectations(t) |
| 242 | }) |
| 243 | } |
| 244 | |
| 245 | func TestHTTPResponseStatusHandling(t *testing.T) { |
| 246 | t.Parallel() |
| 247 | |
| 248 | mockClient := &MockHTTPClient{} |
| 249 | service := createTestService( |
| 250 | t, |
| 251 | "discord://test-token@test-webhook", |
| 252 | mockClient, |
| 253 | ) |
| 254 | |
| 255 | // Use noOpSleeper to avoid real sleeps in tests |
| 256 | service.Sleeper = &noOpSleeper{} |
| 257 | |
| 258 | tests := []struct { |
| 259 | name string |
| 260 | statusCode int |
| 261 | responseBody string |
| 262 | expectError bool |
| 263 | expectedCalls int |
| 264 | }{ |
| 265 | {"success", http.StatusNoContent, "", false, 1}, |
| 266 | {"bad request", http.StatusBadRequest, `{"message": "error"}`, true, 1}, |
| 267 | {"unauthorized", http.StatusUnauthorized, `{"message": "error"}`, true, 1}, |
| 268 | {"forbidden", http.StatusForbidden, `{"message": "error"}`, true, 1}, |
| 269 | {"not found", http.StatusNotFound, `{"message": "error"}`, true, 1}, |
| 270 | { |
| 271 | "rate limited", |
| 272 | http.StatusTooManyRequests, |
| 273 | `{"retry_after": 1}`, |
| 274 | true, |
| 275 | 6, |
| 276 | }, // Should retry but eventually fail |
| 277 | {"server error", http.StatusInternalServerError, `{"message": "error"}`, true, 6}, |
| 278 | } |
| 279 | |
| 280 | for _, tt := range tests { |
| 281 | mockClient.On("Do", mock.Anything). |
| 282 | Return(createMockResponse(tt.statusCode, tt.responseBody), nil). |
| 283 | Times(tt.expectedCalls) |
| 284 | |
| 285 | err := service.Send("Test message", nil) |
| 286 | |
| 287 | if tt.expectError { |
| 288 | require.Error(t, err) |
| 289 | } else { |
| 290 | require.NoError(t, err) |
| 291 | } |
| 292 | |
| 293 | mockClient.AssertExpectations(t) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | func TestHTTPMultipartUpload(t *testing.T) { |
| 298 | t.Parallel() |
| 299 | synctest.Test(t, func(t *testing.T) { |
| 300 | mockClient := &MockHTTPClient{} |
| 301 | service := createTestService( |
| 302 | t, |
| 303 | "discord://test-token@test-webhook", |
| 304 | mockClient, |
| 305 | ) |
| 306 | |
| 307 | mockClient.On("Do", mock.Anything). |
| 308 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 309 | Once() |
| 310 | |
| 311 | items := []types.MessageItem{ |
| 312 | createTestMessageItemWithFile("Test", "file.txt", []byte("content")), |
| 313 | } |
| 314 | |
| 315 | err := service.SendItems(items, nil) |
| 316 | |
| 317 | require.NoError(t, err) |
| 318 | assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 319 | return req.Header.Get("Content-Type") != "application/json" // Should be multipart |
| 320 | }, "multipart content type") |
| 321 | |
| 322 | mockClient.AssertExpectations(t) |
| 323 | }) |
| 324 | } |
| 325 | |
| 326 | func TestHTTPConnectionReuse(t *testing.T) { |
| 327 | t.Parallel() |
| 328 | synctest.Test(t, func(t *testing.T) { |
| 329 | mockClient := &MockHTTPClient{} |
| 330 | service := createTestService( |
| 331 | t, |
| 332 | "discord://test-token@test-webhook", |
| 333 | mockClient, |
| 334 | ) |
| 335 | |
| 336 | // Test multiple requests reuse connections (hard to test directly with mock) |
| 337 | mockClient.On("Do", mock.Anything). |
| 338 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 339 | Times(3) |
| 340 | |
| 341 | err1 := service.Send("Message 1", nil) |
| 342 | err2 := service.Send("Message 2", nil) |
| 343 | err3 := service.Send("Message 3", nil) |
| 344 | |
| 345 | require.NoError(t, err1) |
| 346 | require.NoError(t, err2) |
| 347 | require.NoError(t, err3) |
| 348 | mockClient.AssertNumberOfCalls(t, "Do", 3) |
| 349 | |
| 350 | mockClient.AssertExpectations(t) |
| 351 | }) |
| 352 | } |