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.

306 line
7.6KB

  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 TestName(t *testing.T) {
  89. Convey("rex.Name", t, func() {
  90. app := New()
  91. app.GET("/login", func(w http.ResponseWriter, r *http.Request) {
  92. w.WriteHeader(http.StatusOK)
  93. })
  94. request, _ := http.NewRequest("GET", "/login", nil)
  95. response := httptest.NewRecorder()
  96. app.ServeHTTP(response, request)
  97. So(app.Name(request), ShouldEqual, "GET:/login")
  98. })
  99. }
  100. func TestGET(t *testing.T) {
  101. app := New()
  102. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  103. w.Header().Set("X-Powered-By", "rex")
  104. w.Header().Set("Content-Type", "application/json")
  105. })
  106. Convey("rex.GET", t, func() {
  107. request, _ := http.NewRequest("GET", "/", nil)
  108. response := httptest.NewRecorder()
  109. app.ServeHTTP(response, request)
  110. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  111. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  112. })
  113. }
  114. func TestPOST(t *testing.T) {
  115. app := New()
  116. app.POST("/", func(w http.ResponseWriter, r *http.Request) {
  117. w.Header().Set("X-Powered-By", "rex")
  118. w.Header().Set("Content-Type", "application/json")
  119. })
  120. Convey("rex.POST", t, func() {
  121. request, _ := http.NewRequest("POST", "/", nil)
  122. response := httptest.NewRecorder()
  123. app.ServeHTTP(response, request)
  124. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  125. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  126. })
  127. }
  128. func TestPUT(t *testing.T) {
  129. app := New()
  130. app.PUT("/", func(w http.ResponseWriter, r *http.Request) {
  131. w.Header().Set("X-Powered-By", "rex")
  132. w.Header().Set("Content-Type", "application/json")
  133. })
  134. Convey("rex.PUT", t, func() {
  135. request, _ := http.NewRequest("PUT", "/", nil)
  136. response := httptest.NewRecorder()
  137. app.ServeHTTP(response, request)
  138. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  139. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  140. })
  141. }
  142. func TestDELETE(t *testing.T) {
  143. app := New()
  144. app.DELETE("/", func(w http.ResponseWriter, r *http.Request) {
  145. w.Header().Set("X-Powered-By", "rex")
  146. w.Header().Set("Content-Type", "application/json")
  147. })
  148. Convey("rex.DELETE", t, func() {
  149. request, _ := http.NewRequest("DELETE", "/", nil)
  150. response := httptest.NewRecorder()
  151. app.ServeHTTP(response, request)
  152. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  153. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  154. })
  155. }
  156. func TestCONNECT(t *testing.T) {
  157. app := New()
  158. app.CONNECT("/", func(w http.ResponseWriter, r *http.Request) {
  159. w.Header().Set("X-Powered-By", "rex")
  160. })
  161. Convey("rex.CONNECT", t, func() {
  162. request, _ := http.NewRequest("CONNECT", "/", nil)
  163. response := httptest.NewRecorder()
  164. app.ServeHTTP(response, request)
  165. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  166. })
  167. }
  168. func TestTRACE(t *testing.T) {
  169. app := New()
  170. app.TRACE("/", func(w http.ResponseWriter, r *http.Request) {
  171. w.Header().Set("X-Powered-By", "rex")
  172. })
  173. Convey("rex.TRACE", t, func() {
  174. request, _ := http.NewRequest("TRACE", "/", nil)
  175. response := httptest.NewRecorder()
  176. app.ServeHTTP(response, request)
  177. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  178. })
  179. }
  180. func TestGroup(t *testing.T) {
  181. app := New()
  182. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  183. io.WriteString(w, "index")
  184. })
  185. user := app.Group("/users")
  186. user.GET("/", func(w http.ResponseWriter, r *http.Request) {
  187. w.Header().Set("X-Powered-By", "rex")
  188. })
  189. Convey("rex.Group", t, func() {
  190. request, _ := http.NewRequest("GET", "/users/", nil)
  191. response := httptest.NewRecorder()
  192. app.ServeHTTP(response, request)
  193. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  194. })
  195. }
  196. func TestFileServer(t *testing.T) {
  197. Convey("rex.FileServer", t, func() {
  198. var (
  199. prefix = "/assets/"
  200. filename = "logo.png"
  201. )
  202. tempdir := os.TempDir()
  203. filepath := path.Join(tempdir, filename)
  204. os.Create(filepath)
  205. defer os.Remove(filepath)
  206. app := New()
  207. app.FileServer(prefix, tempdir)
  208. request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
  209. response := httptest.NewRecorder()
  210. app.ServeHTTP(response, request)
  211. So(response.Code, ShouldEqual, http.StatusOK)
  212. filename = "index.html"
  213. request, _ = http.NewRequest("HEAD", prefix, nil)
  214. response = httptest.NewRecorder()
  215. app.ServeHTTP(response, request)
  216. So(response.Code, ShouldEqual, http.StatusOK)
  217. })
  218. }
  219. func TestUse(t *testing.T) {
  220. app := New()
  221. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  222. io.WriteString(w, "index")
  223. })
  224. app.Use(func(next http.Handler) http.Handler {
  225. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  226. w.Header().Set("Content-Type", "application/json")
  227. next.ServeHTTP(w, r)
  228. })
  229. })
  230. Convey("rex.Use", t, func() {
  231. request, _ := http.NewRequest("GET", "/", nil)
  232. response := httptest.NewRecorder()
  233. app.ServeHTTP(response, request)
  234. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  235. })
  236. }
  237. func TestVars(t *testing.T) {
  238. Convey("rex.Vars", t, func() {
  239. app := New()
  240. app.GET("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  241. vars := app.Vars(r)
  242. So(vars["id"], ShouldEqual, "123")
  243. })
  244. request, _ := http.NewRequest("GET", "/users/123", nil)
  245. response := httptest.NewRecorder()
  246. app.ServeHTTP(response, request)
  247. })
  248. }