代码拉取完成,页面将自动刷新
同步操作将从 lunny/tango 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
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.Any("/1", func() string {
return "/1"
})
g.Options("/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("OPTIONS", "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.Trace("/1", func() string {
return "/1"
})
cg.Patch("/2", func() string {
return "/2"
})
})
})
req, err := http.NewRequest("TRACE", "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("PATCH", "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.Delete("/api/1", func() string {
return "/1"
})
g.Head("/api/2", func() string {
return "/2"
})
})
req, err := http.NewRequest("DELETE", "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("HEAD", "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 TestGroup5(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var handlerGroup bool
o.Group("/api", func(g *Group) {
g.Use(HandlerFunc(func(ctx *Context) {
handlerGroup = true
ctx.Next()
}))
g.Put("/1", func() string {
return "/1"
})
})
o.Post("/2", func() string {
return "/2"
})
req, err := http.NewRequest("PUT", "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")
expect(t, handlerGroup, true)
handlerGroup = false
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/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")
expect(t, handlerGroup, false)
}
func TestGroup6(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
var handlerGroup bool
g := NewGroup()
g.Use(HandlerFunc(func(ctx *Context) {
handlerGroup = true
ctx.Next()
}))
g.Get("/1", func() string {
return "/1"
})
o := Classic()
o.Group("/api", g)
o.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")
expect(t, handlerGroup, true)
handlerGroup = false
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/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")
expect(t, handlerGroup, false)
}
func TestGroup7(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
var isPanic bool
defer func() {
if err := recover(); err != nil {
isPanic = true
}
expect(t, isPanic, true)
}()
o := Classic()
o.Group("/api", func() {
})
req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
}
func TestGroup8(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
var handlerGroup bool
g := NewGroup()
g.Use(HandlerFunc(func(ctx *Context) {
handlerGroup = true
ctx.Next()
}))
g.Get("/1", func(ctx *Context) string {
p := ctx.Params()
return (*p)[0].Value
})
o := Classic()
o.Group("/:user", g)
o.Post("/2", func() string {
return "/2"
})
req, err := http.NewRequest("GET", "http://localhost:8000/myname/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(), "myname")
expect(t, handlerGroup, true)
handlerGroup = false
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/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")
expect(t, handlerGroup, false)
}
func TestGroup9(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var handlerGroup string
o.Group("/api/v1", func(g *Group) {
g.Group("/case/:case_id", func(tg *Group) {
tg.Use(HandlerFunc(func(ctx *Context) {
handlerGroup = ctx.Param("case_id")
ctx.Next()
}))
tg.Put("/1", func() string {
return "/1"
})
})
})
o.Post("/api/v1/2", func() string {
return "/2"
})
req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/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")
expect(t, handlerGroup, "1")
handlerGroup = ""
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")
expect(t, handlerGroup, "")
}
func TestGroup10(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var handlerGroup string
o.Group("/api/v1", func(g *Group) {
g.Group("/case/:case_id", func(tg *Group) {
tg.Put("/1", func() string {
return "/1"
})
})
}, HandlerFunc(func(ctx *Context) {
handlerGroup = ctx.Param("case_id")
ctx.Next()
}))
o.Post("/api/v1/2", func() string {
return "/2"
})
req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/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")
expect(t, handlerGroup, "1")
handlerGroup = ""
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")
expect(t, handlerGroup, "")
}
func TestGroup11(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var handlerGroup string
o.Group("/api/v1", func(g *Group) {
g.Group("/case/:case_id", func(tg *Group) {
tg.Put("/1", func() string {
return "/1"
})
}, HandlerFunc(func(ctx *Context) {
handlerGroup = ctx.Param("case_id")
ctx.Next()
}))
})
o.Post("/api/v1/2", func() string {
return "/2"
})
req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/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")
expect(t, handlerGroup, "1")
handlerGroup = ""
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")
expect(t, handlerGroup, "")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。