From 61e205f454899c565ed817ea232abd78c48fb54f Mon Sep 17 00:00:00 2001 From: JP Moral Date: Tue, 9 Sep 2014 15:00:01 +0800 Subject: [PATCH 1/2] Add Operation#request method Returns the request without making an actual call --- lib/savon/operation.rb | 5 +++++ spec/savon/operation_spec.rb | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/savon/operation.rb b/lib/savon/operation.rb index 760ff686..959894e8 100644 --- a/lib/savon/operation.rb +++ b/lib/savon/operation.rb @@ -55,6 +55,11 @@ def call(locals = {}, &block) create_response(response) end + def request(locals = {}, &block) + builder = build(locals, &block) + build_request(builder) + end + private def create_response(response) diff --git a/spec/savon/operation_spec.rb b/spec/savon/operation_spec.rb index 88e595d3..e770d2eb 100644 --- a/spec/savon/operation_spec.rb +++ b/spec/savon/operation_spec.rb @@ -192,6 +192,15 @@ def new_operation(operation_name, wsdl, globals) end end + describe "#request" do + it "returns the request" do + operation = new_operation(:verify_address, wsdl, globals) + request = operation.request + + expect(request.body).to include('') + end + end + def with_multipart_mocked multipart_response = Class.new { def initialize(*args); end } multipart_mock = Module.new From 649965ddd5fdcd2ba550c8e9edf67d967d5113b9 Mon Sep 17 00:00:00 2001 From: JP Moral Date: Tue, 9 Sep 2014 15:28:01 +0800 Subject: [PATCH 2/2] Add Client#build_request method Returns request without making an actual call --- lib/savon/client.rb | 4 +++ spec/savon/client_spec.rb | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/lib/savon/client.rb b/lib/savon/client.rb index 970c9c1a..49f0e113 100644 --- a/lib/savon/client.rb +++ b/lib/savon/client.rb @@ -41,6 +41,10 @@ def service_name @wsdl.service_name end + def build_request(operation_name, locals = {}, &block) + operation(operation_name).request(locals, &block) + end + private def set_globals(globals, block) diff --git a/spec/savon/client_spec.rb b/spec/savon/client_spec.rb index 330efff5..0fbea4fa 100644 --- a/spec/savon/client_spec.rb +++ b/spec/savon/client_spec.rb @@ -170,6 +170,79 @@ end end + describe "#build_request" do + it "returns the request without making an actual call" do + expected_request = mock('request') + wsdl = Wasabi::Document.new('http://example.com') + + operation = Savon::Operation.new( + :authenticate, + wsdl, + Savon::GlobalOptions.new + ) + operation.expects(:request).returns(expected_request) + + Savon::Operation.expects(:create).with( + :authenticate, + instance_of(Wasabi::Document), + instance_of(Savon::GlobalOptions) + ).returns(operation) + + operation.expects(:call).never + + client = new_client(:endpoint => @server.url(:repeat)) + request = client.build_request(:authenticate) do + message(:symbol => "AAPL" ) + end + + expect(request).to eq expected_request + end + + it "accepts a block without arguments" do + client = new_client(:endpoint => @server.url(:repeat)) + request = client.build_request(:authenticate) do + message(:symbol => "AAPL" ) + end + + expect(request.body). + to include('AAPL') + end + + it "accepts a block with one argument" do + client = new_client(:endpoint => @server.url(:repeat)) + + # supports instance variables! + @instance_variable = { :symbol => "AAPL" } + + request = client.build_request(:authenticate) do |locals| + locals.message(@instance_variable) + end + + expect(request.body). + to include("AAPL") + end + + it "accepts argument for the message tag" do + client = new_client(:endpoint => @server.url(:repeat)) + request = client.build_request(:authenticate, :attributes => { "ID" => "ABC321" }) + + expect(request.body). + to include("") + end + + it "raises when the operation name is not a symbol" do + expect { new_client.build_request("not a symbol") }.to raise_error + end + + it "raises when given an unknown option via the Hash syntax" do + expect { new_client.build_request(:authenticate, :invalid_local_option => true) }.to raise_error + end + + it "raises when given an unknown option via the block syntax" do + expect { new_client.build_request(:authenticate) { another_invalid_local_option true } }.to raise_error + end + end + def new_http_response(options = {}) defaults = { :code => 200, :headers => {}, :body => Fixture.response(:authentication) } response = defaults.merge options