File: decompress_tests.rb

package info (click to toggle)
ruby-excon 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,232 kB
  • sloc: ruby: 7,974; makefile: 5
file content (187 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Shindo.tests('Excon Decompress Middleware') do
  env_init

  with_server('good_ipv4') do
    before do
      @connection ||= Excon.new(
        'http://127.0.0.1:9292/echo/content-encoded',
        :method => :post,
        :body => 'hello world',
        :middlewares => Excon.defaults[:middlewares]
      )
    end

    tests('gzip') do
      resp = nil

      tests('response body decompressed').returns('hello world') do
        resp = @connection.request(
          :headers => { 'Accept-Encoding' => 'gzip, deflate;q=0' }
        )
        resp[:body]
      end

      tests('server sent content-encoding').returns('gzip') do
        resp[:headers]['Content-Encoding-Sent']
      end

      tests('removes processed encoding from header').returns('') do
        resp[:headers]['Content-Encoding']
      end

      tests('empty response body').returns('') do
        resp = @connection.request(:body => '')
        resp[:body]
      end
    end

    tests('deflate') do
      resp = nil

      tests('response body decompressed').returns('hello world') do
        resp = @connection.request(
          :headers => { 'Accept-Encoding' => 'gzip;q=0, deflate' }
        )
        resp[:body]
      end

      tests('server sent content-encoding').returns('deflate') do
        resp[:headers]['Content-Encoding-Sent']
      end

      tests('removes processed encoding from header').returns('') do
        resp[:headers]['Content-Encoding']
      end
    end

    tests('deflate-raw') do
      resp = nil

      tests('response body decompressed').returns('hello world') do
        resp = @connection.request(
          :headers => { 'Accept-Encoding' => 'deflate-raw' }
        )
        resp[:body]
      end

      tests('server sent content-encoding').returns('deflate') do
        resp[:headers]['Content-Encoding-Sent']
      end

      tests('removes processed encoding from header').returns('') do
        resp[:headers]['Content-Encoding']
      end
    end

    tests('with pre-encoding') do
      resp = nil

      tests('server sent content-encoding').returns('other, gzip') do
        resp = @connection.request(
          :headers => { 'Accept-Encoding' => 'gzip, deflate;q=0',
                        'Content-Encoding-Pre' => 'other' }
        )
        resp[:headers]['Content-Encoding-Sent']
      end

      tests('processed encoding removed from header').returns('other') do
        resp[:headers]['Content-Encoding']
      end

      tests('response body decompressed').returns('hello world') do
        resp[:body]
      end

    end

    tests('with post-encoding') do
      resp = nil

      tests('server sent content-encoding').returns('gzip, other') do
        resp = @connection.request(
          :headers => { 'Accept-Encoding' => 'gzip, deflate;q=0',
                        'Content-Encoding-Post' => 'other' }
        )
        resp[:headers]['Content-Encoding-Sent']
      end

      tests('unprocessed since last applied is unknown').returns('gzip, other') do
        resp[:headers]['Content-Encoding']
      end

      tests('response body still compressed').returns('hello world') do
        Zlib::GzipReader.new(StringIO.new(resp[:body])).read
      end

    end

    tests('with a :response_block') do
      captures = nil
      resp = nil

      tests('server sent content-encoding').returns('gzip') do
        captures = capture_response_block do |block|
          resp = @connection.request(
            :headers => { 'Accept-Encoding' => 'gzip'},
            :response_block => block
          )
        end
        resp[:headers]['Content-Encoding-Sent']
      end

      tests('unprocessed since :response_block was used').returns('gzip') do
        resp[:headers]['Content-Encoding']
      end

      tests(':response_block passed unprocessed data').returns('hello world') do
        body = captures.map {|capture| capture[0] }.join
        Zlib::GzipReader.new(StringIO.new(body)).read
      end

    end

    tests('adds Accept-Encoding if needed') do

      tests('without a :response_block').returns('deflate, gzip') do
        resp = Excon.post(
          'http://127.0.0.1:9292/echo/request',
          :body => 'hello world',
          :middlewares => Excon.defaults[:middlewares] +
                          [Excon::Middleware::Decompress]
        )
        request = Marshal.load(resp.body)
        request[:headers]['Accept-Encoding']
      end

      tests('with a :response_block').returns(nil) do
        captures = capture_response_block do |block|
          Excon.post(
            'http://127.0.0.1:9292/echo/request',
            :body => 'hello world',
            :response_block => block,
            :middlewares => Excon.defaults[:middlewares] +
                            [Excon::Middleware::Decompress]
          )
        end
        request = Marshal.load(captures.map {|capture| capture[0] }.join)
        request[:headers]['Accept-Encoding']
      end

    end

  end

  tests('empty content-encoding') do
    Excon.stub({ method: :get }, { body: 'body', headers: { 'Content-Encoding' => '' }, status: 200 })

    tests('succeeds').returns('body') do
      connection = Excon.new('http://example.com', mock: true)
      resp = connection.request(method: :get)
      resp[:body]
    end

    Excon.stubs.clear
  end

  env_restore
end