gzip_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2013 Martini Authors
  2. // Copyright 2014 Unknwon
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package macaron
  16. import (
  17. "net/http"
  18. "net/http/httptest"
  19. "strings"
  20. "testing"
  21. . "github.com/smartystreets/goconvey/convey"
  22. )
  23. func Test_Gzip(t *testing.T) {
  24. Convey("Gzip response content", t, func() {
  25. before := false
  26. m := New()
  27. m.Use(Gziper())
  28. m.Use(func(r http.ResponseWriter) {
  29. r.(ResponseWriter).Before(func(rw ResponseWriter) {
  30. before = true
  31. })
  32. })
  33. m.Get("/", func() string { return "hello wolrd!" })
  34. // Not yet gzip.
  35. resp := httptest.NewRecorder()
  36. req, err := http.NewRequest("GET", "/", nil)
  37. So(err, ShouldBeNil)
  38. m.ServeHTTP(resp, req)
  39. _, ok := resp.HeaderMap[HeaderContentEncoding]
  40. So(ok, ShouldBeFalse)
  41. ce := resp.Header().Get(HeaderContentEncoding)
  42. So(strings.EqualFold(ce, "gzip"), ShouldBeFalse)
  43. // Gzip now.
  44. resp = httptest.NewRecorder()
  45. req.Header.Set(HeaderAcceptEncoding, "gzip")
  46. m.ServeHTTP(resp, req)
  47. _, ok = resp.HeaderMap[HeaderContentEncoding]
  48. So(ok, ShouldBeTrue)
  49. ce = resp.Header().Get(HeaderContentEncoding)
  50. So(strings.EqualFold(ce, "gzip"), ShouldBeTrue)
  51. So(before, ShouldBeTrue)
  52. })
  53. }