27 Star 83 Fork 11

lunny/tango

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
context_test.go 10.67 KB
一键复制 编辑 原始数据 按行查看 历史
lunny 提交于 2020-12-09 20:53 . More tests
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
// 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"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type CtxAction struct {
Ctx
}
func (p *CtxAction) Get() {
p.Ctx.Write([]byte("context"))
}
func TestContext1(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/", new(CtxAction))
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(), "context")
}
type CtxJSONAction struct {
Ctx
}
func (p *CtxJSONAction) Get() error {
return p.Ctx.ServeJson(map[string]string{
"get": "ctx",
})
}
func TestContext2(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/", new(CtxJSONAction))
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, recorder.Header().Get("Content-Type"), "application/json; charset=UTF-8")
expect(t, strings.TrimSpace(buff.String()), `{"get":"ctx"}`)
}
type CtxXMLAction struct {
Ctx
}
type XMLStruct struct {
Content string
}
func (p *CtxXMLAction) Get() error {
return p.Ctx.ServeXml(XMLStruct{"content"})
}
func TestContext3(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/", new(CtxXMLAction))
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)
expect(t, recorder.Header().Get("Content-Type"), "application/xml; charset=UTF-8")
refute(t, len(buff.String()), 0)
expect(t, strings.TrimSpace(buff.String()), `<XMLStruct><Content>content</Content></XMLStruct>`)
}
type CtxFileAction struct {
Ctx
}
func (p *CtxFileAction) Get() error {
return p.Ctx.ServeFile("./public/index.html")
}
func TestContext4(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(CtxFileAction))
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, strings.TrimSpace(buff.String()), `this is index.html`)
}
func TestContext5(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/2", func() string {
return "2"
})
o.Any("/", func(ctx *Context) {
ctx.Redirect("/2")
})
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusFound)
refute(t, len(buff.String()), 0)
expect(t, strings.TrimSpace(buff.String()), `<a href="/2">Found</a>.`)
}
type NotFoundAction struct {
Ctx
}
func (n *NotFoundAction) Get() {
n.NotFound("not found")
}
func TestContext6(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(NotFoundAction))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusNotFound)
refute(t, len(buff.String()), 0)
expect(t, strings.TrimSpace(buff.String()), "not found")
}
type NotModifidAction struct {
Ctx
}
func (n *NotModifidAction) Get() {
n.NotModified()
}
func TestContext7(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(NotModifidAction))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusNotModified)
expect(t, len(buff.String()), 0)
}
type UnauthorizedAction struct {
Ctx
}
func (n *UnauthorizedAction) Get() {
n.Unauthorized()
}
func TestContext8(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(UnauthorizedAction))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusUnauthorized)
expect(t, buff.String(), http.StatusText(http.StatusUnauthorized))
}
type DownloadAction struct {
Ctx
}
func (n *DownloadAction) Get() {
n.Download("./public/index.html")
}
func TestContext9(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(DownloadAction))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Header().Get("Content-Disposition"), `attachment; filename="index.html"`)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "this is index.html")
}
// check unsupported function will panic
func TestContext10(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var ifPanic bool
defer func() {
if err := recover(); err != nil {
ifPanic = true
}
expect(t, ifPanic, true)
}()
o.Any("/", func(i int) {
fmt.Println(i)
})
}
type DownloadAction2 struct {
Ctx
}
func (n *DownloadAction2) Get() {
n.ServeFile("./public/a.html")
}
func TestContext11(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(DownloadAction2))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusNotFound)
}
func TestContext12(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", func() string {
return "text"
})
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)
expect(t, buff.String(), "text")
}
func TestContext13(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/", func(responseWriter http.ResponseWriter, req *http.Request) {
responseWriter.Write([]byte("text"))
})
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)
expect(t, buff.String(), "text")
}
func TestContext14(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/", func(req *http.Request) string {
return "text"
})
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)
expect(t, buff.String(), "text")
}
func TestContextDecodeJSON(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
type Data struct {
Name string
}
o := Classic()
o.Post("/", func(ctx *Context) string {
var data Data
err := ctx.DecodeJSON(&data)
if err != nil {
return err.Error()
}
return data.Name
})
req, err := http.NewRequest("POST", "http://localhost:8000/", bytes.NewBufferString(`
{
"Name": "lunny"
}
`))
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "lunny")
}
func TestContextDecodeXML(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
type Data struct {
Name string `xml:"name"`
}
o := Classic()
o.Post("/", func(ctx *Context) string {
var data Data
err := ctx.DecodeXML(&data)
if err != nil {
return err.Error()
}
return data.Name
})
req, err := http.NewRequest("POST", "http://localhost:8000/", bytes.NewBufferString(`
<Data><name>lunny</name></Data>
`))
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "lunny")
}
type ActionTag struct {
Name string `tag:"name"`
}
func (a *ActionTag) Get() interface{} {
return "lunny"
}
func TestContextActionTag(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Use(HandlerFunc(func(ctx *Context) {
ctx.Next()
v := ctx.ActionValue()
if a, ok := v.Interface().(*ActionTag); ok {
fmt.Println(a.Get())
}
tagName := ctx.ActionTag("Name")
expect(t, `tag:"name"`, tagName)
}))
o.Get("/", new(ActionTag))
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)
expect(t, buff.String(), "lunny")
}
type AnyAnyAction struct {
JSON
}
func (a *AnyAnyAction) Any() interface{} {
return map[string]string{
"here": "anyany",
}
}
func TestContextAnyAny(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := New(
Logging(),
Recovery(false),
Return(),
Param(),
Contexts(),
)
o.Any("/", new(AnyAnyAction))
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)
expect(t, strings.TrimSpace(buff.String()), `{"here":"anyany"}`)
}
type AnyAnyGetAction struct {
JSON
}
func (a *AnyAnyGetAction) Any() interface{} {
return map[string]string{
"here": "anyany",
}
}
func (a *AnyAnyGetAction) Get() interface{} {
return map[string]string{
"here": "anyget",
}
}
func TestContextAnyAnyGet(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := New(
Logging(),
Recovery(false),
Return(),
Param(),
Contexts(),
)
o.Any("/", new(AnyAnyGetAction))
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)
expect(t, strings.TrimSpace(buff.String()), `{"here":"anyget"}`)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lunny/tango.git
git@gitee.com:lunny/tango.git
lunny
tango
tango
master

搜索帮助