context_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package tango
  2. import (
  3. "testing"
  4. "bytes"
  5. "strings"
  6. "net/http/httptest"
  7. "net/http"
  8. )
  9. type CtxAction struct {
  10. Ctx
  11. }
  12. func (p *CtxAction) Get() {
  13. p.Ctx.Write([]byte("context"))
  14. }
  15. func TestContext1(t *testing.T) {
  16. buff := bytes.NewBufferString("")
  17. recorder := httptest.NewRecorder()
  18. recorder.Body = buff
  19. o := Classic()
  20. o.Get("/", new(CtxAction))
  21. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. o.ServeHTTP(recorder, req)
  26. expect(t, recorder.Code, http.StatusOK)
  27. refute(t, len(buff.String()), 0)
  28. expect(t, buff.String(), "context")
  29. }
  30. type CtxJsonAction struct {
  31. Ctx
  32. }
  33. func (p *CtxJsonAction) Get() error {
  34. return p.Ctx.ServeJson(map[string]string{
  35. "get": "ctx",
  36. })
  37. }
  38. func TestContext2(t *testing.T) {
  39. buff := bytes.NewBufferString("")
  40. recorder := httptest.NewRecorder()
  41. recorder.Body = buff
  42. o := Classic()
  43. o.Get("/", new(CtxJsonAction))
  44. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  45. if err != nil {
  46. t.Error(err)
  47. }
  48. o.ServeHTTP(recorder, req)
  49. expect(t, recorder.Code, http.StatusOK)
  50. refute(t, len(buff.String()), 0)
  51. expect(t, strings.TrimSpace(buff.String()), `{"get":"ctx"}`)
  52. }
  53. type CtxXmlAction struct {
  54. Ctx
  55. }
  56. type XmlStruct struct {
  57. Content string
  58. }
  59. func (p *CtxXmlAction) Get() error {
  60. return p.Ctx.ServeXml(XmlStruct{"content"})
  61. }
  62. func TestContext3(t *testing.T) {
  63. buff := bytes.NewBufferString("")
  64. recorder := httptest.NewRecorder()
  65. recorder.Body = buff
  66. o := Classic()
  67. o.Get("/", new(CtxXmlAction))
  68. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  69. if err != nil {
  70. t.Error(err)
  71. }
  72. o.ServeHTTP(recorder, req)
  73. expect(t, recorder.Code, http.StatusOK)
  74. refute(t, len(buff.String()), 0)
  75. expect(t, strings.TrimSpace(buff.String()), `<XmlStruct><Content>content</Content></XmlStruct>`)
  76. }
  77. type CtxFileAction struct {
  78. Ctx
  79. }
  80. func (p *CtxFileAction) Get() error {
  81. return p.Ctx.ServeFile("./public/index.html")
  82. }
  83. func TestContext4(t *testing.T) {
  84. buff := bytes.NewBufferString("")
  85. recorder := httptest.NewRecorder()
  86. recorder.Body = buff
  87. o := Classic()
  88. o.Any("/", new(CtxFileAction))
  89. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  90. if err != nil {
  91. t.Error(err)
  92. }
  93. o.ServeHTTP(recorder, req)
  94. expect(t, recorder.Code, http.StatusOK)
  95. refute(t, len(buff.String()), 0)
  96. expect(t, strings.TrimSpace(buff.String()), `this is index.html`)
  97. }
  98. func TestContext5(t *testing.T) {
  99. buff := bytes.NewBufferString("")
  100. recorder := httptest.NewRecorder()
  101. recorder.Body = buff
  102. o := Classic()
  103. o.Any("/2", func() string {
  104. return "2"
  105. })
  106. o.Any("/", func(ctx *Context) {
  107. ctx.Redirect("/2")
  108. })
  109. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  110. if err != nil {
  111. t.Error(err)
  112. }
  113. o.ServeHTTP(recorder, req)
  114. expect(t, recorder.Code, http.StatusFound)
  115. refute(t, len(buff.String()), 0)
  116. expect(t, strings.TrimSpace(buff.String()), `<a href="/2">Found</a>.`)
  117. }