tests pass for POST /v1/feeds
parent
e2eb0afe06
commit
e4e5529887
|
|
@ -2,14 +2,16 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"show-rss/src/feeds"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error {
|
func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
return h.feedsPost(ctx, r.Body)
|
return h.feedsPost(r.Context(), r.Body)
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
@ -17,4 +19,18 @@ func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) feedsPost(ctx context.Context, r io.Reader) error {
|
func (h Handler) feedsPost(ctx context.Context, r io.Reader) error {
|
||||||
|
var req struct {
|
||||||
|
URL string
|
||||||
|
Cron string
|
||||||
|
Pattern string
|
||||||
|
WebhookMethod string
|
||||||
|
WebhookURL string
|
||||||
|
WebhookBody string
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r).Decode(&req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ func TestFeeds(t *testing.T) {
|
||||||
"webhookURL": "wurl",
|
"webhookURL": "wurl",
|
||||||
"webhookBody": "wbody"
|
"webhookBody": "wbody"
|
||||||
}`))
|
}`))
|
||||||
|
r = r.WithContext(ctx)
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Errorf("(%d) %s", w.Code, w.Body.Bytes())
|
t.Errorf("(%d) %s", w.Code, w.Body.Bytes())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue