123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package tango
- import (
- "bytes"
- "net/http"
- "net/http/httptest"
- "testing"
- )
- func TestGroup1(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Group("/api", func(g *Group) {
- g.Get("/1", func() string {
- return "/1"
- })
- g.Post("/2", func() string {
- return "/2"
- })
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/api/1", 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(), "/1")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/api/2", 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(), "/2")
- }
- func TestGroup2(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- g := NewGroup()
- g.Get("/1", func() string {
- return "/1"
- })
- g.Post("/2", func() string {
- return "/2"
- })
- o := Classic()
- o.Group("/api", g)
- req, err := http.NewRequest("GET", "http://localhost:8000/api/1", 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(), "/1")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/api/2", 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(), "/2")
- }
- func TestGroup3(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Group("/api", func(g *Group) {
- g.Group("/v1", func(cg *Group) {
- cg.Get("/1", func() string {
- return "/1"
- })
- cg.Post("/2", func() string {
- return "/2"
- })
- })
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/api/v1/1", 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(), "/1")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", 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(), "/2")
- }
- func TestGroup4(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Group("", func(g *Group) {
- g.Get("/api/1", func() string {
- return "/1"
- })
- g.Post("/api/2", func() string {
- return "/2"
- })
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/api/1", 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(), "/1")
- buff.Reset()
- req, err = http.NewRequest("POST", "http://localhost:8000/api/2", 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(), "/2")
- }
|