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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
class MyException < StandardError; end;
class Responder
def call(request)
{body: request.body}
end
end
shared_context "declared responses" do |*adapter_info|
describe "when request stub declares that request should raise exception" do
it "should raise exception" do
stub_request(:get, "www.example.com").to_raise(MyException)
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "Exception from WebMock")
end
it "should raise exception if declared as and exception instance" do
stub_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "hello world")
end
it "should raise exception if declared as an exception instance" do
stub_request(:get, "www.example.com").to_raise("hello world")
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error("hello world")
end
it "should raise exception after returning declared successful response first" do
stub_request(:get, "www.example.com").to_return(body: "abc").then.to_raise(MyException)
expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "Exception from WebMock")
end
end
describe "when request stub declares that request should timeout" do
it "should timeout" do
stub_request(:get, "www.example.com").to_timeout
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(client_timeout_exception_class)
end
it "should timeout after returning declared successful response" do
stub_request(:get, "www.example.com").to_return(body: "abc").then.to_timeout
expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(client_timeout_exception_class)
end
end
describe "when request stub declares that request should return a response" do
it "should return response with declared body" do
stub_request(:get, "www.example.com").to_return(body: "abc")
expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
end
it "should return response with declared headers" do
stub_request(:get, "www.example.com").to_return(headers: SAMPLE_RESPONSE_HEADERS)
response = http_request(:get, "http://www.example.com/")
expect(response.headers["Content-Type"]).to eq("application/json")
unless adapter_info.include?(:no_content_length_header)
expect(response.headers["Content-Length"]).to eq("8888")
end
end
it "should return response with declared headers even if there are multiple headers with the same key" do
stub_request(:get, "www.example.com").to_return(headers: {"a" => ["b", "c"]})
response = http_request(:get, "http://www.example.com/")
expect(response.headers["A"]).to eq("b, c")
end
it "should return response with declared status code" do
stub_request(:get, "www.example.com").to_return(status: 500)
expect(http_request(:get, "http://www.example.com/").status).to eq("500")
end
it "should return response with declared status message", unless: (adapter_info.include?(:no_status_message)) do
stub_request(:get, "www.example.com").to_return(status: [500, "Internal Server Error"])
response = http_request(:get, "http://www.example.com/")
expect(response.message).to eq("Internal Server Error")
end
it "should return response with a default status code" do
stub_request(:get, "www.example.com")
expect(http_request(:get, "http://www.example.com/").status).to eq("200")
end
it "should return default response with empty message if response was not declared", unless: (adapter_info.include?(:no_status_message)) do
stub_request(:get, "www.example.com")
response = http_request(:get, "http://www.example.com/")
expect(response.message).to eq("")
end
describe "when response body was declared as IO" do
it "should return response body" do
stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
end
it "should return response body if requested many times" do
stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
2.times do
expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
end
end
it "should close IO after request" do
stub_request(:get, "www.example.com").to_return(body: @file = File.new(__FILE__))
expect(@file).to be_closed
end
end
describe "when response parts were declared as lambdas" do
it "should return evaluated response body" do
stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
end
it "should return evaluated response headers" do
stub_request(:post, "www.example.com").to_return(headers: lambda { |request| request.headers })
expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'B'}).headers['A']).to eq('B')
expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'C'}).headers['A']).to eq('C')
end
it "should evaluate response body for each request" do
stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
end
end
describe "when response was declared via to_return_json" do
describe "and declared response body is lamba" do
it "should evaluate the response body at the time of returning the response" do
record = double("SomeRecord")
allow(record).to receive_messages(to_json: '{"what":"something callable"}.')
stub_request(:get, "www.example.com").to_return_json(body: -> {record})
allow(record).to receive_messages(to_json: '{"what":"something else callable"}.')
expect(http_request(:get, "http://www.example.com/").body).to eq('{"what":"something else callable"}.')
end
it "should evaluate the response body with request passed as argument to lambda" do
stub_request(:get, "www.example.com").to_return_json(body: ->(request) {"\"#{request.uri.to_s}\""})
expect(http_request(:get, "http://www.example.com/").body).to eq("\"http://www.example.com:80/\"")
end
end
end
describe "when response was declared as lambda" do
it "should return evaluated response body" do
stub_request(:post, "www.example.com").to_return(lambda {|request|
{body: request.body}
})
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
end
it "should return evaluated response headers" do
stub_request(:get, "www.example.com").to_return(lambda { |request|
{headers: request.headers}
})
expect(http_request(:get, "http://www.example.com/", headers: {'A' => 'B'}).headers['A']).to eq('B')
end
it "should return dynamic response declared as a block" do
stub_request(:post, "www.example.com").to_return do |request|
{body: request.body}
end
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
end
it "should return dynamic response declared as an object responding to call" do
stub_request(:post, "www.example.com").to_return(Responder.new)
expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
end
end
describe "when response was declared as a file with a raw response" do
before(:each) do
@file = File.new(CURL_EXAMPLE_OUTPUT_PATH)
stub_request(:get, "www.example.com").to_return(@file)
@response = http_request(:get, "http://www.example.com/")
end
it "should return recorded headers" do
if adapter_info.include?(:no_content_length_header)
expect(@response.headers).to eq({
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
})
else
expect(@response.headers).to eq({
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
})
end
end
it "should return recorded body" do
expect(@response.body.size).to eq(419)
end
it "should return recorded status" do
expect(@response.status).to eq("202")
end
it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
expect(@response.message).to eq("OK")
end
it "should ensure file is closed" do
expect(@file).to be_closed
end
end
describe "when response was declared as a string with a raw response" do
before(:each) do
@input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
stub_request(:get, "www.example.com").to_return(@input)
@response = http_request(:get, "http://www.example.com/")
end
it "should return recorded headers" do
if adapter_info.include?(:no_content_length_header)
expect(@response.headers).to eq({
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
})
else
expect(@response.headers).to eq({
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
})
end
end
it "should return recorded body" do
expect(@response.body.size).to eq(419)
end
it "should return recorded status" do
expect(@response.status).to eq("202")
end
it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
expect(@response.message).to eq("OK")
end
end
describe "when response was declared as lambda evaluating to a string with a raw response" do
before(:each) do
@files = {
"www.example.com" => File.new(CURL_EXAMPLE_OUTPUT_PATH)
}
end
it "should return response from evaluated file" do
stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
end
it "should return response from evaluated string" do
stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
end
end
describe "when response is declared as an Rack app" do
it "should return response returned by the rack app" do
stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
expect(http_request(:post, 'http://www.example.com/greet', body: 'name=Jimmy').body).to eq('Good to meet you, Jimmy!')
end
it "should pass along the port number to the rack app" do
stub_request(:get, "http://www.example.com/compute").to_rack(MyRackApp)
expect(http_request(:get, "http://www.example.com/compute").status).to eq("200")
end
it "preserves content-type header when proxying to a rack app" do
stub_request(:any, //).to_rack(lambda {|req| [200, {}, ["OK"]] })
url = "https://google.com/hi/there"
headers = {
"Accept" => "application/json",
"Content-Type" => "application/json"
}
http_request(:get, url, headers: headers)
expect(WebMock).to have_requested(:get, url).with(headers: headers)
end
end
describe "when sequences of responses are declared" do
it "should return responses one by one if declared in array" do
stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"}, {body: "3"} ])
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
end
it "should repeat returning last declared response from a sequence after all responses were returned" do
stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"} ])
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
end
it "should return responses one by one if declared as comma separated params" do
stub_request(:get, "www.example.com").to_return({body: "1"}, {body: "2"}, {body: "3"})
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
end
it "should return responses one by one if declared with several to_return invokations" do
stub_request(:get, "www.example.com").
to_return({body: "1"}).
to_return({body: "2"}).
to_return({body: "3"})
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
end
it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
stub_request(:get, "www.example.com").to_return({body: "1"}).then.
to_return({body: "2"}).then.to_return({body: "3"})
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
end
end
describe "when responses are declared to return more than once" do
it "should repeat one response declared number of times" do
stub_request(:get, "www.example.com").
to_return({body: "1"}).times(2).
to_return({body: "2"})
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
end
it "should repeat sequence of response declared number of times" do
stub_request(:get, "www.example.com").
to_return({body: "1"}, {body: "2"}).times(2).
to_return({body: "3"})
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
expect(http_request(:get, "http://www.example.com/").body).to eq("3")
end
it "should repeat infinitely last response even if number of declared times is lower" do
stub_request(:get, "www.example.com").
to_return({body: "1"}).times(2)
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
expect(http_request(:get, "http://www.example.com/").body).to eq("1")
end
it "should give error if times is declared without specifying response" do
expect {
stub_request(:get, "www.example.com").times(3)
}.to raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
end
end
describe "when exception is declared to be raised more than once" do
it "should repeat raising exception declared number of times" do
stub_request(:get, "www.example.com").
to_raise(MyException).times(2).
to_return({body: "2"})
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "Exception from WebMock")
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "Exception from WebMock")
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
end
it "should repeat raising sequence of exceptions declared number of times" do
stub_request(:get, "www.example.com").
to_raise(MyException, ArgumentError).times(2).
to_return({body: "2"})
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "Exception from WebMock")
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(ArgumentError)
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(MyException, "Exception from WebMock")
expect {
http_request(:get, "http://www.example.com/")
}.to raise_error(ArgumentError)
expect(http_request(:get, "http://www.example.com/").body).to eq("2")
end
end
end
end
|