Skip to content

Commit

Permalink
changed the way one can disable the wsdl
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Jan 2, 2010
1 parent 7abc1b9 commit 3d1d51c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 50 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
== UPCOMING
* Changed the way you can disable Savon::WSDL. More information at: http://wiki.github.com/rubiii/savon/wsdl
Instead of disabling the WSDL globally/per request via two different methods, you now simply append an
exclamation mark (!) to your SOAP call. For example: client.get_all_users!
The Wiki page will be updated to relect this change as soon as it's released.

== 0.6.8 (2010-01-01)
* Improved specifications for various kinds of WSDL documents.
* Added support for SOAP endpoints which are different than the WSDL endpoint of a service.
Expand Down
31 changes: 7 additions & 24 deletions lib/savon/client.rb
Expand Up @@ -6,37 +6,19 @@ module Savon
# with SOAP services and XML.
class Client

# Global setting of whether to use Savon::WSDL.
@@wsdl = true

# Sets the global setting of whether to use Savon::WSDL.
def self.wsdl=(wsdl)
@@wsdl = wsdl
end

# Returns the global setting of whether to use Savon::WSDL.
def self.wsdl?
@@wsdl
end

# Expects a SOAP +endpoint+ String. Also accepts an optional Hash of
# +options+ for specifying a proxy server and SSL client authentication.
def initialize(endpoint, options = {})
@request = Request.new endpoint, options
@wsdl = WSDL.new @request
end

# Accessor for Savon::WSDL.
attr_accessor :wsdl
# Returns the Savon::WSDL.
attr_reader :wsdl

# Returns the Savon::Request.
attr_reader :request

# Returns whether to use Savon::WSDL.
def wsdl?
self.class.wsdl? && @wsdl
end

# Returns +true+ for available methods and SOAP actions.
def respond_to?(method)
return true if @wsdl.respond_to? method
Expand All @@ -47,7 +29,8 @@ def respond_to?(method)

# Dispatches requests to SOAP actions matching a given +method+ name.
def method_missing(method, *args, &block) #:doc:
super if wsdl? && !@wsdl.respond_to?(method)
@wsdl.enabled = (method.to_s[-1,1] == "!") ? false : true
super if @wsdl.enabled? && !@wsdl.respond_to?(method)

setup_objects operation_from(method), &block
Response.new @request.soap(@soap)
Expand All @@ -56,13 +39,13 @@ def method_missing(method, *args, &block) #:doc:
# Returns a SOAP operation Hash containing the SOAP action and input
# for a given +method+.
def operation_from(method)
return @wsdl.operations[method] if wsdl?
return @wsdl.operations[method] if @wsdl.enabled?
{ :action => method.to_soap_key, :input => method.to_soap_key }
end

# Returns the SOAP endpoint.
def soap_endpoint
wsdl? ? @wsdl.soap_endpoint : @request.endpoint
@wsdl.enabled? ? @wsdl.soap_endpoint : @request.endpoint
end

# Expects a SOAP operation Hash and sets up Savon::SOAP and Savon::WSSE.
Expand All @@ -73,7 +56,7 @@ def setup_objects(operation, &block)

yield_objects &block if block

@soap.namespaces["xmlns:wsdl"] ||= @wsdl.namespace_uri if wsdl?
@soap.namespaces["xmlns:wsdl"] ||= @wsdl.namespace_uri if @wsdl.enabled?
@soap.wsse = @wsse
end

Expand Down
8 changes: 8 additions & 0 deletions lib/savon/wsdl.rb
Expand Up @@ -10,6 +10,14 @@ def initialize(request)
@request = request
end

# Sets whether to use the WSDL.
attr_writer :enabled

# Returns whether to use the WSDL. Defaults to +true+.
def enabled?
@enabled.nil? ? true : @enabled
end

# Returns the namespace URI of the WSDL.
def namespace_uri
@namespace_uri ||= stream.namespace_uri
Expand Down
35 changes: 9 additions & 26 deletions spec/savon/client_spec.rb
Expand Up @@ -8,7 +8,7 @@
end

it "accepts a proxy URI passed in via options" do
Savon::Client.new EndpointHelper.wsdl_endpoint, :proxy => 'http://proxy'
Savon::Client.new EndpointHelper.wsdl_endpoint, :proxy => "http://proxy"
end

it "accepts settings for SSL client authentication via options" do
Expand Down Expand Up @@ -41,42 +41,25 @@
@client.authenticate.should be_a(Savon::Response)
end

describe "disabling retrieving and parsing the WSDL document" do
it "can be done globally for every instance of Savon::Client" do
@client.wsdl?.should be_true
Savon::Client.wsdl = false
it "disables the WSDL when passed a method with an exclamation mark" do
@client.wsdl.enabled?.should be_true

expect_the_wsdl_to_be_disabled
@client.authenticate.should be_a(Savon::Response)

Savon::Client.wsdl = true
[:respond_to?, :operations, :namespace_uri, :soap_endpoint].each do |method|
Savon::WSDL.any_instance.expects(method).never
end
@client.authenticate!.should be_a(Savon::Response)

it "can be done per request" do
@client.wsdl = false

expect_the_wsdl_to_be_disabled
@client.authenticate.should be_a(Savon::Response)
end

def expect_the_wsdl_to_be_disabled
@client.wsdl?.should be_false
[:respond_to?, :operations, :namespace_uri, :soap_endpoint].each do |method|
Savon::WSDL.any_instance.expects(method).never
end
end
@client.wsdl.enabled?.should be_false
end

it "raises a Savon::SOAPFault in case of a SOAP fault" do
client = Savon::Client.new EndpointHelper.wsdl_endpoint(:soap_fault)
client.wsdl = false
lambda { client.authenticate }.should raise_error(Savon::SOAPFault)
lambda { client.authenticate! }.should raise_error(Savon::SOAPFault)
end

it "raises a Savon::HTTPError in case of an HTTP error" do
client = Savon::Client.new EndpointHelper.wsdl_endpoint(:http_error)
client.wsdl = false
lambda { client.authenticate }.should raise_error(Savon::HTTPError)
lambda { client.authenticate! }.should raise_error(Savon::HTTPError)
end

it "yields the SOAP object to a block when it expects one argument" do
Expand Down
4 changes: 4 additions & 0 deletions spec/savon/wsdl_spec.rb
Expand Up @@ -8,6 +8,10 @@
Savon::WSDL.new Savon::Request.new(EndpointHelper.wsdl_endpoint)
end

it "is enabled by default" do
@wsdl.enabled?.should be_true
end

it "has a getter for the namespace URI" do
@wsdl.namespace_uri.should == WSDLFixture.authentication(:namespace_uri)
end
Expand Down

0 comments on commit 3d1d51c

Please sign in to comment.