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.

40 lines
954B

  1. package rex
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestMiddleware(t *testing.T) {
  9. env := func(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. w.Header().Set("X-Powered-By", "rex")
  12. next.ServeHTTP(w, r)
  13. })
  14. }
  15. json := func(next http.Handler) http.Handler {
  16. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. next.ServeHTTP(w, r)
  18. w.Header().Set("Content-Type", "application/json")
  19. })
  20. }
  21. mw := new(middleware)
  22. mw.stack = append(mw.stack, env)
  23. mw.stack = append(mw.stack, json)
  24. Convey("rex.middleware", t, func() {
  25. request, _ := http.NewRequest("GET", "/", nil)
  26. response := httptest.NewRecorder()
  27. mw.ServeHTTP(response, request)
  28. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  29. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  30. })
  31. }