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.

62 lines
2.1KB

  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package rex
  24. import (
  25. "net/http"
  26. "net/http/httptest"
  27. "testing"
  28. . "github.com/smartystreets/goconvey/convey"
  29. )
  30. func TestMiddleware(t *testing.T) {
  31. env := func(next http.Handler) http.Handler {
  32. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  33. w.Header().Set("X-Powered-By", "rex")
  34. next.ServeHTTP(w, r)
  35. })
  36. }
  37. json := func(next http.Handler) http.Handler {
  38. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  39. next.ServeHTTP(w, r)
  40. w.Header().Set("Content-Type", "application/json")
  41. })
  42. }
  43. mw := new(middleware)
  44. mw.stack = append(mw.stack, env)
  45. mw.stack = append(mw.stack, json)
  46. Convey("rex.middleware", t, func() {
  47. request, _ := http.NewRequest("GET", "/", nil)
  48. response := httptest.NewRecorder()
  49. mw.ServeHTTP(response, request)
  50. So(response.Header().Get("X-Powered-By"), ShouldEqual, "rex")
  51. So(response.Header().Get("Content-Type"), ShouldEqual, "application/json")
  52. })
  53. }