From 0b51aa74a4792a6106be314f31b8f7e6cf968ed7 Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Mon, 21 Oct 2019 23:10:13 +0200 Subject: [PATCH] Improve client stubs in specs Make them closer to reality. Related to: https://github.com/httprb/http/pull/546 --- spec/lib/http/client_spec.rb | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index 3ce51175..eb4eb1e2 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -10,7 +10,8 @@ StubbedClient = Class.new(HTTP::Client) do def perform(request, options) - stubs.fetch(request.uri) { super(request, options) } + stubbed = stubs[request.uri] + stubbed ? stubbed.call(request) : super(request, options) end def stubs @@ -27,22 +28,26 @@ def stub(stubs) end def redirect_response(location, status = 302) - HTTP::Response.new( - :status => status, - :version => "1.1", - :headers => {"Location" => location}, - :body => "", - :request => HTTP::Request.new(:verb => :get, :uri => "http://example.com") - ) + lambda do |request| + HTTP::Response.new( + :status => status, + :version => "1.1", + :headers => {"Location" => location}, + :body => "", + :request => request + ) + end end def simple_response(body, status = 200) - HTTP::Response.new( - :status => status, - :version => "1.1", - :body => body, - :request => HTTP::Request.new(:verb => :get, :uri => "http://example.com") - ) + lambda do |request| + HTTP::Response.new( + :status => status, + :version => "1.1", + :body => body, + :request => request + ) + end end describe "following redirects" do