2018-01-31 17:07:11 -08:00
|
|
|
package defaults
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-02-01 10:25:54 -08:00
|
|
|
"strings"
|
2018-01-31 17:07:11 -08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
r := NewRouter()
|
|
|
|
var get, post, delete string
|
|
|
|
wantGet, wantPost, wantDelete := "testget", "testpost", "testdelete"
|
|
|
|
|
|
|
|
r.Get("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
get = string(b)
|
|
|
|
}))
|
|
|
|
r.Post("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
post = string(b)
|
|
|
|
}))
|
|
|
|
r.Delete("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete = string(b)
|
|
|
|
}))
|
|
|
|
|
2018-02-01 10:25:54 -08:00
|
|
|
wr := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/test", strings.NewReader("testget"))
|
|
|
|
r.ServeHTTP(wr, req)
|
2018-01-31 17:07:11 -08:00
|
|
|
if get != wantGet {
|
|
|
|
t.Error("want:", wantGet, "got:", get)
|
|
|
|
}
|
2018-02-01 10:25:54 -08:00
|
|
|
if len(post) != 0 || len(delete) != 0 {
|
|
|
|
t.Error("should be empty:", post, delete)
|
|
|
|
}
|
|
|
|
|
|
|
|
wr = httptest.NewRecorder()
|
|
|
|
req = httptest.NewRequest("POST", "/test", strings.NewReader("testpost"))
|
|
|
|
r.ServeHTTP(wr, req)
|
2018-01-31 17:07:11 -08:00
|
|
|
if post != wantPost {
|
|
|
|
t.Error("want:", wantPost, "got:", post)
|
|
|
|
}
|
2018-02-01 10:25:54 -08:00
|
|
|
if len(delete) != 0 {
|
|
|
|
t.Error("should be empty:", delete)
|
|
|
|
}
|
|
|
|
|
|
|
|
wr = httptest.NewRecorder()
|
|
|
|
req = httptest.NewRequest("DELETE", "/test", strings.NewReader("testdelete"))
|
|
|
|
r.ServeHTTP(wr, req)
|
2018-01-31 17:07:11 -08:00
|
|
|
if delete != wantDelete {
|
|
|
|
t.Error("want:", wantDelete, "got:", delete)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterBadMethod(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
r := NewRouter()
|
|
|
|
wr := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("OPTIONS", "/", nil)
|
|
|
|
|
|
|
|
r.ServeHTTP(wr, req)
|
|
|
|
|
2018-02-04 21:24:55 -08:00
|
|
|
if wr.Code != http.StatusMethodNotAllowed {
|
|
|
|
t.Error("want method not allowed code, got:", wr.Code)
|
2018-01-31 17:07:11 -08:00
|
|
|
}
|
|
|
|
}
|