Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

server_test.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. }