Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

gzip_test.go 2.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  1. // Copyright 2013 Martini Authors
  2. // Copyright 2015 The Macaron Authors
  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 gzip
  16. import (
  17. "bufio"
  18. "net"
  19. "net/http"
  20. "net/http/httptest"
  21. "strings"
  22. "testing"
  23. . "github.com/smartystreets/goconvey/convey"
  24. "gopkg.in/macaron.v1"
  25. )
  26. func Test_Gzip(t *testing.T) {
  27. Convey("Gzip response content", t, func() {
  28. before := false
  29. m := macaron.New()
  30. m.Use(Gziper(Options{-10}))
  31. m.Use(func(r http.ResponseWriter) {
  32. r.(macaron.ResponseWriter).Before(func(rw macaron.ResponseWriter) {
  33. before = true
  34. })
  35. })
  36. m.Get("/", func() string { return "hello wolrd!" })
  37. // Not yet gzip.
  38. resp := httptest.NewRecorder()
  39. req, err := http.NewRequest("GET", "/", nil)
  40. So(err, ShouldBeNil)
  41. m.ServeHTTP(resp, req)
  42. _, ok := resp.Result().Header[_HEADER_CONTENT_ENCODING]
  43. So(ok, ShouldBeFalse)
  44. ce := resp.Header().Get(_HEADER_CONTENT_ENCODING)
  45. So(strings.EqualFold(ce, "gzip"), ShouldBeFalse)
  46. // Gzip now.
  47. resp = httptest.NewRecorder()
  48. req.Header.Set(_HEADER_ACCEPT_ENCODING, "gzip")
  49. m.ServeHTTP(resp, req)
  50. _, ok = resp.Result().Header[_HEADER_CONTENT_ENCODING]
  51. So(ok, ShouldBeTrue)
  52. ce = resp.Header().Get(_HEADER_CONTENT_ENCODING)
  53. So(strings.EqualFold(ce, "gzip"), ShouldBeTrue)
  54. So(before, ShouldBeTrue)
  55. })
  56. }
  57. type hijackableResponse struct {
  58. Hijacked bool
  59. header http.Header
  60. }
  61. func newHijackableResponse() *hijackableResponse {
  62. return &hijackableResponse{header: make(http.Header)}
  63. }
  64. func (h *hijackableResponse) Header() http.Header { return h.header }
  65. func (h *hijackableResponse) Write(buf []byte) (int, error) { return 0, nil }
  66. func (h *hijackableResponse) WriteHeader(code int) {}
  67. func (h *hijackableResponse) Flush() {}
  68. func (h *hijackableResponse) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  69. h.Hijacked = true
  70. return nil, nil, nil
  71. }
  72. func Test_ResponseWriter_Hijack(t *testing.T) {
  73. Convey("Hijack response", t, func() {
  74. hijackable := newHijackableResponse()
  75. m := macaron.New()
  76. m.Use(Gziper())
  77. m.Use(func(rw http.ResponseWriter) {
  78. hj, ok := rw.(http.Hijacker)
  79. So(ok, ShouldBeTrue)
  80. _, _, err := hj.Hijack()
  81. So(err, ShouldBeNil)
  82. })
  83. r, err := http.NewRequest("GET", "/", nil)
  84. So(err, ShouldBeNil)
  85. r.Header.Set(_HEADER_ACCEPT_ENCODING, "gzip")
  86. m.ServeHTTP(hijackable, r)
  87. So(hijackable.Hijacked, ShouldBeTrue)
  88. })
  89. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...