123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008 |
- package router
- import (
- "bytes"
- "fmt"
- "io"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/rest/httpx"
- "github.com/tal-tech/go-zero/rest/internal/context"
- )
- const (
- applicationJsonWithUtf8 = "application/json; charset=utf-8"
- contentLength = "Content-Length"
- )
- type mockedResponseWriter struct {
- code int
- }
- func (m *mockedResponseWriter) Header() http.Header {
- return http.Header{}
- }
- func (m *mockedResponseWriter) Write(p []byte) (int, error) {
- return len(p), nil
- }
- func (m *mockedResponseWriter) WriteHeader(code int) {
- m.code = code
- }
- func TestPatRouterHandleErrors(t *testing.T) {
- tests := []struct {
- method string
- path string
- err error
- }{
- {"FAKE", "", ErrInvalidMethod},
- {"GET", "", ErrInvalidPath},
- }
- for _, test := range tests {
- t.Run(test.method, func(t *testing.T) {
- router := NewRouter()
- err := router.Handle(test.method, test.path, nil)
- assert.Error(t, ErrInvalidMethod, err)
- })
- }
- }
- func TestPatRouterNotFound(t *testing.T) {
- var notFound bool
- router := NewRouter()
- router.SetNotFoundHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- notFound = true
- }))
- err := router.Handle(http.MethodGet, "/a/b",
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
- assert.Nil(t, err)
- r, _ := http.NewRequest(http.MethodGet, "/b/c", nil)
- w := new(mockedResponseWriter)
- router.ServeHTTP(w, r)
- assert.True(t, notFound)
- }
- func TestPatRouterNotAllowed(t *testing.T) {
- var notAllowed bool
- router := NewRouter()
- router.SetNotAllowedHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- notAllowed = true
- }))
- err := router.Handle(http.MethodGet, "/a/b",
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
- assert.Nil(t, err)
- r, _ := http.NewRequest(http.MethodPost, "/a/b", nil)
- w := new(mockedResponseWriter)
- router.ServeHTTP(w, r)
- assert.True(t, notAllowed)
- }
- func TestPatRouter(t *testing.T) {
- tests := []struct {
- method string
- path string
- expect bool
- code int
- err error
- }{
- // we don't explicitly set status code, framework will do it.
- {http.MethodGet, "/a/b", true, 0, nil},
- {http.MethodGet, "/a/b/", true, 0, nil},
- {http.MethodGet, "/a/b?a=b", true, 0, nil},
- {http.MethodGet, "/a/b/?a=b", true, 0, nil},
- {http.MethodGet, "/a/b/c?a=b", true, 0, nil},
- {http.MethodGet, "/b/d", false, http.StatusNotFound, nil},
- }
- for _, test := range tests {
- t.Run(test.method+":"+test.path, func(t *testing.T) {
- routed := false
- router := NewRouter()
- err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- routed = true
- assert.Equal(t, 1, len(context.Vars(r)))
- }))
- assert.Nil(t, err)
- err = router.Handle(test.method, "/a/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- routed = true
- assert.Nil(t, context.Vars(r))
- }))
- assert.Nil(t, err)
- err = router.Handle(test.method, "/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- routed = true
- }))
- assert.Nil(t, err)
- w := new(mockedResponseWriter)
- r, _ := http.NewRequest(test.method, test.path, nil)
- router.ServeHTTP(w, r)
- assert.Equal(t, test.expect, routed)
- assert.Equal(t, test.code, w.code)
- if test.code == 0 {
- r, _ = http.NewRequest(http.MethodPut, test.path, nil)
- router.ServeHTTP(w, r)
- assert.Equal(t, http.StatusMethodNotAllowed, w.code)
- }
- })
- }
- }
- func TestParseSlice(t *testing.T) {
- body := `names=%5B%22first%22%2C%22second%22%5D`
- reader := strings.NewReader(body)
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/", reader)
- assert.Nil(t, err)
- r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- rt := NewRouter()
- err = rt.Handle(http.MethodPost, "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Names []string `form:"names"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- assert.Equal(t, 2, len(v.Names))
- assert.Equal(t, "first", v.Names[0])
- assert.Equal(t, "second", v.Names[1])
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- rt.ServeHTTP(rr, r)
- }
- func TestParseJsonPost(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
- w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d:%s:%d:%s:%d", v.Name, v.Year,
- v.Nickname, v.Zipcode, v.Location, v.Time))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017:whatever:200000:shanghai:20170912", rr.Body.String())
- }
- func TestParseJsonPostWithIntSlice(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
- bytes.NewBufferString(`{"ages": [1, 2], "years": [3, 4]}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
- w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Ages []int `json:"ages"`
- Years []int64 `json:"years"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- assert.ElementsMatch(t, []int{1, 2}, v.Ages)
- assert.ElementsMatch(t, []int64{3, 4}, v.Years)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseJsonPostError(t *testing.T) {
- payload := `[{"abcd": "cdef"}]`
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(payload))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseJsonPostInvalidRequest(t *testing.T) {
- payload := `{"ages": ["cdef"]}`
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/",
- bytes.NewBufferString(payload))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Ages []int `json:"ages"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseJsonPostRequired(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
- bytes.NewBufferString(`{"location": "shanghai"`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParsePath(t *testing.T) {
- r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s in %d", v.Name, v.Year))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin in 2017", rr.Body.String())
- }
- func TestParsePathRequired(t *testing.T) {
- r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin", nil)
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- 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 := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Nickname, v.Zipcode))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "whatever:200000", rr.Body.String())
- }
- func TestParseQueryRequired(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever", nil)
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- 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 := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode,optional"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Nickname, v.Zipcode))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "whatever:0", rr.Body.String())
- }
- func TestParseNestedInRequestEmpty(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017", bytes.NewBufferString("{}"))
- assert.Nil(t, err)
- type (
- Request struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }
- Audio struct {
- Volume int `json:"volume"`
- }
- WrappedRequest struct {
- Request
- Audio Audio `json:"audio,optional"`
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Name, v.Year))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017", rr.Body.String())
- }
- func TestParsePtrInRequest(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
- bytes.NewBufferString(`{"audio": {"volume": 100}}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- type (
- Request struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }
- Audio struct {
- Volume int `json:"volume"`
- }
- WrappedRequest struct {
- Request
- Audio *Audio `json:"audio,optional"`
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d:%d", v.Name, v.Year, v.Audio.Volume))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017:100", rr.Body.String())
- }
- func TestParsePtrInRequestEmpty(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin", bytes.NewBufferString("{}"))
- assert.Nil(t, err)
- type (
- Audio struct {
- Volume int `json:"volume"`
- }
- WrappedRequest struct {
- Audio *Audio `json:"audio,optional"`
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/kevin", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- 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 := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode,optional"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Nickname, v.Zipcode))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "whatever:0", rr.Body.String())
- }
- 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 := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d:%s:%d", v.Name, v.Year, v.Nickname, v.Zipcode))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017:whatever:200000", rr.Body.String())
- }
- func TestParseWrappedRequest(t *testing.T) {
- r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
- assert.Nil(t, err)
- type (
- Request struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }
- WrappedRequest struct {
- Request
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Name, v.Year))
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017", rr.Body.String())
- }
- func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) {
- r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
- type (
- Request struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }
- WrappedRequest struct {
- Request
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Name, v.Year))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017", rr.Body.String())
- }
- func TestParseWrappedHeadRequestWithJsonHeader(t *testing.T) {
- r, err := http.NewRequest(http.MethodHead, "http://hello.com/kevin/2017", nil)
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
- type (
- Request struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }
- WrappedRequest struct {
- Request
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodHead, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Name, v.Year))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017", rr.Body.String())
- }
- func TestParseWrappedRequestPtr(t *testing.T) {
- r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
- assert.Nil(t, err)
- type (
- Request struct {
- Name string `path:"name"`
- Year int `path:"year"`
- }
- WrappedRequest struct {
- *Request
- }
- )
- router := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- var v WrappedRequest
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d", v.Name, v.Year))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017", rr.Body.String())
- }
- func TestParseWithAll(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d:%s:%d:%s:%d", v.Name, v.Year,
- v.Nickname, v.Zipcode, v.Location, v.Time))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017:whatever:200000:shanghai:20170912", rr.Body.String())
- }
- func TestParseWithAllUtf8(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.Nil(t, err)
- _, err = io.WriteString(w, fmt.Sprintf("%s:%d:%s:%d:%s:%d", v.Name, v.Year,
- v.Nickname, v.Zipcode, v.Location, v.Time))
- assert.Nil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- assert.Equal(t, "kevin:2017:whatever:200000:shanghai:20170912", rr.Body.String())
- }
- func TestParseWithMissingForm(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- assert.Equal(t, "field zipcode is not set", err.Error())
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseWithMissingAllForms(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseWithMissingJson(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"location": "shanghai"}`))
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotEqual(t, io.EOF, err)
- assert.NotNil(t, httpx.Parse(r, &v))
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- 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 := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotEqual(t, io.EOF, err)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseWithMissingPath(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- assert.Equal(t, "field name is not set", err.Error())
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseWithMissingAllPaths(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
- assert.Nil(t, err)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseGetWithContentLengthHeader(t *testing.T) {
- r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
- r.Header.Set(contentLength, "1024")
- router := NewRouter()
- err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Location string `json:"location"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseJsonPostWithTypeMismatch(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
- bytes.NewBufferString(`{"time": "20170912"}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Nickname string `form:"nickname"`
- Zipcode int64 `form:"zipcode"`
- Time int64 `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func TestParseJsonPostWithInt2String(t *testing.T) {
- r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
- bytes.NewBufferString(`{"time": 20170912}`))
- assert.Nil(t, err)
- r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
- router := NewRouter()
- err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
- func(w http.ResponseWriter, r *http.Request) {
- v := struct {
- Name string `path:"name"`
- Year int `path:"year"`
- Time string `json:"time"`
- }{}
- err = httpx.Parse(r, &v)
- assert.NotNil(t, err)
- }))
- assert.Nil(t, err)
- rr := httptest.NewRecorder()
- router.ServeHTTP(rr, r)
- }
- func BenchmarkPatRouter(b *testing.B) {
- b.ReportAllocs()
- router := NewRouter()
- router.Handle(http.MethodGet, "/api/:user/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- }))
- w := &mockedResponseWriter{}
- r, _ := http.NewRequest(http.MethodGet, "/api/a/b", nil)
- for i := 0; i < b.N; i++ {
- router.ServeHTTP(w, r)
- }
- }
|