| 1 |
package ntfy_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"net/http" |
| 6 |
"testing" |
| 7 |
"testing/synctest" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/internal/meta" |
| 12 |
) |
| 13 |
|
| 14 |
func TestAPIURLFormat(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
synctest.Test(t, func(t *testing.T) { |
| 17 |
service, mockClient := createTestServiceWithMock( |
| 18 |
t, |
| 19 |
"ntfy://ntfy.example.com/mytopic", |
| 20 |
) |
| 21 |
|
| 22 |
err := service.Send("test", nil) |
| 23 |
require.NoError(t, err) |
| 24 |
|
| 25 |
assertRequestMade(t, mockClient, "https://ntfy.example.com/mytopic") |
| 26 |
|
| 27 |
mockClient.AssertExpectations(t) |
| 28 |
}) |
| 29 |
} |
| 30 |
|
| 31 |
func TestAPIURLFormatWithNestedTopic(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
synctest.Test(t, func(t *testing.T) { |
| 34 |
service, mockClient := createTestServiceWithMock( |
| 35 |
t, |
| 36 |
"ntfy://ntfy.example.com/alerts/critical", |
| 37 |
) |
| 38 |
|
| 39 |
err := service.Send("test", nil) |
| 40 |
require.NoError(t, err) |
| 41 |
|
| 42 |
assertRequestMade(t, mockClient, "https://ntfy.example.com/alerts/critical") |
| 43 |
|
| 44 |
mockClient.AssertExpectations(t) |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
func TestHTTPMethodIsPost(t *testing.T) { |
| 49 |
t.Parallel() |
| 50 |
synctest.Test(t, func(t *testing.T) { |
| 51 |
service, mockClient := createTestServiceWithMock( |
| 52 |
t, |
| 53 |
validNtfyURL, |
| 54 |
) |
| 55 |
|
| 56 |
err := service.Send("test", nil) |
| 57 |
require.NoError(t, err) |
| 58 |
|
| 59 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 60 |
return req.Method == http.MethodPost |
| 61 |
}, "POST method") |
| 62 |
|
| 63 |
mockClient.AssertExpectations(t) |
| 64 |
}) |
| 65 |
} |
| 66 |
|
| 67 |
func TestContentTypeHeader(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
synctest.Test(t, func(t *testing.T) { |
| 70 |
service, mockClient := createTestServiceWithMock( |
| 71 |
t, |
| 72 |
validNtfyURL, |
| 73 |
) |
| 74 |
|
| 75 |
err := service.Send("test", nil) |
| 76 |
require.NoError(t, err) |
| 77 |
|
| 78 |
assertRequestHeaderContains(t, mockClient, "Content-Type", "text/plain; charset=utf-8") |
| 79 |
|
| 80 |
mockClient.AssertExpectations(t) |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
func TestContentTypeHeaderMarkdown(t *testing.T) { |
| 85 |
t.Parallel() |
| 86 |
synctest.Test(t, func(t *testing.T) { |
| 87 |
service, mockClient := createTestServiceWithMock( |
| 88 |
t, |
| 89 |
validNtfyURL+"?markdown=yes", |
| 90 |
) |
| 91 |
|
| 92 |
err := service.Send("test", nil) |
| 93 |
require.NoError(t, err) |
| 94 |
|
| 95 |
assertRequestHeaderContains(t, mockClient, "Content-Type", "text/markdown") |
| 96 |
|
| 97 |
mockClient.AssertExpectations(t) |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
func TestUserAgentHeader(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
synctest.Test(t, func(t *testing.T) { |
| 104 |
service, mockClient := createTestServiceWithMock( |
| 105 |
t, |
| 106 |
validNtfyURL, |
| 107 |
) |
| 108 |
|
| 109 |
err := service.Send("test", nil) |
| 110 |
require.NoError(t, err) |
| 111 |
|
| 112 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 113 |
userAgent := req.Header.Get("User-Agent") |
| 114 |
|
| 115 |
return userAgent == "shoutrrr/"+meta.Version |
| 116 |
}, "User-Agent header") |
| 117 |
|
| 118 |
mockClient.AssertExpectations(t) |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
func TestBasicAuthHeader(t *testing.T) { |
| 123 |
t.Parallel() |
| 124 |
synctest.Test(t, func(t *testing.T) { |
| 125 |
service, mockClient := createTestServiceWithMock( |
| 126 |
t, |
| 127 |
"ntfy://myuser:[email protected]/mytopic", |
| 128 |
) |
| 129 |
|
| 130 |
err := service.Send("test", nil) |
| 131 |
require.NoError(t, err) |
| 132 |
|
| 133 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 134 |
authHeader := req.Header.Get("Authorization") |
| 135 |
expected := "Basic " + base64.StdEncoding.EncodeToString([]byte("myuser:mypass")) |
| 136 |
|
| 137 |
return authHeader == expected |
| 138 |
}, "Basic Auth header") |
| 139 |
|
| 140 |
mockClient.AssertExpectations(t) |
| 141 |
}) |
| 142 |
} |
| 143 |
|
| 144 |
func TestBasicAuthHeaderUsernameOnly(t *testing.T) { |
| 145 |
t.Parallel() |
| 146 |
synctest.Test(t, func(t *testing.T) { |
| 147 |
service, mockClient := createTestServiceWithMock( |
| 148 |
t, |
| 149 |
"ntfy://[email protected]/mytopic", |
| 150 |
) |
| 151 |
|
| 152 |
err := service.Send("test", nil) |
| 153 |
require.NoError(t, err) |
| 154 |
|
| 155 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 156 |
authHeader := req.Header.Get("Authorization") |
| 157 |
expected := "Basic " + base64.StdEncoding.EncodeToString([]byte("myuser:")) |
| 158 |
|
| 159 |
return authHeader == expected |
| 160 |
}, "Basic Auth header with username only") |
| 161 |
|
| 162 |
mockClient.AssertExpectations(t) |
| 163 |
}) |
| 164 |
} |
| 165 |
|
| 166 |
func TestDisableTLSUsesHTTP(t *testing.T) { |
| 167 |
t.Parallel() |
| 168 |
synctest.Test(t, func(t *testing.T) { |
| 169 |
service, mockClient := createTestServiceWithMock( |
| 170 |
t, |
| 171 |
"ntfy://ntfy.example.com/mytopic?disabletls=yes", |
| 172 |
) |
| 173 |
|
| 174 |
err := service.Send("test", nil) |
| 175 |
require.NoError(t, err) |
| 176 |
|
| 177 |
assertRequestMade(t, mockClient, "http://ntfy.example.com/mytopic") |
| 178 |
|
| 179 |
mockClient.AssertExpectations(t) |
| 180 |
}) |
| 181 |
} |
| 182 |
|
| 183 |
func TestRequestBodyIsPlainText(t *testing.T) { |
| 184 |
t.Parallel() |
| 185 |
synctest.Test(t, func(t *testing.T) { |
| 186 |
service, mockClient := createTestServiceWithMock( |
| 187 |
t, |
| 188 |
validNtfyURL, |
| 189 |
) |
| 190 |
|
| 191 |
err := service.Send("plain text body", nil) |
| 192 |
require.NoError(t, err) |
| 193 |
|
| 194 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 195 |
contentType := req.Header.Get("Content-Type") |
| 196 |
|
| 197 |
return contentType == "text/plain; charset=utf-8" |
| 198 |
}, "plain text Content-Type") |
| 199 |
|
| 200 |
mockClient.AssertExpectations(t) |
| 201 |
}) |
| 202 |
} |
| 203 |
|
| 204 |
func TestRequestBodyContainsExactMessage(t *testing.T) { |
| 205 |
t.Parallel() |
| 206 |
synctest.Test(t, func(t *testing.T) { |
| 207 |
service, mockClient := createTestServiceWithMock( |
| 208 |
t, |
| 209 |
validNtfyURL, |
| 210 |
) |
| 211 |
|
| 212 |
message := "Exact message body" |
| 213 |
err := service.Send(message, nil) |
| 214 |
require.NoError(t, err) |
| 215 |
|
| 216 |
assertRequestContains(t, mockClient, message) |
| 217 |
|
| 218 |
mockClient.AssertExpectations(t) |
| 219 |
}) |
| 220 |
} |