From ea86f2b02148e67c5e2b7452151f578b58511951 Mon Sep 17 00:00:00 2001 From: Cyril Mougel Date: Fri, 16 Nov 2012 10:48:26 +0100 Subject: [PATCH] Allow more kind of object in mock With a mock you allow return a symbol or an Hash. But with our test we can want return some Savon::SOAP::Response or HTTPI::Response save by vcr for example. This commit allow to return this kind of Object. You can do something like : ```ruby resources = VCR.use_cassette('get_resources') do Savon.client(wsdl).request(:get_resouces) end savon.expects(:get_resources).returns(resources) ``` --- lib/savon/spec/mock.rb | 14 ++++++--- spec/savon/spec/mock_spec.rb | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/savon/spec/mock.rb b/lib/savon/spec/mock.rb index 5c098c2..9fffe74 100644 --- a/lib/savon/spec/mock.rb +++ b/lib/savon/spec/mock.rb @@ -46,7 +46,7 @@ def with(body = nil, &block) def returns(response = nil) http = case response when Symbol then { :body => Fixture[action, response] } - when Hash then response + else response end Savon.config.hooks.define(:spec_response, :soap_request) do |_, request| @@ -80,9 +80,15 @@ def action=(action) def respond_with(http = {}) defaults = { :code => 200, :headers => {}, :body => "" } - http = defaults.merge(http) - - HTTPI::Response.new(http[:code], http[:headers], http[:body]) + case http + when Hash + http = defaults.merge(http) + HTTPI::Response.new(http[:code], http[:headers], http[:body]) + when HTTPI::Response + http + when Savon::SOAP::Response + http.http + end end # Extracted from CoreExt of Savon 0.9.9 diff --git a/spec/savon/spec/mock_spec.rb b/spec/savon/spec/mock_spec.rb index 605491a..4fa4503 100644 --- a/spec/savon/spec/mock_spec.rb +++ b/spec/savon/spec/mock_spec.rb @@ -185,6 +185,62 @@ end + context "with a HTTPI::Response" do + + let(:response) do + client.request(:get_user) + end + + let(:http_response) do + HTTPI::Response.new(201, { "Set-Cookie" => "ID=1; Max-Age=3600;" }, "cookie" ) + end + + before do + savon.expects(:get_user).returns(http_response) + end + + it "returns the given response code" do + response.http.code.should == http_response.code + end + + it "returns the given response headers" do + response.http.headers.should == http_response.headers + end + + it "returns the given response body" do + response.http.body.should == http_response.body + end + + end + + context "with a Savon::SOAP::Response" do + + let(:response) do + client.request(:get_user) + end + + let(:savon_response) do + Savon::SOAP::Response.new(double(:config, :raise_errors => false), HTTPI::Response.new(201, { "Set-Cookie" => "ID=1; Max-Age=3600;" }, "cookie" )) + end + + before do + savon.expects(:get_user).returns(savon_response) + end + + it "returns the given response code" do + response.http.code.should == savon_response.http.code + end + + it "returns the given response headers" do + response.http.headers.should == savon_response.http.headers + end + + it "returns the given response body" do + response.http.body.should == savon_response.http.body + end + + end + end end