Change feed url from query param

master
Bel LaPointe 2018-10-10 17:04:37 -06:00
parent 7ee5f9829c
commit 78e38f84d1
3 changed files with 19 additions and 18 deletions

View File

@ -90,8 +90,7 @@ func Test_Core(t *testing.T) {
}, },
{ {
method: "get", method: "get",
path: "api/feed", path: "api/feed/" + rssserver.URL + "/feed",
body: rssserver.URL + "/feed",
status: 200, status: 200,
}, },
{ {

View File

@ -89,12 +89,14 @@ func (s *Server) api(w http.ResponseWriter, r *http.Request) {
func (s *Server) feed(w http.ResponseWriter, r *http.Request) { func (s *Server) feed(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "GET": case "GET":
switch advance(r) { v := advance(r)
switch v {
case "item": case "item":
s.getFeedItem(w, r) s.getFeedItem(w, r)
case "tag": case "tag":
s.getFeedTag(w, r) s.getFeedTag(w, r)
case "": default:
r.URL.Path = v + "/" + r.URL.Path
s.getFeed(w, r) s.getFeed(w, r)
} }
case "POST": case "POST":
@ -172,23 +174,20 @@ func (s *Server) getFeedItem(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) getFeed(w http.ResponseWriter, r *http.Request) { func (s *Server) getFeed(w http.ResponseWriter, r *http.Request) {
url, err := url.ParseQuery(r.URL.RawQuery) var err error
if err != nil { url := r.URL.Path
logger.Logf("cannot get feed to read: %v", err) if url == "" {
s.mybad(w, r) s.bad(w, r)
return return
} }
limit := 20 limit := 20
if url.Get("limit") != "" { if limitB, err := strconv.Atoi(strings.Split(url, ":")[0]); err == nil {
limit, err = strconv.Atoi(url.Get("limit")) limit = limitB
if err != nil { url = url[len(strings.Split(url, ":")[0])+1:]
s.bad(w, r)
return
}
} }
feedBody, err := s.getFeedHandler(url.Get("url"), limit) feedBody, err := s.getFeedHandler(url, limit)
if err != nil { if err != nil {
logger.Logf("cannot get feed %s: %v", url.Get("url"), err) logger.Logf("cannot get feed %s: %v", url, err)
s.mybad(w, r) s.mybad(w, r)
return return
} }

View File

@ -31,7 +31,7 @@ func Test_Server(t *testing.T) {
if err := checkStatus("GET", "api", http.StatusNotFound); err != nil { if err := checkStatus("GET", "api", http.StatusNotFound); err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if err := checkStatus("GET", "api/feed", http.StatusOK); err != nil { if err := checkStatus("GET", "api/feed", http.StatusBadRequest); err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if err := checkStatus("POST", "api/feed", http.StatusBadRequest); err != nil { if err := checkStatus("POST", "api/feed", http.StatusBadRequest); err != nil {
@ -46,7 +46,10 @@ func Test_Server(t *testing.T) {
if err := checkStatus("PUT", "api/feed", http.StatusOK, `{"url":"localhost:1234", "refresh":"1m", "tags":["a", "b"]}`); err != nil { if err := checkStatus("PUT", "api/feed", http.StatusOK, `{"url":"localhost:1234", "refresh":"1m", "tags":["a", "b"]}`); err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if err := checkStatus("GET", "api/feed?url=localhost_1234", http.StatusOK); err != nil { if err := checkStatus("GET", "api/feed/localhost_1234", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api/feed/1:localhost_1234", http.StatusOK); err != nil {
t.Errorf(err.Error()) t.Errorf(err.Error())
} }
if err := checkStatus("GET", "api/feed/item/localhost_1234", http.StatusOK); err != nil { if err := checkStatus("GET", "api/feed/item/localhost_1234", http.StatusOK); err != nil {