De-comment server tests

master
Bel LaPointe 2018-10-10 14:18:06 -06:00
parent 6c40901465
commit 440ebcc54a
2 changed files with 74 additions and 44 deletions

View File

@ -18,17 +18,19 @@ import (
type Server struct { type Server struct {
addr string addr string
newFeedHandler func(string, string, string, time.Duration) newFeedHandler func(string, string, string, []string, time.Duration)
getFeedHandler func(string, int) (string, error) getFeedHandler func(string, int) (string, error)
getFeedItemHandler func(string) (string, error) getFeedItemHandler func(string) (string, error)
getFeedTagHandler func(string) (string, error)
} }
func New(addr string, newFeedHandler func(string, string, string, time.Duration), getFeedHandler func(string, int) (string, error), getFeedItemHandler func(string) (string, error)) (*Server, error) { func New(addr string, newFeedHandler func(string, string, string, []string, time.Duration), getFeedHandler func(string, int) (string, error), getFeedItemHandler func(string) (string, error), getFeedTagHandler func(string) (string, error)) (*Server, error) {
return &Server{ return &Server{
addr: addr, addr: addr,
newFeedHandler: newFeedHandler, newFeedHandler: newFeedHandler,
getFeedHandler: getFeedHandler, getFeedHandler: getFeedHandler,
getFeedItemHandler: getFeedItemHandler, getFeedItemHandler: getFeedItemHandler,
getFeedTagHandler: getFeedTagHandler,
}, nil }, nil
} }
@ -87,9 +89,12 @@ 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":
if advance(r) == "item" { switch advance(r) {
case "item":
s.getFeedItem(w, r) s.getFeedItem(w, r)
} else { case "tag":
s.getFeedTag(w, r)
case "":
s.getFeed(w, r) s.getFeed(w, r)
} }
case "POST": case "POST":
@ -104,10 +109,11 @@ func (s *Server) feed(w http.ResponseWriter, r *http.Request) {
func (s *Server) newFeed(w http.ResponseWriter, r *http.Request) { func (s *Server) newFeed(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
newFeedBody := struct { newFeedBody := struct {
URL string `json:"url"` URL string `json:"url"`
Refresh string `json:"refresh"` Refresh string `json:"refresh"`
ItemFilter string `json:"items"` ItemFilter string `json:"items"`
ContentFilter string `json:"content"` ContentFilter string `json:"content"`
Tags []string `json:"tags"`
}{ }{
Refresh: "3h", Refresh: "3h",
} }
@ -132,7 +138,23 @@ func (s *Server) newFeed(w http.ResponseWriter, r *http.Request) {
s.bad(w, r) s.bad(w, r)
return return
} }
s.newFeedHandler(newFeedBody.URL, newFeedBody.ItemFilter, newFeedBody.ContentFilter, interval) s.newFeedHandler(newFeedBody.URL, newFeedBody.ItemFilter, newFeedBody.ContentFilter, newFeedBody.Tags, interval)
}
func (s *Server) getFeedTag(w http.ResponseWriter, r *http.Request) {
url, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
logger.Logf("cannot get feed tag to read: %v", err)
s.mybad(w, r)
return
}
feedBody, err := s.getFeedTagHandler(url.Get("url"))
if err != nil {
logger.Logf("cannot get feed tag %s: %v", url.Get("url"), err)
s.mybad(w, r)
return
}
fmt.Fprintln(w, feedBody)
} }
func (s *Server) getFeedItem(w http.ResponseWriter, r *http.Request) { func (s *Server) getFeedItem(w http.ResponseWriter, r *http.Request) {

View File

@ -4,50 +4,58 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest"
"strings"
"syscall" "syscall"
"testing" "testing"
"time" "time"
) )
const testPort = "39231" var testPort = "39231"
func Test_Server(t *testing.T) { func Test_Server(t *testing.T) {
cases := []struct { server := httptest.NewUnstartedServer(nil)
}{ server.Close()
{}, testPort = strings.Split(server.Listener.Addr().String(), ":")[1]
}
for _, _ = range cases { var err error
var err error s, err := New(testPort, func(string, string, string, []string, time.Duration) {}, func(string, int) (string, error) { return "", nil }, func(string) (string, error) { return "", nil }, func(string) (string, error) { return "", nil })
s, err := New(testPort, func(string, string, string, time.Duration) {}, func(string, int) (string, error) { return "", nil }, func(string) (string, error) { return "", nil }) if err != nil {
if err != nil { t.Errorf("failed to create server: %v", err)
t.Errorf("failed to create server: %v", err)
}
go s.Serve()
time.Sleep(time.Second * 1)
if err := checkStatus("GET", "", http.StatusNotFound); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api", http.StatusNotFound); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api/feed", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("POST", "api/feed", http.StatusBadRequest); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("PUT", "api/feed", http.StatusBadRequest, "invalid json"); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("POST", "api/feed", http.StatusBadRequest, `{"url":"hello/world", "refresh":"1m"}`); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("PUT", "api/feed", http.StatusOK, `{"url":"localhost:1234", "refresh":"1m"}`); err != nil {
t.Errorf(err.Error())
}
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
} }
go s.Serve()
time.Sleep(time.Second * 1)
if err := checkStatus("GET", "", http.StatusNotFound); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api", http.StatusNotFound); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api/feed", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("POST", "api/feed", http.StatusBadRequest); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("PUT", "api/feed", http.StatusBadRequest, "invalid json"); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("POST", "api/feed", http.StatusBadRequest, `{"url":"hello/world", "refresh":"1m"}`); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("PUT", "api/feed", http.StatusOK, `{"url":"localhost:1234", "refresh":"1m", "tags":["a", "b"]}`); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api/feed?url=localhost_1234", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api/feed/item?url=localhost_1234", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("GET", "api/feed/tag?url=b", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
} }
func checkStatus(method, path string, code int, body ...string) error { func checkStatus(method, path string, code int, body ...string) error {