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.

292 line
7.2KB

  1. package rex
  2. import (
  3. "io"
  4. "net/http"
  5. "net/http/httptest"
  6. "os"
  7. "path"
  8. "testing"
  9. "github.com/goanywhere/env"
  10. mw "github.com/goanywhere/rex/middleware"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestConfigure(t *testing.T) {
  14. Convey("rex.configure", t, func() {
  15. app := New()
  16. app.configure()
  17. So(debug, ShouldBeTrue)
  18. So(port, ShouldEqual, 5000)
  19. env.Set("PORT", 9394)
  20. app.configure()
  21. So(port, ShouldEqual, 5000)
  22. })
  23. }
  24. func TestBuild(t *testing.T) {
  25. Convey("rex.build", t, func() {
  26. app := New()
  27. app.build()
  28. So(len(app.middleware.stack), ShouldEqual, 1)
  29. app.Use(mw.NoCache)
  30. So(len(app.middleware.stack), ShouldEqual, 2)
  31. })
  32. }
  33. func TestRegister(t *testing.T) {
  34. Convey("rex.register", t, func() {
  35. app := New()
  36. app.register("/login", func(w http.ResponseWriter, r *http.Request) {
  37. w.WriteHeader(http.StatusAccepted)
  38. w.Header().Set("X-Auth-Server", "rex")
  39. }, "POST")
  40. request, _ := http.NewRequest("POST", "/login", nil)
  41. response := httptest.NewRecorder()
  42. app.ServeHTTP(response, request)
  43. So(response.Code, ShouldEqual, http.StatusAccepted)
  44. So(response.Header().Get("X-Auth-Server"), ShouldEqual, "rex")
  45. })
  46. }
  47. func TestAny(t *testing.T) {
  48. app := New()
  49. app.Any("/", func(w http.ResponseWriter, r *http.Request) {
  50. w.Header().Set("X-Powered-By", "rex")
  51. w.Header().Set("Content-Type", "application/json")
  52. switch r.Method {
  53. case "GET":
  54. w.WriteHeader(http.StatusOK)
  55. case "POST":
  56. w.WriteHeader(http.StatusCreated)
  57. case "PUT":
  58. w.WriteHeader(http.StatusAccepted)
  59. case "DELETE":
  60. w.WriteHeader(http.StatusGone)
  61. default:
  62. w.Header().Set("X-HTTP-Method", r.Method)
  63. }
  64. })
  65. Convey("rex.Any", t, func() {
  66. var (
  67. request *http.Request
  68. response *httptest.ResponseRecorder
  69. )
  70. request, _ = http.NewRequest("GET", "/", nil)
  71. response = httptest.NewRecorder()
  72. app.ServeHTTP(response, request)
  73. So(response.Code, ShouldEqual, http.StatusOK)
  74. request, _ = http.NewRequest("POST", "/", nil)
  75. response = httptest.NewRecorder()
  76. app.ServeHTTP(response, request)
  77. So(response.Code, ShouldEqual, http.StatusCreated)
  78. request, _ = http.NewRequest("PUT", "/", nil)
  79. response = httptest.NewRecorder()
  80. app.ServeHTTP(response, request)
  81. So(response.Code, ShouldEqual, http.StatusAccepted)
  82. request, _ = http.NewRequest("DELETE", "/", nil)
  83. response = httptest.NewRecorder()
  84. app.ServeHTTP(response, request)
  85. So(response.Code, ShouldEqual, http.StatusGone)
  86. })
  87. }
  88. func TestGET(t *testing.T) {
  89. app := New()
  90. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  91. w.Header().Set("X-Powered-By", "rex")
  92. w.Header().Set("Content-Type", "application/json")
  93. })
  94. Convey("rex.GET", t, func() {
  95. request, _ := http.NewRequest("GET", "/", nil)
  96. response := httptest.NewRecorder()
  97. app.ServeHTTP(response, request)
  98. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  99. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  100. })
  101. }
  102. func TestPOST(t *testing.T) {
  103. app := New()
  104. app.POST("/", func(w http.ResponseWriter, r *http.Request) {
  105. w.Header().Set("X-Powered-By", "rex")
  106. w.Header().Set("Content-Type", "application/json")
  107. })
  108. Convey("rex.POST", t, func() {
  109. request, _ := http.NewRequest("POST", "/", nil)
  110. response := httptest.NewRecorder()
  111. app.ServeHTTP(response, request)
  112. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  113. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  114. })
  115. }
  116. func TestPUT(t *testing.T) {
  117. app := New()
  118. app.PUT("/", func(w http.ResponseWriter, r *http.Request) {
  119. w.Header().Set("X-Powered-By", "rex")
  120. w.Header().Set("Content-Type", "application/json")
  121. })
  122. Convey("rex.PUT", t, func() {
  123. request, _ := http.NewRequest("PUT", "/", nil)
  124. response := httptest.NewRecorder()
  125. app.ServeHTTP(response, request)
  126. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  127. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  128. })
  129. }
  130. func TestDELETE(t *testing.T) {
  131. app := New()
  132. app.DELETE("/", func(w http.ResponseWriter, r *http.Request) {
  133. w.Header().Set("X-Powered-By", "rex")
  134. w.Header().Set("Content-Type", "application/json")
  135. })
  136. Convey("rex.DELETE", t, func() {
  137. request, _ := http.NewRequest("DELETE", "/", nil)
  138. response := httptest.NewRecorder()
  139. app.ServeHTTP(response, request)
  140. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  141. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  142. })
  143. }
  144. func TestCONNECT(t *testing.T) {
  145. app := New()
  146. app.CONNECT("/", func(w http.ResponseWriter, r *http.Request) {
  147. w.Header().Set("X-Powered-By", "rex")
  148. })
  149. Convey("rex.CONNECT", t, func() {
  150. request, _ := http.NewRequest("CONNECT", "/", nil)
  151. response := httptest.NewRecorder()
  152. app.ServeHTTP(response, request)
  153. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  154. })
  155. }
  156. func TestTRACE(t *testing.T) {
  157. app := New()
  158. app.TRACE("/", func(w http.ResponseWriter, r *http.Request) {
  159. w.Header().Set("X-Powered-By", "rex")
  160. })
  161. Convey("rex.TRACE", t, func() {
  162. request, _ := http.NewRequest("TRACE", "/", nil)
  163. response := httptest.NewRecorder()
  164. app.ServeHTTP(response, request)
  165. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  166. })
  167. }
  168. func TestGroup(t *testing.T) {
  169. app := New()
  170. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  171. io.WriteString(w, "index")
  172. })
  173. user := app.Group("/users")
  174. user.GET("/", func(w http.ResponseWriter, r *http.Request) {
  175. w.Header().Set("X-Powered-By", "rex")
  176. })
  177. Convey("rex.Group", t, func() {
  178. request, _ := http.NewRequest("GET", "/users/", nil)
  179. response := httptest.NewRecorder()
  180. app.ServeHTTP(response, request)
  181. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  182. })
  183. }
  184. func TestFileServer(t *testing.T) {
  185. Convey("rex.FileServer", t, func() {
  186. var (
  187. prefix = "/assets/"
  188. filename = "logo.png"
  189. )
  190. tempdir := os.TempDir()
  191. filepath := path.Join(tempdir, filename)
  192. os.Create(filepath)
  193. defer os.Remove(filepath)
  194. app := New()
  195. app.FileServer(prefix, tempdir)
  196. request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
  197. response := httptest.NewRecorder()
  198. app.ServeHTTP(response, request)
  199. So(response.Code, ShouldEqual, http.StatusOK)
  200. filename = "index.html"
  201. request, _ = http.NewRequest("HEAD", prefix, nil)
  202. response = httptest.NewRecorder()
  203. app.ServeHTTP(response, request)
  204. So(response.Code, ShouldEqual, http.StatusOK)
  205. })
  206. }
  207. func TestUse(t *testing.T) {
  208. app := New()
  209. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  210. io.WriteString(w, "index")
  211. })
  212. app.Use(func(next http.Handler) http.Handler {
  213. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  214. w.Header().Set("Content-Type", "application/json")
  215. next.ServeHTTP(w, r)
  216. })
  217. })
  218. Convey("rex.Use", t, func() {
  219. request, _ := http.NewRequest("GET", "/", nil)
  220. response := httptest.NewRecorder()
  221. app.ServeHTTP(response, request)
  222. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  223. })
  224. }
  225. func TestVars(t *testing.T) {
  226. Convey("rex.Vars", t, func() {
  227. app := New()
  228. app.GET("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  229. vars := app.Vars(r)
  230. So(vars["id"], ShouldEqual, "123")
  231. })
  232. request, _ := http.NewRequest("GET", "/users/123", nil)
  233. response := httptest.NewRecorder()
  234. app.ServeHTTP(response, request)
  235. })
  236. }