| 1 | package discord_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "testing/synctest" |
| 8 | "unicode/utf8" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | "github.com/stretchr/testify/mock" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 15 | ) |
| 16 | |
| 17 | func TestEmptyMessage(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | synctest.Test(t, func(t *testing.T) { |
| 20 | mockClient := &MockHTTPClient{} |
| 21 | service := createTestService( |
| 22 | t, |
| 23 | "discord://test-token@test-webhook", |
| 24 | mockClient, |
| 25 | ) |
| 26 | |
| 27 | err := service.Send("", nil) |
| 28 | |
| 29 | require.Error(t, err) |
| 30 | require.Contains(t, err.Error(), "message is empty") |
| 31 | |
| 32 | mockClient.AssertExpectations(t) |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | func TestNilMessageItems(t *testing.T) { |
| 37 | t.Parallel() |
| 38 | synctest.Test(t, func(t *testing.T) { |
| 39 | mockClient := &MockHTTPClient{} |
| 40 | service := createTestService( |
| 41 | t, |
| 42 | "discord://test-token@test-webhook", |
| 43 | mockClient, |
| 44 | ) |
| 45 | |
| 46 | err := service.SendItems(nil, nil) |
| 47 | |
| 48 | require.Error(t, err) |
| 49 | require.Contains(t, err.Error(), "creating payload") |
| 50 | |
| 51 | mockClient.AssertExpectations(t) |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func TestEmptyMessageItems(t *testing.T) { |
| 56 | t.Parallel() |
| 57 | synctest.Test(t, func(t *testing.T) { |
| 58 | mockClient := &MockHTTPClient{} |
| 59 | service := createTestService( |
| 60 | t, |
| 61 | "discord://test-token@test-webhook", |
| 62 | mockClient, |
| 63 | ) |
| 64 | |
| 65 | err := service.SendItems([]types.MessageItem{}, nil) |
| 66 | |
| 67 | require.Error(t, err) |
| 68 | require.Contains(t, err.Error(), "creating payload") |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | func TestUnicodeMessages(t *testing.T) { |
| 73 | t.Parallel() |
| 74 | synctest.Test(t, func(t *testing.T) { |
| 75 | mockClient := &MockHTTPClient{} |
| 76 | service := createTestService( |
| 77 | t, |
| 78 | "discord://test-token@test-webhook", |
| 79 | mockClient, |
| 80 | ) |
| 81 | |
| 82 | tests := []struct { |
| 83 | name string |
| 84 | message string |
| 85 | }{ |
| 86 | {"emoji", "Hello 🌍 🚀 ❤️"}, |
| 87 | {"chinese", "你好世界"}, |
| 88 | {"japanese", "こんにちは世界"}, |
| 89 | {"arabic", "مرحبا بالعالم"}, |
| 90 | {"mixed", "Hello 世界 🌍 こんにちは 🚀"}, |
| 91 | {"combining", "café naïve résumé"}, |
| 92 | {"zero width", "test\u200bhidden\u200btext"}, // zero-width spaces |
| 93 | } |
| 94 | |
| 95 | for _, tt := range tests { |
| 96 | mockClient.On("Do", mock.Anything). |
| 97 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 98 | Once() |
| 99 | |
| 100 | err := service.Send(tt.message, nil) |
| 101 | |
| 102 | require.NoError(t, err) |
| 103 | require.True(t, utf8.ValidString(tt.message), "Message should be valid UTF-8") |
| 104 | } |
| 105 | |
| 106 | mockClient.AssertExpectations(t) |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | func TestControlCharacters(t *testing.T) { |
| 111 | t.Parallel() |
| 112 | synctest.Test(t, func(t *testing.T) { |
| 113 | tests := []struct { |
| 114 | name string |
| 115 | message string |
| 116 | }{ |
| 117 | {"newlines", "Line 1\nLine 2\nLine 3"}, |
| 118 | {"tabs", "Col1\tCol2\tCol3"}, |
| 119 | {"carriage return", "Line 1\r\nLine 2"}, |
| 120 | {"form feed", "Page 1\fPage 2"}, |
| 121 | {"vertical tab", "Line 1\vLine 2"}, |
| 122 | {"backspace", "Text\b"}, |
| 123 | {"null byte", "Text\x00More"}, |
| 124 | {"escape", "Text\x1b[31mRed\x1b[0m"}, |
| 125 | } |
| 126 | |
| 127 | for _, tt := range tests { |
| 128 | mockClient := &MockHTTPClient{} |
| 129 | service := createTestService( |
| 130 | t, |
| 131 | "discord://test-token@test-webhook", |
| 132 | mockClient, |
| 133 | ) |
| 134 | |
| 135 | // With splitLines enabled by default, all lines are sent as embeds in one request |
| 136 | expectedCalls := 1 |
| 137 | mockClient.On("Do", mock.Anything). |
| 138 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 139 | Times(expectedCalls) |
| 140 | |
| 141 | err := service.Send(tt.message, nil) |
| 142 | |
| 143 | require.NoError(t, err) |
| 144 | mockClient.AssertExpectations(t) |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | |
| 149 | func TestVeryLongMessage(t *testing.T) { |
| 150 | t.Parallel() |
| 151 | synctest.Test(t, func(t *testing.T) { |
| 152 | mockClient := &MockHTTPClient{} |
| 153 | service := createTestService( |
| 154 | t, |
| 155 | "discord://test-token@test-webhook", |
| 156 | mockClient, |
| 157 | ) |
| 158 | |
| 159 | mockClient.On("Do", mock.Anything). |
| 160 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 161 | Once() |
| 162 | // Long message sent as one |
| 163 | |
| 164 | longMessage := strings.Repeat("This is a very long message that should be chunked. ", 1000) |
| 165 | err := service.Send(longMessage, nil) |
| 166 | |
| 167 | require.NoError(t, err) |
| 168 | mockClient.AssertNumberOfCalls(t, "Do", 1) |
| 169 | |
| 170 | mockClient.AssertExpectations(t) |
| 171 | }) |
| 172 | } |
| 173 | |
| 174 | func TestMessageWithMaximumLength(t *testing.T) { |
| 175 | t.Parallel() |
| 176 | synctest.Test(t, func(t *testing.T) { |
| 177 | mockClient := &MockHTTPClient{} |
| 178 | service := createTestService( |
| 179 | t, |
| 180 | "discord://test-token@test-webhook", |
| 181 | mockClient, |
| 182 | ) |
| 183 | |
| 184 | // Discord's max message length is 2000 chars |
| 185 | maxLengthMessage := strings.Repeat("a", 2000) |
| 186 | |
| 187 | mockClient.On("Do", mock.Anything). |
| 188 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 189 | Once() |
| 190 | |
| 191 | err := service.Send(maxLengthMessage, nil) |
| 192 | |
| 193 | require.NoError(t, err) |
| 194 | |
| 195 | mockClient.AssertExpectations(t) |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | func TestMessageWithNullBytes(t *testing.T) { |
| 200 | t.Parallel() |
| 201 | synctest.Test(t, func(t *testing.T) { |
| 202 | mockClient := &MockHTTPClient{} |
| 203 | service := createTestService( |
| 204 | t, |
| 205 | "discord://test-token@test-webhook", |
| 206 | mockClient, |
| 207 | ) |
| 208 | |
| 209 | messageWithNulls := "Hello\x00World\x00Test" |
| 210 | |
| 211 | mockClient.On("Do", mock.Anything). |
| 212 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 213 | Once() |
| 214 | |
| 215 | err := service.Send(messageWithNulls, nil) |
| 216 | |
| 217 | require.NoError(t, err) |
| 218 | |
| 219 | mockClient.AssertExpectations(t) |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | func TestInvalidUTF8(t *testing.T) { |
| 224 | t.Parallel() |
| 225 | synctest.Test(t, func(t *testing.T) { |
| 226 | mockClient := &MockHTTPClient{} |
| 227 | service := createTestService( |
| 228 | t, |
| 229 | "discord://test-token@test-webhook", |
| 230 | mockClient, |
| 231 | ) |
| 232 | |
| 233 | // Create invalid UTF-8 sequence |
| 234 | invalidUTF8 := []byte{0x80, 0x81, 0x82} // Invalid UTF-8 start bytes |
| 235 | message := string(invalidUTF8) |
| 236 | |
| 237 | require.False(t, utf8.ValidString(message), "Should be invalid UTF-8") |
| 238 | |
| 239 | mockClient.On("Do", mock.Anything). |
| 240 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 241 | Once() |
| 242 | |
| 243 | err := service.Send(message, nil) |
| 244 | |
| 245 | require.NoError(t, err) // Should handle invalid UTF-8 gracefully |
| 246 | |
| 247 | mockClient.AssertExpectations(t) |
| 248 | }) |
| 249 | } |
| 250 | |
| 251 | func TestExtremelyLargeFile(t *testing.T) { |
| 252 | t.Parallel() |
| 253 | synctest.Test(t, func(t *testing.T) { |
| 254 | mockClient := &MockHTTPClient{} |
| 255 | service := createTestService( |
| 256 | t, |
| 257 | "discord://test-token@test-webhook", |
| 258 | mockClient, |
| 259 | ) |
| 260 | |
| 261 | // Test with a very large file (simulated) |
| 262 | largeData := make([]byte, 50*1024*1024) // 50MB |
| 263 | for i := range largeData { |
| 264 | largeData[i] = byte(i % 256) |
| 265 | } |
| 266 | |
| 267 | mockClient.On("Do", mock.Anything). |
| 268 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 269 | Once() |
| 270 | |
| 271 | items := []types.MessageItem{ |
| 272 | createTestMessageItemWithFile("Large file", "large.dat", largeData), |
| 273 | } |
| 274 | |
| 275 | err := service.SendItems(items, nil) |
| 276 | |
| 277 | require.NoError(t, err) |
| 278 | |
| 279 | mockClient.AssertExpectations(t) |
| 280 | }) |
| 281 | } |
| 282 | |
| 283 | func TestFileWithEmptyName(t *testing.T) { |
| 284 | t.Parallel() |
| 285 | synctest.Test(t, func(t *testing.T) { |
| 286 | mockClient := &MockHTTPClient{} |
| 287 | service := createTestService( |
| 288 | t, |
| 289 | "discord://test-token@test-webhook", |
| 290 | mockClient, |
| 291 | ) |
| 292 | |
| 293 | mockClient.On("Do", mock.Anything). |
| 294 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 295 | Once() |
| 296 | |
| 297 | items := []types.MessageItem{ |
| 298 | createTestMessageItemWithFile("Test", "", []byte("content")), |
| 299 | } |
| 300 | |
| 301 | err := service.SendItems(items, nil) |
| 302 | |
| 303 | require.NoError(t, err) |
| 304 | |
| 305 | mockClient.AssertExpectations(t) |
| 306 | }) |
| 307 | } |
| 308 | |
| 309 | func TestFileWithSpecialCharactersInName(t *testing.T) { |
| 310 | t.Parallel() |
| 311 | synctest.Test(t, func(t *testing.T) { |
| 312 | mockClient := &MockHTTPClient{} |
| 313 | service := createTestService( |
| 314 | t, |
| 315 | "discord://test-token@test-webhook", |
| 316 | mockClient, |
| 317 | ) |
| 318 | |
| 319 | specialNames := []string{ |
| 320 | "file with spaces.txt", |
| 321 | "file-with-dashes.txt", |
| 322 | "file_with_underscores.txt", |
| 323 | "file.with.dots.txt", |
| 324 | "file(1).txt", |
| 325 | "file[1].txt", |
| 326 | "file+plus.txt", |
| 327 | "file%percent.txt", |
| 328 | "file#hash.txt", |
| 329 | "[email protected]", |
| 330 | "file$dollar.txt", |
| 331 | "file&ersand.txt", |
| 332 | "file*star.txt", |
| 333 | "file?question.txt", |
| 334 | "file^caret.txt", |
| 335 | "file`backtick.txt", |
| 336 | "file|pipe.txt", |
| 337 | "file\\backslash.txt", |
| 338 | "file/slash.txt", |
| 339 | "file:colon.txt", |
| 340 | "file;semicolon.txt", |
| 341 | "file<less.txt", |
| 342 | "file>greater.txt", |
| 343 | "file\"quote.txt", |
| 344 | "file'apostrophe.txt", |
| 345 | } |
| 346 | |
| 347 | for _, filename := range specialNames { |
| 348 | mockClient.On("Do", mock.Anything). |
| 349 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 350 | Once() |
| 351 | |
| 352 | items := []types.MessageItem{ |
| 353 | createTestMessageItemWithFile("Test file", filename, []byte("content")), |
| 354 | } |
| 355 | |
| 356 | err := service.SendItems(items, nil) |
| 357 | |
| 358 | require.NoError(t, err) |
| 359 | } |
| 360 | |
| 361 | mockClient.AssertExpectations(t) |
| 362 | }) |
| 363 | } |
| 364 | |
| 365 | func TestConcurrentRequests(t *testing.T) { |
| 366 | t.Parallel() |
| 367 | synctest.Test(t, func(t *testing.T) { |
| 368 | // Test multiple concurrent requests |
| 369 | done := make(chan bool, 3) |
| 370 | |
| 371 | for i := range 3 { |
| 372 | go func(_ int) { |
| 373 | mockClient := &MockHTTPClient{} |
| 374 | mockClient.On("Do", mock.Anything). |
| 375 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 376 | Once() |
| 377 | |
| 378 | service := createTestService(t, "discord://test-token@test-webhook", mockClient) |
| 379 | |
| 380 | err := service.Send("Concurrent message", nil) |
| 381 | assert.NoError(t, err) |
| 382 | |
| 383 | mockClient.AssertExpectations(t) |
| 384 | |
| 385 | done <- true |
| 386 | }(i) |
| 387 | } |
| 388 | |
| 389 | // Wait for all goroutines to complete |
| 390 | for range 3 { |
| 391 | <-done |
| 392 | } |
| 393 | }) |
| 394 | } |
| 395 | |
| 396 | func TestMemoryExhaustionSimulation(t *testing.T) { |
| 397 | t.Parallel() |
| 398 | synctest.Test(t, func(t *testing.T) { |
| 399 | mockClient := &MockHTTPClient{} |
| 400 | service := createTestService( |
| 401 | t, |
| 402 | "discord://test-token@test-webhook", |
| 403 | mockClient, |
| 404 | ) |
| 405 | |
| 406 | // Test with many large embeds |
| 407 | mockClient.On("Do", mock.Anything). |
| 408 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 409 | Once() |
| 410 | |
| 411 | items := make([]types.MessageItem, 0, 10) |
| 412 | for i := range 10 { |
| 413 | items = append(items, types.MessageItem{ |
| 414 | Text: "Embed " + string(rune(i+'0')), |
| 415 | Fields: []types.Field{ |
| 416 | {Key: "Field1", Value: strings.Repeat("Value", 100)}, |
| 417 | {Key: "Field2", Value: strings.Repeat("Value", 100)}, |
| 418 | }, |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | err := service.SendItems(items, nil) |
| 423 | |
| 424 | require.NoError(t, err) |
| 425 | |
| 426 | mockClient.AssertExpectations(t) |
| 427 | }) |
| 428 | } |
| 429 | |
| 430 | func TestRapidSuccessionRequests(t *testing.T) { |
| 431 | t.Parallel() |
| 432 | synctest.Test(t, func(t *testing.T) { |
| 433 | mockClient := &MockHTTPClient{} |
| 434 | service := createTestService( |
| 435 | t, |
| 436 | "discord://test-token@test-webhook", |
| 437 | mockClient, |
| 438 | ) |
| 439 | |
| 440 | // Test many requests in quick succession |
| 441 | for range 10 { |
| 442 | mockClient.On("Do", mock.Anything). |
| 443 | Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 444 | Once() |
| 445 | } |
| 446 | |
| 447 | for range 10 { |
| 448 | err := service.Send("Message", nil) |
| 449 | require.NoError(t, err) |
| 450 | } |
| 451 | |
| 452 | mockClient.AssertExpectations(t) |
| 453 | }) |
| 454 | } |