Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

216 lines
5.5KB

  1. package rex
  2. import (
  3. "io"
  4. "net/http"
  5. "net/http/httptest"
  6. "os"
  7. "path"
  8. "testing"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestAny(t *testing.T) {
  12. app := New()
  13. app.Any("/", func(w http.ResponseWriter, r *http.Request) {
  14. w.Header().Set("X-Powered-By", "rex")
  15. w.Header().Set("Content-Type", "application/json")
  16. switch r.Method {
  17. case "GET":
  18. w.WriteHeader(http.StatusOK)
  19. case "POST":
  20. w.WriteHeader(http.StatusCreated)
  21. case "PUT":
  22. w.WriteHeader(http.StatusAccepted)
  23. case "DELETE":
  24. w.WriteHeader(http.StatusGone)
  25. default:
  26. w.Header().Set("X-HTTP-Method", r.Method)
  27. }
  28. })
  29. Convey("rex.Any", t, func() {
  30. var (
  31. request *http.Request
  32. response *httptest.ResponseRecorder
  33. )
  34. request, _ = http.NewRequest("GET", "/", nil)
  35. response = httptest.NewRecorder()
  36. app.ServeHTTP(response, request)
  37. So(response.Code, ShouldEqual, http.StatusOK)
  38. request, _ = http.NewRequest("POST", "/", nil)
  39. response = httptest.NewRecorder()
  40. app.ServeHTTP(response, request)
  41. So(response.Code, ShouldEqual, http.StatusCreated)
  42. request, _ = http.NewRequest("PUT", "/", nil)
  43. response = httptest.NewRecorder()
  44. app.ServeHTTP(response, request)
  45. So(response.Code, ShouldEqual, http.StatusAccepted)
  46. request, _ = http.NewRequest("DELETE", "/", nil)
  47. response = httptest.NewRecorder()
  48. app.ServeHTTP(response, request)
  49. So(response.Code, ShouldEqual, http.StatusGone)
  50. })
  51. }
  52. func TestGet(t *testing.T) {
  53. app := New()
  54. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  55. w.Header().Set("X-Powered-By", "rex")
  56. w.Header().Set("Content-Type", "application/json")
  57. })
  58. Convey("rex.Get", t, func() {
  59. request, _ := http.NewRequest("GET", "/", nil)
  60. response := httptest.NewRecorder()
  61. app.ServeHTTP(response, request)
  62. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  63. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  64. })
  65. }
  66. func TestPost(t *testing.T) {
  67. app := New()
  68. app.Post("/", func(w http.ResponseWriter, r *http.Request) {
  69. w.Header().Set("X-Powered-By", "rex")
  70. w.Header().Set("Content-Type", "application/json")
  71. })
  72. Convey("rex.Post", t, func() {
  73. request, _ := http.NewRequest("POST", "/", nil)
  74. response := httptest.NewRecorder()
  75. app.ServeHTTP(response, request)
  76. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  77. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  78. })
  79. }
  80. func TestPut(t *testing.T) {
  81. app := New()
  82. app.Put("/", func(w http.ResponseWriter, r *http.Request) {
  83. w.Header().Set("X-Powered-By", "rex")
  84. w.Header().Set("Content-Type", "application/json")
  85. })
  86. Convey("rex.Put", t, func() {
  87. request, _ := http.NewRequest("PUT", "/", nil)
  88. response := httptest.NewRecorder()
  89. app.ServeHTTP(response, request)
  90. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  91. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  92. })
  93. }
  94. func TestDelete(t *testing.T) {
  95. app := New()
  96. app.Delete("/", func(w http.ResponseWriter, r *http.Request) {
  97. w.Header().Set("X-Powered-By", "rex")
  98. w.Header().Set("Content-Type", "application/json")
  99. })
  100. Convey("rex.Delete", t, func() {
  101. request, _ := http.NewRequest("DELETE", "/", nil)
  102. response := httptest.NewRecorder()
  103. app.ServeHTTP(response, request)
  104. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  105. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  106. })
  107. }
  108. func TestGroup(t *testing.T) {
  109. app := New()
  110. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  111. io.WriteString(w, "index")
  112. })
  113. user := app.Group("/users")
  114. user.Get("/", func(w http.ResponseWriter, r *http.Request) {
  115. w.Header().Set("X-Powered-By", "rex")
  116. })
  117. Convey("rex.Group", t, func() {
  118. request, _ := http.NewRequest("GET", "/users/", nil)
  119. response := httptest.NewRecorder()
  120. app.ServeHTTP(response, request)
  121. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  122. })
  123. }
  124. func TestFileServer(t *testing.T) {
  125. Convey("rex.FileServer", t, func() {
  126. var (
  127. prefix = "/assets/"
  128. filename = "logo.png"
  129. )
  130. tempdir := os.TempDir()
  131. filepath := path.Join(tempdir, filename)
  132. os.Create(filepath)
  133. defer os.Remove(filepath)
  134. app := New()
  135. app.FileServer(prefix, tempdir)
  136. request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
  137. response := httptest.NewRecorder()
  138. app.ServeHTTP(response, request)
  139. So(response.Code, ShouldEqual, http.StatusOK)
  140. filename = "index.html"
  141. request, _ = http.NewRequest("HEAD", prefix, nil)
  142. response = httptest.NewRecorder()
  143. app.ServeHTTP(response, request)
  144. So(response.Code, ShouldEqual, http.StatusOK)
  145. })
  146. }
  147. func TestUse(t *testing.T) {
  148. app := New()
  149. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  150. io.WriteString(w, "index")
  151. })
  152. app.Use(func(next http.Handler) http.Handler {
  153. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  154. w.Header().Set("Content-Type", "application/json")
  155. next.ServeHTTP(w, r)
  156. })
  157. })
  158. Convey("rex.Use", t, func() {
  159. request, _ := http.NewRequest("GET", "/", nil)
  160. response := httptest.NewRecorder()
  161. app.ServeHTTP(response, request)
  162. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  163. })
  164. }
  165. func TestVars(t *testing.T) {
  166. Convey("rex.Vars", t, func() {
  167. app := New()
  168. app.Get("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  169. vars := app.Vars(r)
  170. So(vars["id"], ShouldEqual, "123")
  171. })
  172. request, _ := http.NewRequest("GET", "/users/123", nil)
  173. response := httptest.NewRecorder()
  174. app.ServeHTTP(response, request)
  175. })
  176. }