Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

373 lines
9.3KB

  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. app.register("/signup", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  41. w.WriteHeader(http.StatusAccepted)
  42. w.Header().Set("X-Auth-Server", "rex")
  43. }), "POST")
  44. request, _ := http.NewRequest("POST", "/login", nil)
  45. response := httptest.NewRecorder()
  46. app.ServeHTTP(response, request)
  47. So(response.Code, ShouldEqual, http.StatusAccepted)
  48. So(response.Header().Get("X-Auth-Server"), ShouldEqual, "rex")
  49. request, _ = http.NewRequest("POST", "/signup", nil)
  50. response = httptest.NewRecorder()
  51. app.ServeHTTP(response, request)
  52. So(response.Code, ShouldEqual, http.StatusAccepted)
  53. So(response.Header().Get("X-Auth-Server"), ShouldEqual, "rex")
  54. So(func() {
  55. app.register("/panic", nil)
  56. }, ShouldPanic)
  57. })
  58. }
  59. func TestAny(t *testing.T) {
  60. app := New()
  61. app.Any("/", func(w http.ResponseWriter, r *http.Request) {
  62. w.Header().Set("X-Powered-By", "rex")
  63. w.Header().Set("Content-Type", "application/json")
  64. switch r.Method {
  65. case "GET":
  66. w.WriteHeader(http.StatusOK)
  67. case "POST":
  68. w.WriteHeader(http.StatusCreated)
  69. case "PUT":
  70. w.WriteHeader(http.StatusAccepted)
  71. case "DELETE":
  72. w.WriteHeader(http.StatusGone)
  73. default:
  74. w.Header().Set("X-HTTP-Method", r.Method)
  75. }
  76. })
  77. Convey("rex.Any", t, func() {
  78. var (
  79. request *http.Request
  80. response *httptest.ResponseRecorder
  81. )
  82. request, _ = http.NewRequest("GET", "/", nil)
  83. response = httptest.NewRecorder()
  84. app.ServeHTTP(response, request)
  85. So(response.Code, ShouldEqual, http.StatusOK)
  86. request, _ = http.NewRequest("POST", "/", nil)
  87. response = httptest.NewRecorder()
  88. app.ServeHTTP(response, request)
  89. So(response.Code, ShouldEqual, http.StatusCreated)
  90. request, _ = http.NewRequest("PUT", "/", nil)
  91. response = httptest.NewRecorder()
  92. app.ServeHTTP(response, request)
  93. So(response.Code, ShouldEqual, http.StatusAccepted)
  94. request, _ = http.NewRequest("DELETE", "/", nil)
  95. response = httptest.NewRecorder()
  96. app.ServeHTTP(response, request)
  97. So(response.Code, ShouldEqual, http.StatusGone)
  98. })
  99. }
  100. func TestName(t *testing.T) {
  101. Convey("rex.Name", t, func() {
  102. app := New()
  103. app.Get("/login", func(w http.ResponseWriter, r *http.Request) {
  104. w.WriteHeader(http.StatusOK)
  105. })
  106. request, _ := http.NewRequest("GET", "/login", nil)
  107. response := httptest.NewRecorder()
  108. app.ServeHTTP(response, request)
  109. So(app.Name(request), ShouldEqual, "GET:/login")
  110. })
  111. }
  112. func TestGet(t *testing.T) {
  113. app := New()
  114. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  115. w.Header().Set("X-Powered-By", "rex")
  116. })
  117. Convey("rex.GET", t, func() {
  118. request, _ := http.NewRequest("GET", "/", nil)
  119. response := httptest.NewRecorder()
  120. app.ServeHTTP(response, request)
  121. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  122. })
  123. }
  124. func TestHead(t *testing.T) {
  125. app := New()
  126. app.Head("/", func(w http.ResponseWriter, r *http.Request) {
  127. w.Header().Set("X-Powered-By", "rex")
  128. })
  129. Convey("rex.HEAD", t, func() {
  130. request, _ := http.NewRequest("HEAD", "/", nil)
  131. response := httptest.NewRecorder()
  132. app.ServeHTTP(response, request)
  133. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  134. })
  135. }
  136. func TestOptions(t *testing.T) {
  137. app := New()
  138. app.Options("/", func(w http.ResponseWriter, r *http.Request) {
  139. w.Header().Set("X-Powered-By", "rex")
  140. })
  141. Convey("rex.OPTIONS", t, func() {
  142. request, _ := http.NewRequest("OPTIONS", "/", nil)
  143. response := httptest.NewRecorder()
  144. app.ServeHTTP(response, request)
  145. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  146. })
  147. }
  148. func TestPost(t *testing.T) {
  149. app := New()
  150. app.Post("/", func(w http.ResponseWriter, r *http.Request) {
  151. w.Header().Set("X-Powered-By", "rex")
  152. w.Header().Set("Content-Type", "application/json")
  153. })
  154. Convey("rex.POST", t, func() {
  155. request, _ := http.NewRequest("POST", "/", nil)
  156. response := httptest.NewRecorder()
  157. app.ServeHTTP(response, request)
  158. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  159. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  160. })
  161. }
  162. func TestPut(t *testing.T) {
  163. app := New()
  164. app.Put("/", func(w http.ResponseWriter, r *http.Request) {
  165. w.Header().Set("X-Powered-By", "rex")
  166. w.Header().Set("Content-Type", "application/json")
  167. })
  168. Convey("rex.PUT", t, func() {
  169. request, _ := http.NewRequest("PUT", "/", nil)
  170. response := httptest.NewRecorder()
  171. app.ServeHTTP(response, request)
  172. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  173. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  174. })
  175. }
  176. func TestDelete(t *testing.T) {
  177. app := New()
  178. app.Delete("/", func(w http.ResponseWriter, r *http.Request) {
  179. w.Header().Set("X-Powered-By", "rex")
  180. w.Header().Set("Content-Type", "application/json")
  181. })
  182. Convey("rex.DELETE", t, func() {
  183. request, _ := http.NewRequest("DELETE", "/", nil)
  184. response := httptest.NewRecorder()
  185. app.ServeHTTP(response, request)
  186. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  187. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  188. })
  189. }
  190. func TestConnect(t *testing.T) {
  191. app := New()
  192. app.Connect("/", func(w http.ResponseWriter, r *http.Request) {
  193. w.Header().Set("X-Powered-By", "rex")
  194. })
  195. Convey("rex.CONNECT", t, func() {
  196. request, _ := http.NewRequest("CONNECT", "/", nil)
  197. response := httptest.NewRecorder()
  198. app.ServeHTTP(response, request)
  199. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  200. })
  201. }
  202. func TestTrace(t *testing.T) {
  203. app := New()
  204. app.Trace("/", func(w http.ResponseWriter, r *http.Request) {
  205. w.Header().Set("X-Powered-By", "rex")
  206. })
  207. Convey("rex.TRACE", t, func() {
  208. request, _ := http.NewRequest("TRACE", "/", nil)
  209. response := httptest.NewRecorder()
  210. app.ServeHTTP(response, request)
  211. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  212. })
  213. }
  214. func TestGroup(t *testing.T) {
  215. app := New()
  216. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  217. io.WriteString(w, "index")
  218. })
  219. user := app.Group("/users")
  220. user.Get("/", func(w http.ResponseWriter, r *http.Request) {
  221. w.Header().Set("X-Powered-By", "rex")
  222. })
  223. Convey("rex.Group", t, func() {
  224. request, _ := http.NewRequest("GET", "/users/", nil)
  225. response := httptest.NewRecorder()
  226. app.ServeHTTP(response, request)
  227. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  228. })
  229. }
  230. func TestHost(t *testing.T) {
  231. app := New()
  232. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  233. w.Header().Set("X-Powered-By", "base")
  234. })
  235. user := app.Host("localhost")
  236. user.Get("/", func(w http.ResponseWriter, r *http.Request) {
  237. w.Header().Set("X-Powered-By", "rex")
  238. })
  239. Convey("rex.Host", t, func() {
  240. request, _ := http.NewRequest("GET", "http://localhost/", nil)
  241. response := httptest.NewRecorder()
  242. app.ServeHTTP(response, request)
  243. So(response.Header().Get("X-Powered-By"), ShouldEqual, "base")
  244. user.ServeHTTP(response, request)
  245. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  246. })
  247. }
  248. func TestFileServer(t *testing.T) {
  249. Convey("rex.FileServer", t, func() {
  250. var (
  251. prefix = "/assets/"
  252. filename = "logo.png"
  253. )
  254. tempdir := os.TempDir()
  255. filepath := path.Join(tempdir, filename)
  256. os.Create(filepath)
  257. defer os.Remove(filepath)
  258. app := New()
  259. app.FileServer(prefix, tempdir)
  260. request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
  261. response := httptest.NewRecorder()
  262. app.ServeHTTP(response, request)
  263. So(response.Code, ShouldEqual, http.StatusOK)
  264. filename = "index.html"
  265. request, _ = http.NewRequest("HEAD", prefix, nil)
  266. response = httptest.NewRecorder()
  267. app.ServeHTTP(response, request)
  268. So(response.Code, ShouldEqual, http.StatusOK)
  269. })
  270. }
  271. func TestUse(t *testing.T) {
  272. app := New()
  273. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  274. io.WriteString(w, "index")
  275. })
  276. app.Use(func(next http.Handler) http.Handler {
  277. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  278. w.Header().Set("Content-Type", "application/json")
  279. next.ServeHTTP(w, r)
  280. })
  281. })
  282. Convey("rex.Use", t, func() {
  283. request, _ := http.NewRequest("GET", "/", nil)
  284. response := httptest.NewRecorder()
  285. app.ServeHTTP(response, request)
  286. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  287. })
  288. }
  289. func TestVars(t *testing.T) {
  290. Convey("rex.Vars", t, func() {
  291. app := New()
  292. app.Get("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  293. vars := app.Vars(r)
  294. So(vars["id"], ShouldEqual, "123")
  295. })
  296. request, _ := http.NewRequest("GET", "/users/123", nil)
  297. response := httptest.NewRecorder()
  298. app.ServeHTTP(response, request)
  299. })
  300. }