123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package tango
- import (
- "testing"
- "bytes"
- "strings"
- "net/http/httptest"
- "net/http"
- )
- 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, 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)
- 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>.`)
- }
|