您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

187 行
4.8KB

  1. package rex
  2. import (
  3. "io"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAny(t *testing.T) {
  10. app := New()
  11. app.Any("/", func(w http.ResponseWriter, r *http.Request) {
  12. w.Header().Set("X-Powered-By", "rex")
  13. w.Header().Set("Content-Type", "application/json")
  14. switch r.Method {
  15. case "GET":
  16. w.WriteHeader(http.StatusOK)
  17. case "POST":
  18. w.WriteHeader(http.StatusCreated)
  19. case "PUT":
  20. w.WriteHeader(http.StatusAccepted)
  21. case "DELETE":
  22. w.WriteHeader(http.StatusGone)
  23. default:
  24. w.Header().Set("X-HTTP-Method", r.Method)
  25. }
  26. })
  27. Convey("rex.Any", t, func() {
  28. var (
  29. request *http.Request
  30. response *httptest.ResponseRecorder
  31. )
  32. request, _ = http.NewRequest("GET", "/", nil)
  33. response = httptest.NewRecorder()
  34. app.ServeHTTP(response, request)
  35. So(response.Code, ShouldEqual, http.StatusOK)
  36. request, _ = http.NewRequest("POST", "/", nil)
  37. response = httptest.NewRecorder()
  38. app.ServeHTTP(response, request)
  39. So(response.Code, ShouldEqual, http.StatusCreated)
  40. request, _ = http.NewRequest("PUT", "/", nil)
  41. response = httptest.NewRecorder()
  42. app.ServeHTTP(response, request)
  43. So(response.Code, ShouldEqual, http.StatusAccepted)
  44. request, _ = http.NewRequest("DELETE", "/", nil)
  45. response = httptest.NewRecorder()
  46. app.ServeHTTP(response, request)
  47. So(response.Code, ShouldEqual, http.StatusGone)
  48. })
  49. }
  50. func TestGet(t *testing.T) {
  51. app := New()
  52. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  53. w.Header().Set("X-Powered-By", "rex")
  54. w.Header().Set("Content-Type", "application/json")
  55. })
  56. Convey("rex.Get", t, func() {
  57. request, _ := http.NewRequest("GET", "/", nil)
  58. response := httptest.NewRecorder()
  59. app.ServeHTTP(response, request)
  60. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  61. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  62. })
  63. }
  64. func TestPost(t *testing.T) {
  65. app := New()
  66. app.Post("/", func(w http.ResponseWriter, r *http.Request) {
  67. w.Header().Set("X-Powered-By", "rex")
  68. w.Header().Set("Content-Type", "application/json")
  69. })
  70. Convey("rex.Post", t, func() {
  71. request, _ := http.NewRequest("POST", "/", nil)
  72. response := httptest.NewRecorder()
  73. app.ServeHTTP(response, request)
  74. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  75. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  76. })
  77. }
  78. func TestPut(t *testing.T) {
  79. app := New()
  80. app.Put("/", func(w http.ResponseWriter, r *http.Request) {
  81. w.Header().Set("X-Powered-By", "rex")
  82. w.Header().Set("Content-Type", "application/json")
  83. })
  84. Convey("rex.Put", t, func() {
  85. request, _ := http.NewRequest("PUT", "/", nil)
  86. response := httptest.NewRecorder()
  87. app.ServeHTTP(response, request)
  88. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  89. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  90. })
  91. }
  92. func TestDelete(t *testing.T) {
  93. app := New()
  94. app.Delete("/", func(w http.ResponseWriter, r *http.Request) {
  95. w.Header().Set("X-Powered-By", "rex")
  96. w.Header().Set("Content-Type", "application/json")
  97. })
  98. Convey("rex.Delete", t, func() {
  99. request, _ := http.NewRequest("DELETE", "/", nil)
  100. response := httptest.NewRecorder()
  101. app.ServeHTTP(response, request)
  102. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  103. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  104. })
  105. }
  106. func TestGroup(t *testing.T) {
  107. app := New()
  108. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  109. io.WriteString(w, "index")
  110. })
  111. user := app.Group("/users")
  112. user.Get("/", func(w http.ResponseWriter, r *http.Request) {
  113. w.Header().Set("X-Powered-By", "rex")
  114. })
  115. Convey("rex.Group", t, func() {
  116. request, _ := http.NewRequest("GET", "/users/", nil)
  117. response := httptest.NewRecorder()
  118. app.ServeHTTP(response, request)
  119. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  120. })
  121. }
  122. func TestUse(t *testing.T) {
  123. app := New()
  124. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  125. io.WriteString(w, "index")
  126. })
  127. app.Use(func(next http.Handler) http.Handler {
  128. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  129. w.Header().Set("Content-Type", "application/json")
  130. next.ServeHTTP(w, r)
  131. })
  132. })
  133. Convey("rex.Use", t, func() {
  134. request, _ := http.NewRequest("GET", "/", nil)
  135. response := httptest.NewRecorder()
  136. app.ServeHTTP(response, request)
  137. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  138. })
  139. }
  140. func TestVars(t *testing.T) {
  141. Convey("rex.Vars", t, func() {
  142. app := New()
  143. app.Get("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  144. vars := app.Vars(r)
  145. So(vars["id"], ShouldEqual, "123")
  146. })
  147. request, _ := http.NewRequest("GET", "/users/123", nil)
  148. response := httptest.NewRecorder()
  149. app.ServeHTTP(response, request)
  150. })
  151. }