Files
show-rss/src/cmd/server/handler/feeds_test.go
2025-05-07 19:45:39 -06:00

65 lines
1.4 KiB
Go

package handler_test
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"show-rss/src/cmd/server/handler"
"show-rss/src/db"
"show-rss/src/feeds"
"strings"
"testing"
)
func TestFeeds(t *testing.T) {
ctx := db.Test(t, context.Background())
h := handler.New(ctx)
t.Run("happy", func(t *testing.T) {
body := make(url.Values)
body.Set("URL", "url")
body.Set("Cron", "cron")
body.Set("Pattern", "pattern")
body.Set("WebhookMethod", "wmethod")
body.Set("WebhookURL", "wurl")
body.Set("WebhookBody", "wbody")
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodPost, "/v1/feeds", strings.NewReader(body.Encode()))
r = r.WithContext(ctx)
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
h.ServeHTTP(w, r)
if w.Code != http.StatusSeeOther {
t.Errorf("(%d) %s", w.Code, w.Body.Bytes())
}
found := false
if err := feeds.ForEach(ctx, func(f feeds.Feed) error {
t.Logf("%+v", f)
if f.Version.URL != "url" {
t.Errorf("bad url")
}
if f.Version.Cron != "cron" {
t.Errorf("bad cron")
}
if f.Version.Pattern != "pattern" {
t.Errorf("bad pattern")
}
if f.Version.WebhookMethod != "wmethod" {
t.Errorf("bad wmethod")
}
if f.Version.WebhookURL != "wurl" {
t.Errorf("bad wurl")
}
if f.Version.WebhookBody != "wbody" {
t.Errorf("bad wbody")
}
found = true
return nil
}); err != nil {
t.Error(err)
} else if !found {
t.Error(found)
}
})
}