package apprise import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "testing" ) func TestConfigured(t *testing.T) { cases := []struct { name string c *Client want bool }{ {"nil", nil, false}, {"no base", New("", "key", nil, "", 0), false}, {"base no destination", New("http://x", "", nil, "", 0), false}, {"key mode", New("http://x", "key", nil, "", 0), true}, {"stateless mode", New("http://x", "", []string{"mailto:a@b"}, "", 0), true}, } for _, tc := range cases { if got := tc.c.Configured(); got != tc.want { t.Errorf("%s: Configured() = %v, want %v", tc.name, got, tc.want) } } } func TestSendKeyMode(t *testing.T) { var gotPath string var gotBody map[string]any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path raw, _ := io.ReadAll(r.Body) _ = json.Unmarshal(raw, &gotBody) w.WriteHeader(http.StatusOK) })) defer srv.Close() c := New(srv.URL, "sam_email", nil, "", 0) if err := c.Send(context.Background(), "title", "body"); err != nil { t.Fatalf("Send: %v", err) } if gotPath != "/notify/sam_email" { t.Errorf("path = %q, want /notify/sam_email", gotPath) } if gotBody["title"] != "title" || gotBody["body"] != "body" { t.Errorf("body = %#v", gotBody) } if _, ok := gotBody["urls"]; ok { t.Errorf("key mode must not send urls: %#v", gotBody) } } func TestSendStatelessModeWithAuthAndType(t *testing.T) { var gotAuth, gotType string var gotURLs []any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotAuth = r.Header.Get("Authorization") var body map[string]any raw, _ := io.ReadAll(r.Body) _ = json.Unmarshal(raw, &body) gotType, _ = body["type"].(string) gotURLs, _ = body["urls"].([]any) w.WriteHeader(http.StatusOK) })) defer srv.Close() c := New(srv.URL, "", []string{"mailto:sam@example.com"}, "secret", 0) if err := c.SendType(context.Background(), "t", "b", TypeWarning); err != nil { t.Fatalf("SendType: %v", err) } if gotAuth != "Bearer secret" { t.Errorf("auth = %q", gotAuth) } if gotType != TypeWarning { t.Errorf("type = %q, want %q", gotType, TypeWarning) } if len(gotURLs) != 1 || gotURLs[0] != "mailto:sam@example.com" { t.Errorf("urls = %#v", gotURLs) } } func TestSendErrorStatus(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) defer srv.Close() c := New(srv.URL, "key", nil, "", 0) if err := c.Send(context.Background(), "t", "b"); err == nil { t.Fatal("Send() error = nil, want a status error") } } func TestSendNotConfigured(t *testing.T) { c := New("", "", nil, "", 0) if err := c.Send(context.Background(), "t", "b"); err == nil { t.Fatal("Send() error = nil, want not-configured") } }