summaryrefslogtreecommitdiff
path: root/lib/vendor/excon/tests/middlewares/mock_tests.rb
blob: 49c25aae1492044b9c09d438c5e05dd96152d563 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
Shindo.tests('Excon stubs') do
  env_init

  tests("missing stub").raises(Excon::Errors::StubNotFound) do
    connection = Excon.new('http://127.0.0.1:9292', :mock => true)
    connection.request(:method => :get, :path => '/content-length/100')
  end

  tests("stub({})").raises(ArgumentError) do
    Excon.stub({})
  end

  tests("stub({}, {}) {}").raises(ArgumentError) do
    Excon.stub({}, {}) {}
  end

  tests("stub({:method => :get}, {:body => 'body', :status => 200})") do
    connection = nil
    response = nil

    tests('response.body').returns('body') do
      Excon.stub({:method => :get}, {:body => 'body', :status => 200})

      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      response = connection.request(:method => :get, :path => '/content-length/100')

      response.body
    end

    tests('response.headers').returns({}) do
      response.headers
    end

    tests('response.status').returns(200) do
      response.status
    end

    tests('response_block yields body').returns('body') do
      body = ''
      response_block = lambda do |chunk, remaining_bytes, total_bytes|
        body << chunk
      end
      connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
      body
    end

    tests('response.body empty with response_block').returns('') do
      response_block = lambda { |_, _, _| }
      connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block).body
    end

    Excon.stubs.clear

  end

  tests("stub({:path => %r{/tests/(\S+)}}, {:body => $1, :status => 200})") do
    connection = nil
    response = nil

    tests('response.body').returns('test') do
      Excon.stub({:path => %r{/tests/(\S+)}}) do |params|
        {
          :body => params[:captures][:path].first,
          :status => 200
        }
      end

      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      response = connection.request(:method => :get, :path => '/tests/test')

      response.body
    end

    tests('response.headers').returns({}) do
      response.headers
    end

    tests('response.status').returns(200) do
      response.status
    end

    Excon.stubs.clear

  end

  tests("stub({:body => 'body', :method => :get}) {|params| {:body => params[:body], :headers => params[:headers], :status => 200}}") do
    connection = nil
    response = nil

    tests('response.body').returns('body') do
      Excon.stub({:body => 'body', :method => :get}) {|params| {:body => params[:body], :headers => params[:headers], :status => 200}}

      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      response = connection.request(:body => 'body', :method => :get, :path => '/content-length/100')

      response.body
    end

    tests('response.headers').returns({'Host' => '127.0.0.1:9292', 'User-Agent' => "excon/#{Excon::VERSION}"}) do
      response.headers
    end

    tests('response.status').returns(200) do
      response.status
    end

    tests('response_block yields body').returns('body') do
      body = ''
      response_block = lambda do |chunk, remaining_bytes, total_bytes|
        body << chunk
      end
      connection.request(:body => 'body', :method => :get, :path => '/content-length/100', :response_block => response_block)
      body
    end

    tests('response.body empty with response_block').returns('') do
      response_block = lambda { |_, _, _| }
      connection.request(:body => 'body', :method => :get, :path => '/content-length/100', :response_block => response_block).body
    end

    Excon.stubs.clear

  end

  tests("stub({:body => File.open(...), :method => :get}, { :status => 200 })") do

    tests('response.status').returns(200) do
      file_path = File.join(File.dirname(__FILE__), '..', 'data', 'xs')

      Excon.stub(
        { :body => File.read(file_path), :method => :get },
        { :status => 200 }
      )

      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      response = connection.request(:body => File.open(file_path), :method => :get, :path => '/')

      response.status
    end

    Excon.stubs.clear

  end

  tests("invalid stub response").raises(Excon::Errors::InvalidStub) do
    Excon.stub({:body => 42, :method => :get}, {:status => 200})
    connection = Excon.new('http://127.0.0.1:9292', :mock => true)
    connection.request(:body => 42, :method => :get, :path => '/').status
  end

  tests("mismatched stub").raises(Excon::Errors::StubNotFound) do
    Excon.stub({:method => :post}, {:body => 'body'})
    Excon.get('http://127.0.0.1:9292/', :mock => true)
  end

  with_server('good') do
    tests('allow mismatched stub').returns(200) do
      Excon.stub({:path => '/echo/request_count'}, {:body => 'body'})
      Excon.get(
        'http://127.0.0.1:9292/echo/request',
        :mock => true,
        :allow_unstubbed_requests => true
      ).status
    end
  end

  Excon.stubs.clear

  tests("stub({}, {:body => 'x' * (Excon::DEFAULT_CHUNK_SIZE + 1)})") do

    test("response_block yields body") do
      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      Excon.stub({}, {:body => 'x' * (Excon::DEFAULT_CHUNK_SIZE + 1)})

      chunks = []
      response_block = lambda do |chunk, remaining_bytes, total_bytes|
        chunks << chunk
      end
      connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
      chunks == ['x' * Excon::DEFAULT_CHUNK_SIZE, 'x']
    end

    tests("response.body empty with response_block").returns('') do
      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      Excon.stub({}, {:body => 'x' * (Excon::DEFAULT_CHUNK_SIZE + 1)})
      response_block = lambda { |_, _, _| }
      connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block).body
    end

  end

  Excon.stubs.clear

  tests("stub({:url => 'https://user:pass@foo.bar.com:9999/baz?quux=true'}, {:status => 200})") do
    test("get(:expects => 200)") do
      Excon.stub({:url => 'https://user:pass@foo.bar.com:9999/baz?quux=true'}, {:status => 200})
      Excon.new("https://user:pass@foo.bar.com:9999/baz?quux=true", :mock => true).get(:expects => 200)
      true
    end
  end

  Excon.stubs.clear

  tests("stub({}, {:status => 404, :body => 'Not Found'}") do
    connection = nil

    tests("request(:expects => 200, :method => :get, :path => '/')").raises(Excon::Errors::NotFound) do
      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      Excon.stub({}, {:status => 404, :body => 'Not Found'})

      connection.request(:expects => 200, :method => :get, :path => '/')
    end

    tests("Expects exception should contain response object").returns(Excon::Response) do
      begin
        connection.request(:expects => 200, :method => :get, :path => '/')
      rescue Excon::Errors::NotFound => e
        e.response.class
      end
    end

    test("request(:expects => 200, :method => :get, :path => '/') with block does not invoke the block since it raises an error") do
      block_called = false
      begin
        response_block = lambda do |_,_,_|
          block_called = true
        end
        connection.request(:expects => 200, :method => :get, :path => '/', :response_block => response_block)
      rescue Excon::Errors::NotFound
      end
      !block_called
    end

    Excon.stubs.clear

  end

  tests("stub_for({})") do
    tests("stub_for({})").returns([{}, {}]) do
      Excon.new('http://127.0.0.1:9292', :mock => true)
      Excon.stub({}, {})

      Excon.stub_for({})
    end

    Excon.stubs.clear
  end

  tests("unstub({})") do
    connection = nil

    tests("unstub({})").returns([{}, {}]) do
      connection = Excon.new('http://127.0.0.1:9292', :mock => true)
      Excon.stub({}, {})

      Excon.unstub({})
    end

    tests("request(:method => :get)").raises(Excon::Errors::StubNotFound) do
      connection.request(:method => :get)
    end

    Excon.stubs.clear
  end

  tests("global stubs") do
    connection = Excon.new('http://127.0.0.1:9292', :mock => true)
    Excon.stub({}, {:body => '1'})
    t = Thread.new do
      Excon.stub({}, {:body => '2'})
      connection.request(:method => :get).body
    end
    tests("get on a different thread").returns('2') do
      t.join.value
    end
    tests("get on main thread").returns('2') do
      connection.request(:method => :get).body
    end
    Excon.stubs.clear
  end

  tests("thread-local stubs") do
    original_stubs_value = Excon.defaults[:stubs]
    Excon.defaults[:stubs] = :local

    connection = Excon.new('http://127.0.0.1:9292', :mock => true)
    Excon.stub({}, {:body => '1'})
    t = Thread.new do
      Excon.stub({}, {:body => '2'})
      connection.request(:method => :get).body
    end
    tests("get on a different thread").returns('2') do
      t.join.value
    end
    tests("get on main thread").returns('1') do
      connection.request(:method => :get).body
    end
    Excon.stubs.clear

    Excon.defaults[:stubs] = original_stubs_value
  end

  env_restore
end