| 1 |
package discord |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/onsi/ginkgo/v2" |
| 11 |
"github.com/onsi/gomega" |
| 12 |
"github.com/stretchr/testify/mock" |
| 13 |
|
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 15 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/discord/mocks" |
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 |
mockTypes "github.com/nicholas-fedor/shoutrrr/pkg/types/mocks" |
| 18 |
) |
| 19 |
|
| 20 |
const testWebhookURLAlt = "https://discord.com/api/webhooks/123/abc" |
| 21 |
|
| 22 |
var _ = ginkgo.Describe("Discord Service Unit Tests", func() { |
| 23 |
ginkgo.Describe("Service.Send method", func() { |
| 24 |
var service *Service |
| 25 |
|
| 26 |
ginkgo.BeforeEach(func() { |
| 27 |
service = &Service{ |
| 28 |
Config: &Config{ |
| 29 |
WebhookID: "123456789", |
| 30 |
Token: "test-token", |
| 31 |
}, |
| 32 |
} |
| 33 |
service.SetLogger(&mockTypes.MockStdLogger{}) |
| 34 |
}) |
| 35 |
|
| 36 |
ginkgo.It("should send plain text message successfully", func() { |
| 37 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 38 |
service.HTTPClient = mockClient |
| 39 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 40 |
StatusCode: http.StatusNoContent, |
| 41 |
Body: io.NopCloser(strings.NewReader("")), |
| 42 |
}, nil) |
| 43 |
|
| 44 |
message := "Hello, Discord!" |
| 45 |
params := &types.Params{} |
| 46 |
|
| 47 |
err := service.Send(message, params) |
| 48 |
|
| 49 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 50 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 51 |
}) |
| 52 |
|
| 53 |
ginkgo.It("should send JSON message in JSON mode", func() { |
| 54 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 55 |
service.HTTPClient = mockClient |
| 56 |
service.Config.JSON = true |
| 57 |
|
| 58 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 59 |
StatusCode: http.StatusNoContent, |
| 60 |
Body: io.NopCloser(strings.NewReader("")), |
| 61 |
}, nil) |
| 62 |
|
| 63 |
jsonMessage := `{"content":"JSON message"}` |
| 64 |
params := &types.Params{} |
| 65 |
|
| 66 |
err := service.Send(jsonMessage, params) |
| 67 |
|
| 68 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 69 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 70 |
}) |
| 71 |
|
| 72 |
ginkgo.It("should handle empty message", func() { |
| 73 |
message := "" |
| 74 |
params := &types.Params{} |
| 75 |
|
| 76 |
err := service.Send(message, params) |
| 77 |
|
| 78 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 79 |
gomega.Expect(err).To(gomega.MatchError(ErrEmptyMessage)) |
| 80 |
}) |
| 81 |
|
| 82 |
ginkgo.It("should handle large message with chunking", func() { |
| 83 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 84 |
service.HTTPClient = mockClient |
| 85 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 86 |
StatusCode: http.StatusNoContent, |
| 87 |
Body: io.NopCloser(strings.NewReader("")), |
| 88 |
}, nil) |
| 89 |
|
| 90 |
// Create a message larger than TotalChunkSize to force multiple batches |
| 91 |
largeMessage := strings.Repeat("a", TotalChunkSize+100) |
| 92 |
params := &types.Params{} |
| 93 |
|
| 94 |
err := service.Send(largeMessage, params) |
| 95 |
|
| 96 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 97 |
// Should make multiple calls for chunks |
| 98 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 2) |
| 99 |
}) |
| 100 |
|
| 101 |
ginkgo.It("should handle split lines option", func() { |
| 102 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 103 |
service.HTTPClient = mockClient |
| 104 |
service.Config.SplitLines = true |
| 105 |
|
| 106 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 107 |
StatusCode: http.StatusNoContent, |
| 108 |
Body: io.NopCloser(strings.NewReader("")), |
| 109 |
}, nil) |
| 110 |
|
| 111 |
message := "Line 1\nLine 2\nLine 3" |
| 112 |
params := &types.Params{} |
| 113 |
|
| 114 |
err := service.Send(message, params) |
| 115 |
|
| 116 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 117 |
// One call for the batch of lines |
| 118 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 119 |
}) |
| 120 |
|
| 121 |
ginkgo.It("should return error when first batch fails", func() { |
| 122 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 123 |
mockLogger := &mockTypes.MockStdLogger{} |
| 124 |
service.HTTPClient = mockClient |
| 125 |
service.SetLogger(mockLogger) |
| 126 |
mockClient.On("Do", mock.Anything).Return(nil, ErrMaxRetries) |
| 127 |
mockLogger.On("Print", mock.Anything).Return() |
| 128 |
|
| 129 |
message := "Test message" |
| 130 |
params := &types.Params{} |
| 131 |
|
| 132 |
err := service.Send(message, params) |
| 133 |
|
| 134 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 135 |
gomega.Expect(err.Error()). |
| 136 |
To(gomega.ContainSubstring("failed to send discord notification")) |
| 137 |
}) |
| 138 |
}) |
| 139 |
|
| 140 |
ginkgo.Describe("Service.SendItems method", func() { |
| 141 |
ginkgo.It("should delegate to sendItems method", func() { |
| 142 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 143 |
service := &Service{ |
| 144 |
Config: &Config{ |
| 145 |
WebhookID: "123456789", |
| 146 |
Token: "test-token", |
| 147 |
}, |
| 148 |
HTTPClient: mockClient, |
| 149 |
} |
| 150 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 151 |
StatusCode: http.StatusNoContent, |
| 152 |
Body: io.NopCloser(strings.NewReader("")), |
| 153 |
}, nil) |
| 154 |
|
| 155 |
items := []types.MessageItem{ |
| 156 |
{Text: "Test message"}, |
| 157 |
} |
| 158 |
params := &types.Params{} |
| 159 |
|
| 160 |
err := service.SendItems(items, params) |
| 161 |
|
| 162 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 163 |
}) |
| 164 |
}) |
| 165 |
|
| 166 |
ginkgo.Describe("Service.sendItems method", func() { |
| 167 |
var service *Service |
| 168 |
|
| 169 |
ginkgo.BeforeEach(func() { |
| 170 |
service = &Service{ |
| 171 |
Config: &Config{ |
| 172 |
WebhookID: "123456789", |
| 173 |
Token: "test-token", |
| 174 |
}, |
| 175 |
pkr: format.NewPropKeyResolver(&Config{}), |
| 176 |
} |
| 177 |
}) |
| 178 |
|
| 179 |
ginkgo.It("should send simple message item successfully", func() { |
| 180 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 181 |
service.HTTPClient = mockClient |
| 182 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 183 |
StatusCode: http.StatusNoContent, |
| 184 |
Body: io.NopCloser(strings.NewReader("")), |
| 185 |
}, nil) |
| 186 |
|
| 187 |
items := []types.MessageItem{ |
| 188 |
{Text: "Simple message"}, |
| 189 |
} |
| 190 |
params := &types.Params{} |
| 191 |
|
| 192 |
err := service.sendItems(items, params) |
| 193 |
|
| 194 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 195 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 196 |
}) |
| 197 |
|
| 198 |
ginkgo.It("should handle message with file attachment", func() { |
| 199 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 200 |
service.HTTPClient = mockClient |
| 201 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 202 |
StatusCode: http.StatusNoContent, |
| 203 |
Body: io.NopCloser(strings.NewReader("")), |
| 204 |
}, nil) |
| 205 |
|
| 206 |
items := []types.MessageItem{ |
| 207 |
{ |
| 208 |
Text: "Message with file", |
| 209 |
File: &types.File{ |
| 210 |
Name: "test.txt", |
| 211 |
Data: []byte("file content"), |
| 212 |
}, |
| 213 |
}, |
| 214 |
} |
| 215 |
params := &types.Params{} |
| 216 |
|
| 217 |
err := service.sendItems(items, params) |
| 218 |
|
| 219 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 220 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 221 |
}) |
| 222 |
|
| 223 |
ginkgo.It("should update config from params", func() { |
| 224 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 225 |
service.HTTPClient = mockClient |
| 226 |
service.pkr = format.NewPropKeyResolver(service.Config) |
| 227 |
|
| 228 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 229 |
StatusCode: http.StatusNoContent, |
| 230 |
Body: io.NopCloser(strings.NewReader("")), |
| 231 |
}, nil) |
| 232 |
|
| 233 |
items := []types.MessageItem{ |
| 234 |
{Text: "Test message"}, |
| 235 |
} |
| 236 |
params := &types.Params{ |
| 237 |
"username": "CustomBot", |
| 238 |
} |
| 239 |
|
| 240 |
err := service.sendItems(items, params) |
| 241 |
|
| 242 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 243 |
// The payload should include the updated username |
| 244 |
}) |
| 245 |
|
| 246 |
ginkgo.It("should handle empty items", func() { |
| 247 |
items := []types.MessageItem{} |
| 248 |
params := &types.Params{} |
| 249 |
|
| 250 |
err := service.sendItems(items, params) |
| 251 |
|
| 252 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 253 |
gomega.Expect(err).To(gomega.MatchError(ErrEmptyMessage)) |
| 254 |
}) |
| 255 |
|
| 256 |
ginkgo.It("should handle config update error", func() { |
| 257 |
service.pkr = format.NewPropKeyResolver(service.Config) |
| 258 |
|
| 259 |
// Use invalid params to trigger an error |
| 260 |
items := []types.MessageItem{ |
| 261 |
{Text: "Test message"}, |
| 262 |
} |
| 263 |
params := &types.Params{ |
| 264 |
"invalid_key": "value", |
| 265 |
} |
| 266 |
|
| 267 |
err := service.sendItems(items, params) |
| 268 |
|
| 269 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 270 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("updating config from params")) |
| 271 |
}) |
| 272 |
}) |
| 273 |
|
| 274 |
ginkgo.Describe("CreateItemsFromPlain function", func() { |
| 275 |
ginkgo.It("should create single batch for small message without split lines", func() { |
| 276 |
plain := "Short message" |
| 277 |
splitLines := false |
| 278 |
|
| 279 |
result := CreateItemsFromPlain(plain, splitLines) |
| 280 |
|
| 281 |
gomega.Expect(result).To(gomega.HaveLen(1)) |
| 282 |
gomega.Expect(result[0]).To(gomega.HaveLen(1)) |
| 283 |
gomega.Expect(result[0][0].Text).To(gomega.Equal(plain)) |
| 284 |
}) |
| 285 |
|
| 286 |
ginkgo.It("should split lines when splitLines is true", func() { |
| 287 |
plain := "Line 1\nLine 2\nLine 3" |
| 288 |
splitLines := true |
| 289 |
|
| 290 |
result := CreateItemsFromPlain(plain, splitLines) |
| 291 |
|
| 292 |
gomega.Expect(result).To(gomega.HaveLen(1)) // All lines fit in one batch |
| 293 |
gomega.Expect(result[0]).To(gomega.HaveLen(3)) |
| 294 |
gomega.Expect(result[0][0].Text).To(gomega.Equal("Line 1")) |
| 295 |
gomega.Expect(result[0][1].Text).To(gomega.Equal("Line 2")) |
| 296 |
gomega.Expect(result[0][2].Text).To(gomega.Equal("Line 3")) |
| 297 |
}) |
| 298 |
|
| 299 |
ginkgo.It("should chunk large message without split lines", func() { |
| 300 |
largeMessage := strings.Repeat("a", ChunkSize+100) |
| 301 |
splitLines := false |
| 302 |
|
| 303 |
result := CreateItemsFromPlain(largeMessage, splitLines) |
| 304 |
|
| 305 |
gomega.Expect(result).To(gomega.HaveLen(1)) // All chunks fit in one batch |
| 306 |
gomega.Expect(result[0]).To(gomega.HaveLen(2)) // 2 chunks: 2000 and 100 |
| 307 |
|
| 308 |
totalLength := 0 |
| 309 |
for _, item := range result[0] { |
| 310 |
totalLength += len(item.Text) |
| 311 |
} |
| 312 |
|
| 313 |
gomega.Expect(totalLength).To(gomega.Equal(len(largeMessage))) |
| 314 |
}) |
| 315 |
|
| 316 |
ginkgo.It("should handle empty string", func() { |
| 317 |
plain := "" |
| 318 |
splitLines := false |
| 319 |
|
| 320 |
result := CreateItemsFromPlain(plain, splitLines) |
| 321 |
|
| 322 |
gomega.Expect(result).To(gomega.HaveLen(1)) |
| 323 |
gomega.Expect(result[0]).To(gomega.BeEmpty()) // Empty input returns empty items |
| 324 |
}) |
| 325 |
|
| 326 |
ginkgo.It("should handle message exactly at chunk size", func() { |
| 327 |
exactMessage := strings.Repeat("a", ChunkSize) |
| 328 |
splitLines := false |
| 329 |
|
| 330 |
result := CreateItemsFromPlain(exactMessage, splitLines) |
| 331 |
|
| 332 |
gomega.Expect(result).To(gomega.HaveLen(1)) |
| 333 |
gomega.Expect(result[0][0].Text).To(gomega.Equal(exactMessage)) |
| 334 |
}) |
| 335 |
}) |
| 336 |
|
| 337 |
ginkgo.Describe("Service.Initialize method", func() { |
| 338 |
var service *Service |
| 339 |
|
| 340 |
ginkgo.BeforeEach(func() { |
| 341 |
service = &Service{} |
| 342 |
}) |
| 343 |
|
| 344 |
ginkgo.It("should initialize service successfully with valid URL", func() { |
| 345 |
serviceURL, _ := url.Parse("discord://token@webhook?username=TestBot") |
| 346 |
logger := &mockTypes.MockStdLogger{} |
| 347 |
|
| 348 |
err := service.Initialize(serviceURL, logger) |
| 349 |
|
| 350 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 351 |
gomega.Expect(service.Config).NotTo(gomega.BeNil()) |
| 352 |
gomega.Expect(service.Config.WebhookID).To(gomega.Equal("webhook")) |
| 353 |
gomega.Expect(service.Config.Token).To(gomega.Equal("token")) |
| 354 |
gomega.Expect(service.Config.Username).To(gomega.Equal("TestBot")) |
| 355 |
gomega.Expect(service.HTTPClient).NotTo(gomega.BeNil()) |
| 356 |
}) |
| 357 |
|
| 358 |
ginkgo.It("should handle URL parsing error", func() { |
| 359 |
// Simulate config.SetURL error by using invalid URL |
| 360 |
invalidURL, _ := url.Parse("discord://@") // Missing token |
| 361 |
logger := &mockTypes.MockStdLogger{} |
| 362 |
|
| 363 |
err := service.Initialize(invalidURL, logger) |
| 364 |
|
| 365 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 366 |
}) |
| 367 |
|
| 368 |
ginkgo.It("should set default properties", func() { |
| 369 |
serviceURL, _ := url.Parse("discord://token@webhook") |
| 370 |
logger := &mockTypes.MockStdLogger{} |
| 371 |
|
| 372 |
err := service.Initialize(serviceURL, logger) |
| 373 |
|
| 374 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 375 |
// Verify default properties are set |
| 376 |
gomega.Expect(service.pkr).NotTo(gomega.BeNil()) |
| 377 |
}) |
| 378 |
}) |
| 379 |
|
| 380 |
ginkgo.Describe("Service.GetID method", func() { |
| 381 |
var service *Service |
| 382 |
|
| 383 |
ginkgo.BeforeEach(func() { |
| 384 |
service = &Service{} |
| 385 |
}) |
| 386 |
|
| 387 |
ginkgo.It("should return discord scheme", func() { |
| 388 |
id := service.GetID() |
| 389 |
|
| 390 |
gomega.Expect(id).To(gomega.Equal(Scheme)) |
| 391 |
gomega.Expect(id).To(gomega.Equal("discord")) |
| 392 |
}) |
| 393 |
}) |
| 394 |
|
| 395 |
ginkgo.Describe("Service.doSend method", func() { |
| 396 |
var service *Service |
| 397 |
|
| 398 |
ginkgo.BeforeEach(func() { |
| 399 |
service = &Service{} |
| 400 |
}) |
| 401 |
|
| 402 |
ginkgo.It("should send payload successfully", func() { |
| 403 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 404 |
service.HTTPClient = mockClient |
| 405 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 406 |
StatusCode: http.StatusNoContent, |
| 407 |
Body: io.NopCloser(strings.NewReader("")), |
| 408 |
}, nil) |
| 409 |
|
| 410 |
payload := []byte(`{"content":"test"}`) |
| 411 |
postURL := testWebhookURLAlt |
| 412 |
|
| 413 |
err := service.doSend(payload, postURL) |
| 414 |
|
| 415 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 416 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 417 |
}) |
| 418 |
|
| 419 |
ginkgo.It("should handle empty URL", func() { |
| 420 |
payload := []byte(`{"content":"test"}`) |
| 421 |
postURL := "" |
| 422 |
|
| 423 |
err := service.doSend(payload, postURL) |
| 424 |
|
| 425 |
gomega.Expect(err).To(gomega.MatchError(ErrEmptyURL)) |
| 426 |
}) |
| 427 |
|
| 428 |
ginkgo.It("should validate URL scheme", func() { |
| 429 |
payload := []byte(`{"content":"test"}`) |
| 430 |
postURL := "http://" + testWebhookURLAlt[8:] // Change https to http |
| 431 |
|
| 432 |
err := service.doSend(payload, postURL) |
| 433 |
|
| 434 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidScheme)) |
| 435 |
}) |
| 436 |
|
| 437 |
ginkgo.It("should validate host", func() { |
| 438 |
payload := []byte(`{"content":"test"}`) |
| 439 |
postURL := "https://example.com" + testWebhookURLAlt[20:] // Replace discord.com with example.com |
| 440 |
|
| 441 |
err := service.doSend(payload, postURL) |
| 442 |
|
| 443 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidHost)) |
| 444 |
}) |
| 445 |
|
| 446 |
ginkgo.It("should validate URL prefix", func() { |
| 447 |
payload := []byte(`{"content":"test"}`) |
| 448 |
postURL := "https://discord.com/api/invalid" + testWebhookURLAlt[28:] // Replace /webhooks with /invalid |
| 449 |
|
| 450 |
err := service.doSend(payload, postURL) |
| 451 |
|
| 452 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidURLPrefix)) |
| 453 |
}) |
| 454 |
|
| 455 |
ginkgo.It("should validate webhook ID and token", func() { |
| 456 |
payload := []byte(`{"content":"test"}`) |
| 457 |
postURL := "https://discord.com/api/webhooks//" |
| 458 |
|
| 459 |
err := service.doSend(payload, postURL) |
| 460 |
|
| 461 |
gomega.Expect(err).To(gomega.MatchError(ErrMalformedURL)) |
| 462 |
}) |
| 463 |
|
| 464 |
ginkgo.It("should handle HTTP client error", func() { |
| 465 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 466 |
service.HTTPClient = mockClient |
| 467 |
mockClient.On("Do", mock.Anything).Return(nil, errors.New("network error")) |
| 468 |
|
| 469 |
payload := []byte(`{"content":"test"}`) |
| 470 |
postURL := testWebhookURLAlt |
| 471 |
|
| 472 |
err := service.doSend(payload, postURL) |
| 473 |
|
| 474 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 475 |
}) |
| 476 |
}) |
| 477 |
|
| 478 |
ginkgo.Describe("Service.doSendMultipart method", func() { |
| 479 |
var service *Service |
| 480 |
|
| 481 |
ginkgo.BeforeEach(func() { |
| 482 |
service = &Service{} |
| 483 |
}) |
| 484 |
|
| 485 |
ginkgo.It("should send multipart payload with files successfully", func() { |
| 486 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 487 |
service.HTTPClient = mockClient |
| 488 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 489 |
StatusCode: http.StatusNoContent, |
| 490 |
Body: io.NopCloser(strings.NewReader("")), |
| 491 |
}, nil) |
| 492 |
|
| 493 |
payload := WebhookPayload{ |
| 494 |
Content: "Message with files", |
| 495 |
} |
| 496 |
files := []types.File{ |
| 497 |
{Name: "test.txt", Data: []byte("file content")}, |
| 498 |
} |
| 499 |
postURL := testWebhookURLAlt |
| 500 |
|
| 501 |
err := service.doSendMultipart(&payload, files, postURL) |
| 502 |
|
| 503 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 504 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 505 |
}) |
| 506 |
|
| 507 |
ginkgo.It("should handle empty files array", func() { |
| 508 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 509 |
service.HTTPClient = mockClient |
| 510 |
mockClient.On("Do", mock.Anything).Return(&http.Response{ |
| 511 |
StatusCode: http.StatusNoContent, |
| 512 |
Body: io.NopCloser(strings.NewReader("")), |
| 513 |
}, nil) |
| 514 |
|
| 515 |
payload := WebhookPayload{ |
| 516 |
Content: "Message without files", |
| 517 |
} |
| 518 |
files := []types.File{} |
| 519 |
postURL := testWebhookURLAlt |
| 520 |
|
| 521 |
err := service.doSendMultipart(&payload, files, postURL) |
| 522 |
|
| 523 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 524 |
mockClient.AssertNumberOfCalls(ginkgo.GinkgoT(), "Do", 1) |
| 525 |
}) |
| 526 |
|
| 527 |
ginkgo.It("should handle HTTP client error", func() { |
| 528 |
mockClient := mocks.NewMockHTTPClient(ginkgo.GinkgoT()) |
| 529 |
service.HTTPClient = mockClient |
| 530 |
mockClient.On("Do", mock.Anything).Return(nil, errors.New("network error")) |
| 531 |
|
| 532 |
payload := WebhookPayload{ |
| 533 |
Content: "Test message", |
| 534 |
} |
| 535 |
files := []types.File{} |
| 536 |
postURL := testWebhookURLAlt |
| 537 |
|
| 538 |
err := service.doSendMultipart(&payload, files, postURL) |
| 539 |
|
| 540 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 541 |
}) |
| 542 |
}) |
| 543 |
}) |