Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/notifier/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type postOption func(*postOptions)

func postMessage(ctx context.Context, address string, payload any, opts ...postOption) error {
options := &postOptions{
// Default validateResponse function verifies that the response status code is 200, 202 or 201.
// Default validateResponse accepts any 2xx status (including 204 No Content).
responseValidator: func(resp *http.Response) error {
s := resp.StatusCode
if 200 <= s && s < 300 {
Expand Down
33 changes: 33 additions & 0 deletions internal/notifier/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ func Test_postMessage(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
}

func Test_postMessage_accepts2xxStatusCodes(t *testing.T) {
for _, code := range []int{
http.StatusOK,
http.StatusCreated,
http.StatusAccepted,
http.StatusNoContent,
} {
t.Run(http.StatusText(code), func(t *testing.T) {
g := NewWithT(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(code)
}))
defer ts.Close()

err := postMessage(context.Background(), ts.URL, map[string]string{"status": "success"})
g.Expect(err).ToNot(HaveOccurred())
})
}

t.Run(http.StatusText(http.StatusBadRequest), func(t *testing.T) {
g := NewWithT(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("nope"))
}))
defer ts.Close()

err := postMessage(context.Background(), ts.URL, map[string]string{"status": "success"})
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("request failed with status code 400"))
})
}

func Test_postMessage_timeout(t *testing.T) {
g := NewWithT(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down