Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.4KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "testing"
  12. )
  13. func TestError1(t *testing.T) {
  14. buff := bytes.NewBufferString("")
  15. recorder := httptest.NewRecorder()
  16. recorder.Body = buff
  17. l := NewLogger(os.Stdout)
  18. o := Classic(l)
  19. o.Get("/", func(ctx *Context) error {
  20. return NotFound()
  21. })
  22. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. o.ServeHTTP(recorder, req)
  27. expect(t, recorder.Code, http.StatusNotFound)
  28. refute(t, len(buff.String()), 0)
  29. }
  30. func TestError2(t *testing.T) {
  31. buff := bytes.NewBufferString("")
  32. recorder := httptest.NewRecorder()
  33. recorder.Body = buff
  34. o := Classic()
  35. o.Get("/", func(ctx *Context) error {
  36. return NotSupported()
  37. })
  38. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  39. if err != nil {
  40. t.Error(err)
  41. }
  42. o.ServeHTTP(recorder, req)
  43. expect(t, recorder.Code, http.StatusMethodNotAllowed)
  44. refute(t, len(buff.String()), 0)
  45. }
  46. func TestError3(t *testing.T) {
  47. buff := bytes.NewBufferString("")
  48. recorder := httptest.NewRecorder()
  49. recorder.Body = buff
  50. o := Classic()
  51. o.Get("/", func(ctx *Context) error {
  52. return InternalServerError()
  53. })
  54. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  55. if err != nil {
  56. t.Error(err)
  57. }
  58. o.ServeHTTP(recorder, req)
  59. expect(t, recorder.Code, http.StatusInternalServerError)
  60. refute(t, len(buff.String()), 0)
  61. }
  62. func TestError4(t *testing.T) {
  63. buff := bytes.NewBufferString("")
  64. recorder := httptest.NewRecorder()
  65. recorder.Body = buff
  66. o := Classic()
  67. o.Patch("/", func(ctx *Context) error {
  68. return Forbidden()
  69. })
  70. req, err := http.NewRequest("PATCH", "http://localhost:8000/", nil)
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. o.ServeHTTP(recorder, req)
  75. expect(t, recorder.Code, http.StatusForbidden)
  76. refute(t, len(buff.String()), 0)
  77. }
  78. func TestError5(t *testing.T) {
  79. buff := bytes.NewBufferString("")
  80. recorder := httptest.NewRecorder()
  81. recorder.Body = buff
  82. o := Classic()
  83. o.Trace("/", func(ctx *Context) error {
  84. return Unauthorized()
  85. })
  86. req, err := http.NewRequest("TRACE", "http://localhost:8000/", nil)
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. o.ServeHTTP(recorder, req)
  91. expect(t, recorder.Code, http.StatusUnauthorized)
  92. refute(t, len(buff.String()), 0)
  93. }
  94. var err500 = Abort(500, "error")
  95. func TestError6(t *testing.T) {
  96. buff := bytes.NewBufferString("")
  97. recorder := httptest.NewRecorder()
  98. recorder.Body = buff
  99. o := Classic()
  100. o.Head("/", func(ctx *Context) error {
  101. return err500
  102. })
  103. req, err := http.NewRequest("HEAD", "http://localhost:8000/", nil)
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. o.ServeHTTP(recorder, req)
  108. expect(t, recorder.Code, err500.Code())
  109. refute(t, len(buff.String()), 0)
  110. expect(t, buff.String(), err500.Error())
  111. }
  112. func TestError7(t *testing.T) {
  113. buff := bytes.NewBufferString("")
  114. recorder := httptest.NewRecorder()
  115. recorder.Body = buff
  116. o := Classic()
  117. o.Head("/", func(ctx *Context) {
  118. return
  119. })
  120. req, err := http.NewRequest("HEAD", "http://localhost:8000/11?==", nil)
  121. if err != nil {
  122. t.Error(err)
  123. }
  124. o.ServeHTTP(recorder, req)
  125. expect(t, recorder.Code, http.StatusNotFound)
  126. refute(t, len(buff.String()), 0)
  127. }
  128. var (
  129. prefix = "<html><head>tango</head><body><div>"
  130. suffix = fmt.Sprintf("</div><div>version: %s</div></body></html>", Version())
  131. )
  132. func TestError8(t *testing.T) {
  133. buff := bytes.NewBufferString("")
  134. recorder := httptest.NewRecorder()
  135. recorder.Body = buff
  136. o := Classic()
  137. o.ErrHandler = HandlerFunc(func(ctx *Context) {
  138. switch res := ctx.Result.(type) {
  139. case AbortError:
  140. ctx.WriteHeader(res.Code())
  141. ctx.Write([]byte(prefix))
  142. ctx.Write([]byte(res.Error()))
  143. case error:
  144. ctx.WriteHeader(http.StatusInternalServerError)
  145. ctx.Write([]byte(prefix))
  146. ctx.Write([]byte(res.Error()))
  147. default:
  148. ctx.WriteHeader(http.StatusInternalServerError)
  149. ctx.Write([]byte(prefix))
  150. ctx.Write([]byte(http.StatusText(http.StatusInternalServerError)))
  151. }
  152. ctx.Write([]byte(suffix))
  153. })
  154. o.Get("/", func() error {
  155. return NotFound()
  156. })
  157. req, err := http.NewRequest("HEAD", "http://localhost:8000/", nil)
  158. if err != nil {
  159. t.Error(err)
  160. }
  161. o.ServeHTTP(recorder, req)
  162. expect(t, recorder.Code, http.StatusNotFound)
  163. refute(t, len(buff.String()), 0)
  164. expect(t, buff.String(), prefix+"Not Found"+suffix)
  165. }