No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

server_test.go 8.7KB

hace 9 años
hace 9 años
hace 9 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 TestFileServer(t *testing.T) {
  231. Convey("rex.FileServer", t, func() {
  232. var (
  233. prefix = "/assets/"
  234. filename = "logo.png"
  235. )
  236. tempdir := os.TempDir()
  237. filepath := path.Join(tempdir, filename)
  238. os.Create(filepath)
  239. defer os.Remove(filepath)
  240. app := New()
  241. app.FileServer(prefix, tempdir)
  242. request, _ := http.NewRequest("GET", path.Join(prefix, filename), nil)
  243. response := httptest.NewRecorder()
  244. app.ServeHTTP(response, request)
  245. So(response.Code, ShouldEqual, http.StatusOK)
  246. filename = "index.html"
  247. request, _ = http.NewRequest("HEAD", prefix, nil)
  248. response = httptest.NewRecorder()
  249. app.ServeHTTP(response, request)
  250. So(response.Code, ShouldEqual, http.StatusOK)
  251. })
  252. }
  253. func TestUse(t *testing.T) {
  254. app := New()
  255. app.GET("/", func(w http.ResponseWriter, r *http.Request) {
  256. io.WriteString(w, "index")
  257. })
  258. app.Use(func(next http.Handler) http.Handler {
  259. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  260. w.Header().Set("Content-Type", "application/json")
  261. next.ServeHTTP(w, r)
  262. })
  263. })
  264. Convey("rex.Use", t, func() {
  265. request, _ := http.NewRequest("GET", "/", nil)
  266. response := httptest.NewRecorder()
  267. app.ServeHTTP(response, request)
  268. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  269. })
  270. }
  271. func TestVars(t *testing.T) {
  272. Convey("rex.Vars", t, func() {
  273. app := New()
  274. app.GET("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  275. vars := app.Vars(r)
  276. So(vars["id"], ShouldEqual, "123")
  277. })
  278. request, _ := http.NewRequest("GET", "/users/123", nil)
  279. response := httptest.NewRecorder()
  280. app.ServeHTTP(response, request)
  281. })
  282. }