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