| 1 |
package discord_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"testing" |
| 6 |
"testing/synctest" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/mock" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSendWithCustomUsername(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
synctest.Test(t, func(t *testing.T) { |
| 17 |
mockClient := &MockHTTPClient{} |
| 18 |
service := createTestService( |
| 19 |
t, |
| 20 |
"discord://test-token@test-webhook", |
| 21 |
mockClient, |
| 22 |
) |
| 23 |
|
| 24 |
tests := []struct { |
| 25 |
name string |
| 26 |
username string |
| 27 |
}{ |
| 28 |
{"simple username", "TestBot"}, |
| 29 |
{"username with spaces", "Test Bot"}, |
| 30 |
{"username with special chars", "Test_Bot-123"}, |
| 31 |
{"unicode username", "テストボット"}, |
| 32 |
{"long username", "ThisIsAVeryLongUsernameThatMightCauseIssuesButShouldStillWork"}, |
| 33 |
} |
| 34 |
|
| 35 |
for _, tt := range tests { |
| 36 |
mockClient.On("Do", mock.Anything). |
| 37 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 38 |
Once() |
| 39 |
|
| 40 |
params := createTestParams("username", tt.username) |
| 41 |
err := service.Send("Message with custom username", params) |
| 42 |
|
| 43 |
require.NoError(t, err) |
| 44 |
assertRequestContains(t, mockClient, `"username":"`+tt.username+`"`) |
| 45 |
} |
| 46 |
|
| 47 |
mockClient.AssertExpectations(t) |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
func TestSendWithCustomAvatar(t *testing.T) { |
| 52 |
t.Parallel() |
| 53 |
synctest.Test(t, func(t *testing.T) { |
| 54 |
mockClient := &MockHTTPClient{} |
| 55 |
service := createTestService( |
| 56 |
t, |
| 57 |
"discord://test-token@test-webhook", |
| 58 |
mockClient, |
| 59 |
) |
| 60 |
|
| 61 |
tests := []struct { |
| 62 |
name string |
| 63 |
avatar string |
| 64 |
}{ |
| 65 |
{"http avatar", "http://example.com/avatar.png"}, |
| 66 |
{"https avatar", "https://example.com/avatar.jpg"}, |
| 67 |
{"avatar with query params", "https://example.com/avatar.png?size=128"}, |
| 68 |
{"avatar with path", "https://cdn.example.com/avatars/bot/avatar.gif"}, |
| 69 |
{ |
| 70 |
"data URL avatar", |
| 71 |
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", |
| 72 |
}, |
| 73 |
} |
| 74 |
|
| 75 |
for _, tt := range tests { |
| 76 |
mockClient.On("Do", mock.Anything). |
| 77 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 78 |
Once() |
| 79 |
|
| 80 |
params := createTestParams("avatar", tt.avatar) |
| 81 |
err := service.Send("Message with custom avatar", params) |
| 82 |
|
| 83 |
require.NoError(t, err) |
| 84 |
assertRequestContains(t, mockClient, `"avatar_url":"`+tt.avatar+`"`) |
| 85 |
} |
| 86 |
|
| 87 |
mockClient.AssertExpectations(t) |
| 88 |
}) |
| 89 |
} |
| 90 |
|
| 91 |
func TestSendWithCustomColors(t *testing.T) { |
| 92 |
t.Parallel() |
| 93 |
synctest.Test(t, func(t *testing.T) { |
| 94 |
mockClient := &MockHTTPClient{} |
| 95 |
service := createTestService( |
| 96 |
t, |
| 97 |
"discord://test-token@test-webhook", |
| 98 |
mockClient, |
| 99 |
) |
| 100 |
|
| 101 |
tests := []struct { |
| 102 |
name string |
| 103 |
color string |
| 104 |
}{ |
| 105 |
{"red color", "16711680"}, |
| 106 |
{"green color", "65280"}, |
| 107 |
{"blue color", "255"}, |
| 108 |
{"white color", "16777215"}, |
| 109 |
{"black color", "0"}, |
| 110 |
{"hex color", "ff0000"}, |
| 111 |
{"color with hash", "#ff0000"}, |
| 112 |
} |
| 113 |
|
| 114 |
for _, tt := range tests { |
| 115 |
mockClient.On("Do", mock.Anything). |
| 116 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 117 |
Once() |
| 118 |
|
| 119 |
params := createTestParams("color", tt.color) |
| 120 |
err := service.Send("Message with custom color", params) |
| 121 |
|
| 122 |
require.NoError(t, err) |
| 123 |
// Note: Color handling might be complex, just verify the request is made |
| 124 |
} |
| 125 |
|
| 126 |
mockClient.AssertExpectations(t) |
| 127 |
}) |
| 128 |
} |
| 129 |
|
| 130 |
func TestSendWithJSONMode(t *testing.T) { |
| 131 |
t.Parallel() |
| 132 |
synctest.Test(t, func(t *testing.T) { |
| 133 |
mockClient := &MockHTTPClient{} |
| 134 |
mockClient.On("Do", mock.Anything). |
| 135 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 136 |
Once() |
| 137 |
|
| 138 |
// Create service with JSON mode enabled |
| 139 |
service := createTestService( |
| 140 |
t, |
| 141 |
"discord://test-token@test-webhook?json=true", |
| 142 |
mockClient, |
| 143 |
) |
| 144 |
|
| 145 |
jsonPayload := `{"content": "Raw JSON message", "embeds": [{"title": "JSON Embed"}]}` |
| 146 |
|
| 147 |
err := service.Send(jsonPayload, nil) |
| 148 |
|
| 149 |
require.NoError(t, err) |
| 150 |
assertRequestBody(t, mockClient, jsonPayload) |
| 151 |
|
| 152 |
mockClient.AssertExpectations(t) |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
func TestSendWithSplitLines(t *testing.T) { |
| 157 |
t.Parallel() |
| 158 |
synctest.Test(t, func(t *testing.T) { |
| 159 |
tests := []struct { |
| 160 |
name string |
| 161 |
splitLines string |
| 162 |
message string |
| 163 |
expectedCalls int |
| 164 |
}{ |
| 165 |
{"split lines enabled", "true", "Line 1\nLine 2\nLine 3", 1}, |
| 166 |
{"split lines disabled", "false", "LongMessageWithoutSpaces", 1}, |
| 167 |
{"no split config", "", "LongMessageWithoutSpaces", 1}, |
| 168 |
} |
| 169 |
|
| 170 |
for _, tt := range tests { |
| 171 |
mockClient := &MockHTTPClient{} |
| 172 |
service := createTestService( |
| 173 |
t, |
| 174 |
"discord://test-token@test-webhook", |
| 175 |
mockClient, |
| 176 |
) |
| 177 |
|
| 178 |
mockClient.On("Do", mock.Anything). |
| 179 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 180 |
Times(tt.expectedCalls) |
| 181 |
|
| 182 |
var params *types.Params |
| 183 |
if tt.splitLines != "" { |
| 184 |
params = createTestParams("splitLines", tt.splitLines) |
| 185 |
} |
| 186 |
|
| 187 |
err := service.Send(tt.message, params) |
| 188 |
|
| 189 |
require.NoError(t, err) |
| 190 |
mockClient.AssertNumberOfCalls(t, "Do", tt.expectedCalls) |
| 191 |
mockClient.AssertExpectations(t) |
| 192 |
} |
| 193 |
}) |
| 194 |
} |
| 195 |
|
| 196 |
func TestSendWithTitle(t *testing.T) { |
| 197 |
t.Parallel() |
| 198 |
synctest.Test(t, func(t *testing.T) { |
| 199 |
mockClient := &MockHTTPClient{} |
| 200 |
service := createTestService( |
| 201 |
t, |
| 202 |
"discord://test-token@test-webhook", |
| 203 |
mockClient, |
| 204 |
) |
| 205 |
|
| 206 |
mockClient.On("Do", mock.Anything). |
| 207 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 208 |
Once() |
| 209 |
|
| 210 |
params := createTestParams("title", "Custom Title") |
| 211 |
err := service.Send("Message with custom title", params) |
| 212 |
|
| 213 |
require.NoError(t, err) |
| 214 |
// Title handling depends on embed creation, verify request is made |
| 215 |
|
| 216 |
mockClient.AssertExpectations(t) |
| 217 |
}) |
| 218 |
} |
| 219 |
|
| 220 |
func TestSendWithComplexParameterCombination(t *testing.T) { |
| 221 |
t.Parallel() |
| 222 |
synctest.Test(t, func(t *testing.T) { |
| 223 |
mockClient := &MockHTTPClient{} |
| 224 |
service := createTestService( |
| 225 |
t, |
| 226 |
"discord://test-token@test-webhook", |
| 227 |
mockClient, |
| 228 |
) |
| 229 |
|
| 230 |
mockClient.On("Do", mock.Anything). |
| 231 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 232 |
Once() |
| 233 |
|
| 234 |
params := createTestParams( |
| 235 |
"username", "ComplexBot", |
| 236 |
"avatar", "https://example.com/complex-avatar.png", |
| 237 |
"color", "16776960", |
| 238 |
"title", "Complex Test", |
| 239 |
"splitLines", "false", |
| 240 |
) |
| 241 |
|
| 242 |
err := service.Send("Complex parameter combination test", params) |
| 243 |
|
| 244 |
require.NoError(t, err) |
| 245 |
assertRequestContains(t, mockClient, `"username":"ComplexBot"`) |
| 246 |
assertRequestContains( |
| 247 |
t, |
| 248 |
mockClient, |
| 249 |
`"avatar_url":"https://example.com/complex-avatar.png"`, |
| 250 |
) |
| 251 |
|
| 252 |
mockClient.AssertExpectations(t) |
| 253 |
}) |
| 254 |
} |
| 255 |
|
| 256 |
func TestSendWithEmptyParameters(t *testing.T) { |
| 257 |
t.Parallel() |
| 258 |
synctest.Test(t, func(t *testing.T) { |
| 259 |
mockClient := &MockHTTPClient{} |
| 260 |
service := createTestService( |
| 261 |
t, |
| 262 |
"discord://test-token@test-webhook", |
| 263 |
mockClient, |
| 264 |
) |
| 265 |
|
| 266 |
mockClient.On("Do", mock.Anything). |
| 267 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 268 |
Once() |
| 269 |
|
| 270 |
emptyParams := &types.Params{} |
| 271 |
err := service.Send("Message with empty params", emptyParams) |
| 272 |
|
| 273 |
require.NoError(t, err) |
| 274 |
assertRequestContains(t, mockClient, `"content":"Message with empty params"`) |
| 275 |
|
| 276 |
mockClient.AssertExpectations(t) |
| 277 |
}) |
| 278 |
} |
| 279 |
|
| 280 |
func TestSendWithNilParameters(t *testing.T) { |
| 281 |
t.Parallel() |
| 282 |
synctest.Test(t, func(t *testing.T) { |
| 283 |
mockClient := &MockHTTPClient{} |
| 284 |
service := createTestService( |
| 285 |
t, |
| 286 |
"discord://test-token@test-webhook", |
| 287 |
mockClient, |
| 288 |
) |
| 289 |
|
| 290 |
mockClient.On("Do", mock.Anything). |
| 291 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 292 |
Once() |
| 293 |
|
| 294 |
err := service.Send("Message with nil params", nil) |
| 295 |
|
| 296 |
require.NoError(t, err) |
| 297 |
assertRequestContains(t, mockClient, `"content":"Message with nil params"`) |
| 298 |
|
| 299 |
mockClient.AssertExpectations(t) |
| 300 |
}) |
| 301 |
} |
| 302 |
|
| 303 |
func TestSendWithURLParameters(t *testing.T) { |
| 304 |
t.Parallel() |
| 305 |
synctest.Test(t, func(t *testing.T) { |
| 306 |
mockClient := &MockHTTPClient{} |
| 307 |
mockClient.On("Do", mock.Anything). |
| 308 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 309 |
Once() |
| 310 |
|
| 311 |
// Create service with URL parameters |
| 312 |
service := createTestService( |
| 313 |
t, |
| 314 |
"discord://test-token@test-webhook?username=URLBot&avatar=https://example.com/url-avatar.png", |
| 315 |
mockClient, |
| 316 |
) |
| 317 |
|
| 318 |
err := service.Send("Message with URL params", nil) |
| 319 |
|
| 320 |
require.NoError(t, err) |
| 321 |
assertRequestContains(t, mockClient, `"username":"URLBot"`) |
| 322 |
assertRequestContains( |
| 323 |
t, |
| 324 |
mockClient, |
| 325 |
`"avatar_url":"https://example.com/url-avatar.png"`, |
| 326 |
) |
| 327 |
|
| 328 |
mockClient.AssertExpectations(t) |
| 329 |
}) |
| 330 |
} |