123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- package tango
- import (
- "bytes"
- "net/http"
- "net/http/httptest"
- "testing"
- )
- type RouterNoMethodAction struct {
- }
- func TestRouter1(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/:name", new(RouterNoMethodAction))
- req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusNotFound)
- refute(t, len(buff.String()), 0)
- }
- type RouterGetAction struct {
- }
- func (a *RouterGetAction) Get() string {
- return "get"
- }
- func (a *RouterGetAction) Post() string {
- return "post"
- }
- func TestRouter2(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/:name", new(RouterGetAction))
- o.Post("/:name", new(RouterGetAction))
- req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "get")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/foobar", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "post")
- }
- type RouterSpecAction struct {
- a string
- }
- func (RouterSpecAction) Method1() string {
- return "1"
- }
- func (r *RouterSpecAction) Method2() string {
- return r.a
- }
- /*
- func TestRouter3(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/1", new(RouterSpecAction).Method1)
- o.Get("/2", new(RouterSpecAction).Method2)
- req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- }*/
- func TestRouterFunc(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/", func() string {
- return "func"
- })
- o.Post("/", func(ctx *Context) {
- ctx.Write([]byte("func(*Context)"))
- })
- o.Put("/", func(resp http.ResponseWriter, req *http.Request) {
- resp.Write([]byte("func(http.ResponseWriter, *http.Request)"))
- })
- o.Options("/", func(resp http.ResponseWriter) {
- resp.Write([]byte("func(http.ResponseWriter)"))
- })
- o.Delete("/", func(req *http.Request) string {
- return "func(*http.Request)"
- })
- // plain
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "func")
- // context
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "func(*Context)")
- // http
- buff.Reset()
- req, err = http.NewRequest("PUT", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "func(http.ResponseWriter, *http.Request)")
- // response
- buff.Reset()
- req, err = http.NewRequest("OPTIONS", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "func(http.ResponseWriter)")
- // req
- buff.Reset()
- req, err = http.NewRequest("DELETE", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "func(*http.Request)")
- }
- type Router4Action struct {
- Params
- }
- func (r *Router4Action) Get() string {
- return r.Params.Get(":name1") + "-" + r.Params.Get(":name2")
- }
- func TestRouter4(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/:name1-:name2", new(Router4Action))
- req, err := http.NewRequest("GET", "http://localhost:8000/foobar-foobar2", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "foobar-foobar2")
- }
- type Router5Action struct {
- }
- func (r *Router5Action) Get() string {
- return "router5"
- }
- func TestRouter5(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Route("GET", "/", new(Router5Action))
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "router5")
- }
- type Router6Action struct {
- }
- func (r *Router6Action) MyMethod() string {
- return "router6"
- }
- func TestRouter6(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Route("GET:MyMethod", "/", new(Router6Action))
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "router6")
- }
- type Router7Action struct {
- }
- func (r *Router7Action) MyGet() string {
- return "router7-get"
- }
- func (r *Router7Action) Post() string {
- return "router7-post"
- }
- func TestRouter7(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Route([]string{"GET:MyGet", "POST"}, "/", new(Router7Action))
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "router7-get")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "router7-post")
- }
- type Router8Action struct {
- }
- func (r *Router8Action) MyGet() string {
- return "router8-get"
- }
- func (r *Router8Action) Post() string {
- return "router8-post"
- }
- func TestRouter8(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Route(map[string]string{"GET": "MyGet", "POST": "Post"}, "/", new(Router8Action))
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "router8-get")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "router8-post")
- }
|