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.

139 lines
3.5KB

  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 TestGet(t *testing.T) {
  10. app := New()
  11. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  12. w.Header().Set("X-Powered-By", "rex")
  13. w.Header().Set("Content-Type", "application/json")
  14. })
  15. Convey("rex.Get", t, func() {
  16. request, _ := http.NewRequest("GET", "/", nil)
  17. response := httptest.NewRecorder()
  18. app.ServeHTTP(response, request)
  19. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  20. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  21. })
  22. }
  23. func TestPost(t *testing.T) {
  24. app := New()
  25. app.Post("/", func(w http.ResponseWriter, r *http.Request) {
  26. w.Header().Set("X-Powered-By", "rex")
  27. w.Header().Set("Content-Type", "application/json")
  28. })
  29. Convey("rex.Post", t, func() {
  30. request, _ := http.NewRequest("POST", "/", nil)
  31. response := httptest.NewRecorder()
  32. app.ServeHTTP(response, request)
  33. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  34. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  35. })
  36. }
  37. func TestPut(t *testing.T) {
  38. app := New()
  39. app.Put("/", func(w http.ResponseWriter, r *http.Request) {
  40. w.Header().Set("X-Powered-By", "rex")
  41. w.Header().Set("Content-Type", "application/json")
  42. })
  43. Convey("rex.Put", t, func() {
  44. request, _ := http.NewRequest("PUT", "/", nil)
  45. response := httptest.NewRecorder()
  46. app.ServeHTTP(response, request)
  47. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  48. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  49. })
  50. }
  51. func TestDelete(t *testing.T) {
  52. app := New()
  53. app.Delete("/", func(w http.ResponseWriter, r *http.Request) {
  54. w.Header().Set("X-Powered-By", "rex")
  55. w.Header().Set("Content-Type", "application/json")
  56. })
  57. Convey("rex.Delete", t, func() {
  58. request, _ := http.NewRequest("DELETE", "/", nil)
  59. response := httptest.NewRecorder()
  60. app.ServeHTTP(response, request)
  61. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  62. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  63. })
  64. }
  65. func TestGroup(t *testing.T) {
  66. app := New()
  67. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  68. io.WriteString(w, "index")
  69. })
  70. user := app.Group("/users")
  71. user.Get("/", func(w http.ResponseWriter, r *http.Request) {
  72. w.Header().Set("X-Powered-By", "rex")
  73. })
  74. Convey("rex.Group", t, func() {
  75. request, _ := http.NewRequest("GET", "/users/", nil)
  76. response := httptest.NewRecorder()
  77. app.ServeHTTP(response, request)
  78. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  79. })
  80. }
  81. func TestUse(t *testing.T) {
  82. app := New()
  83. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  84. io.WriteString(w, "index")
  85. })
  86. app.Use(func(next http.Handler) http.Handler {
  87. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  88. w.Header().Set("Content-Type", "application/json")
  89. next.ServeHTTP(w, r)
  90. })
  91. })
  92. Convey("rex.Use", t, func() {
  93. request, _ := http.NewRequest("GET", "/", nil)
  94. response := httptest.NewRecorder()
  95. app.ServeHTTP(response, request)
  96. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  97. })
  98. }
  99. func TestVars(t *testing.T) {
  100. Convey("rex.Vars", t, func() {
  101. app := New()
  102. app.Get("/users/{id}", func(w http.ResponseWriter, r *http.Request) {
  103. vars := app.Vars(r)
  104. So(vars["id"], ShouldEqual, "123")
  105. })
  106. request, _ := http.NewRequest("GET", "/users/123", nil)
  107. response := httptest.NewRecorder()
  108. app.ServeHTTP(response, request)
  109. })
  110. }