Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client#build_request method to version 2 #620

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/savon/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/savon/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
73 changes: 73 additions & 0 deletions spec/savon/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<tns:authenticate><symbol>AAPL</symbol></tns:authenticate>')
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("<tns:authenticate><symbol>AAPL</symbol></tns:authenticate>")
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("<tns:authenticate ID=\"ABC321\"></tns:authenticate>")
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
Expand Down
9 changes: 9 additions & 0 deletions spec/savon/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<tns:VerifyAddress></tns:VerifyAddress>')
end
end

def with_multipart_mocked
multipart_response = Class.new { def initialize(*args); end }
multipart_mock = Module.new
Expand Down