|
|
|
@ -10,7 +10,7 @@ import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"zero/core/httprouter"
|
|
|
|
|
"zero/ngin/internal/router"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
@ -20,15 +20,6 @@ const (
|
|
|
|
|
contentLength = "Content-Length"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGetRemoteAddr(t *testing.T) {
|
|
|
|
|
host := "8.8.8.8"
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
r.Header.Set(xForwardFor, host)
|
|
|
|
|
assert.Equal(t, host, GetRemoteAddr(r))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseForm(t *testing.T) {
|
|
|
|
|
var v struct {
|
|
|
|
|
Name string `form:"name"`
|
|
|
|
@ -135,8 +126,8 @@ func TestParseSlice(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
rt := router.NewPatRouter()
|
|
|
|
|
err = rt.Handle(http.MethodPost, "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
|
Names []string `form:"names"`
|
|
|
|
|
}{}
|
|
|
|
@ -150,7 +141,7 @@ func TestParseSlice(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(rr, r)
|
|
|
|
|
rt.ServeHTTP(rr, r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseJsonPost(t *testing.T) {
|
|
|
|
@ -159,7 +150,7 @@ func TestParseJsonPost(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
|
|
|
|
|
w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -191,7 +182,7 @@ func TestParseJsonPostWithIntSlice(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
|
|
|
|
|
w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -219,7 +210,7 @@ func TestParseJsonPostError(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -247,7 +238,7 @@ func TestParseJsonPostInvalidRequest(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -269,7 +260,7 @@ func TestParseJsonPostRequired(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -292,7 +283,7 @@ func TestParsePath(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -317,7 +308,7 @@ func TestParsePathRequired(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -338,7 +329,7 @@ func TestParseQuery(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -363,7 +354,7 @@ func TestParseQueryRequired(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
|
Nickname string `form:"nickname"`
|
|
|
|
@ -383,7 +374,7 @@ func TestParseOptional(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -424,7 +415,7 @@ func TestParseNestedInRequestEmpty(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -463,7 +454,7 @@ func TestParsePtrInRequest(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -494,7 +485,7 @@ func TestParsePtrInRequestEmpty(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/kevin", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -511,7 +502,7 @@ func TestParseQueryOptional(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -536,7 +527,7 @@ func TestParse(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -574,7 +565,7 @@ func TestParseWrappedRequest(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -606,7 +597,7 @@ func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -639,7 +630,7 @@ func TestParseWrappedHeadRequestWithJsonHeader(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodHead, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -671,7 +662,7 @@ func TestParseWrappedRequestPtr(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var v WrappedRequest
|
|
|
|
@ -694,7 +685,7 @@ func TestParseWithAll(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
|
Name string `path:"name"`
|
|
|
|
@ -725,7 +716,7 @@ func TestParseWithAllUtf8(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, applicationJsonWithUtf8)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -756,7 +747,7 @@ func TestParseWithMissingForm(t *testing.T) {
|
|
|
|
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -783,7 +774,7 @@ func TestParseWithMissingAllForms(t *testing.T) {
|
|
|
|
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -809,7 +800,7 @@ func TestParseWithMissingJson(t *testing.T) {
|
|
|
|
|
bytes.NewBufferString(`{"location": "shanghai"}`))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -835,7 +826,7 @@ func TestParseWithMissingAllJsons(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -862,7 +853,7 @@ func TestParseWithMissingPath(t *testing.T) {
|
|
|
|
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -889,7 +880,7 @@ func TestParseWithMissingAllPaths(t *testing.T) {
|
|
|
|
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -916,7 +907,7 @@ func TestParseGetWithContentLengthHeader(t *testing.T) {
|
|
|
|
|
r.Header.Set(ContentType, ApplicationJson)
|
|
|
|
|
r.Header.Set(contentLength, "1024")
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -943,7 +934,7 @@ func TestParseJsonPostWithTypeMismatch(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, applicationJsonWithUtf8)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|
|
|
|
@ -969,7 +960,7 @@ func TestParseJsonPostWithInt2String(t *testing.T) {
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
r.Header.Set(ContentType, applicationJsonWithUtf8)
|
|
|
|
|
|
|
|
|
|
router := httprouter.NewPatRouter()
|
|
|
|
|
router := router.NewPatRouter()
|
|
|
|
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
v := struct {
|