POST /v1/feeds plain html
parent
e4e5529887
commit
29cb008f38
|
|
@ -2,33 +2,38 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"show-rss/src/feeds"
|
"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(r.Context(), r.Body)
|
if err := r.ParseForm(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return h.feedsPost(r.Context(), r.Form)
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) feedsPost(ctx context.Context, r io.Reader) error {
|
func (h Handler) feedsPost(ctx context.Context, form url.Values) error {
|
||||||
var req struct {
|
var req feeds.Version
|
||||||
URL string
|
for k, v := range map[string]*string{
|
||||||
Cron string
|
"url": &req.URL,
|
||||||
Pattern string
|
"cron": &req.Cron,
|
||||||
WebhookMethod string
|
"pattern": &req.Pattern,
|
||||||
WebhookURL string
|
"webhookMethod": &req.WebhookMethod,
|
||||||
WebhookBody string
|
"webhookURL": &req.WebhookURL,
|
||||||
}
|
"webhookBody": &req.WebhookBody,
|
||||||
if err := json.NewDecoder(r).Decode(&req); err != nil {
|
} {
|
||||||
return err
|
if *v = form.Get(k); *v == "" {
|
||||||
|
return fmt.Errorf("no ?%s in %s", k, form.Encode())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
|
_, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"show-rss/src/cmd/server/handler"
|
"show-rss/src/cmd/server/handler"
|
||||||
"show-rss/src/db"
|
"show-rss/src/db"
|
||||||
"show-rss/src/feeds"
|
"show-rss/src/feeds"
|
||||||
|
|
@ -16,16 +17,17 @@ func TestFeeds(t *testing.T) {
|
||||||
h := handler.New(ctx)
|
h := handler.New(ctx)
|
||||||
|
|
||||||
t.Run("happy", func(t *testing.T) {
|
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()
|
w := httptest.NewRecorder()
|
||||||
r := httptest.NewRequest(http.MethodPost, "/v1/feeds", strings.NewReader(`{
|
r := httptest.NewRequest(http.MethodPost, "/v1/feeds", strings.NewReader(body.Encode()))
|
||||||
"url": "url",
|
|
||||||
"cron": "cron",
|
|
||||||
"pattern": "pattern",
|
|
||||||
"webhookMethod": "wmethod",
|
|
||||||
"webhookURL": "wurl",
|
|
||||||
"webhookBody": "wbody"
|
|
||||||
}`))
|
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
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