spec/savon/soap_spec.rb in savon-0.7.5 vs spec/savon/soap_spec.rb in savon-0.7.6

- old
+ new

@@ -1,207 +1,201 @@ require "spec_helper" describe Savon::SOAP do before do - @authenticate_operation = WSDLFixture.authentication(:operations)[:authenticate] - @soap = Savon::SOAP.new @authenticate_operation, EndpointHelper.soap_endpoint + @operation = WSDLFixture.authentication(:operations)[:authenticate] + @action, @input = @operation[:action], @operation[:input] + @soap = Savon::SOAP.new @action, @input, EndpointHelper.soap_endpoint end - it "contains the SOAP namespace for each supported SOAP version" do + it "should contain the SOAP namespace for each supported SOAP version" do Savon::SOAP::Versions.each do |soap_version| Savon::SOAP::Namespace[soap_version].should be_a(String) Savon::SOAP::Namespace[soap_version].should_not be_empty end end - it "contains the Content-Types for each supported SOAP version" do + it "should contain the Content-Types for each supported SOAP version" do Savon::SOAP::Versions.each do |soap_version| Savon::SOAP::ContentType[soap_version].should be_a(String) Savon::SOAP::ContentType[soap_version].should_not be_empty end end - it "contains an Array of supported SOAP versions" do + it "should contain an Array of supported SOAP versions" do Savon::SOAP::Versions.should be_an(Array) Savon::SOAP::Versions.should_not be_empty end - it "contains the xs:dateTime format" do + it "should contain the xs:dateTime format" do Savon::SOAP::DateTimeFormat.should be_a(String) Savon::SOAP::DateTimeFormat.should_not be_empty DateTime.new(2012, 03, 22, 16, 22, 33).strftime(Savon::SOAP::DateTimeFormat). should == "2012-03-22T16:22:33Z" end - it "contains a Regexp matching the xs:dateTime format" do + it "should contain a Regexp matching the xs:dateTime format" do Savon::SOAP::DateTimeRegexp.should be_a(Regexp) (Savon::SOAP::DateTimeRegexp === "2012-03-22T16:22:33").should be_true end - it "defaults to SOAP 1.1" do + it "should default to SOAP 1.1" do Savon::SOAP.version.should == 1 end - it "has both getter and setter for the SOAP version to use (global setting)" do - [2, 1].each do |soap_version| - Savon::SOAP.version = soap_version - Savon::SOAP.version.should == soap_version + describe "xml returned via to_xml" do + before do + @xml_declaration = '<?xml version="1.0" encoding="UTF-8"?>' + @namespace = { "xmlns:ns" => "http://example.com" } + @namespace_string = 'xmlns:ns="http://example.com"' + @namespaces = { "xmlns:ns" => "http://ns.example.com", "xmlns:ns2" => "http://ns2.example.com" } + + # reset to defaults + Savon::SOAP.version = 1 + Savon::SOAP.header = {} + Savon::SOAP.namespaces = {} end - end - it "has a setter for the Savon::WSSE" do - @soap.wsse = Savon::WSSE.new - end + it "should contain an xml declaration" do + @soap.to_xml.should include(@xml_declaration) + end - it "has both getter and setter for the SOAP action" do - @soap.action.should == @authenticate_operation[:action] + # namespaces - @soap.action = "someAction" - @soap.action.should == "someAction" - end + it "should contain the namespace for SOAP 1.1" do + @soap.to_xml.should include('xmlns:env="' + Savon::SOAP::Namespace[1] + '"') + end - it "has both getter and setter for the SOAP input" do - @soap.input.should == @authenticate_operation[:input] - - @soap.input = "whatever" - @soap.input.should == "whatever" - - args = "FindUserRequest", { "username" => "auser", "anotherAttr" => "someVal" } - @soap.input = *args - @soap.input.should == [*args] - end - - it "has both getter and setter for global SOAP headers" do - header = { "some" => "header" } - Savon::SOAP.header = header - Savon::SOAP.header.should == header - - Savon::SOAP.header = {} - end - - it "has both getter and setter for the SOAP header" do - @soap.header.should be_a(Hash) - @soap.header.should be_empty - - @soap.header = { "specialAuthKey" => "secret" } - @soap.header.should == { "specialAuthKey" => "secret" } - end - - it "has a getter for the SOAP body, expecting a Hash or an XML String" do - @soap.body = { :id => 666 } - @soap.body = "<id>666</id>" - end - - it "has a setter for specifying a Hash of namespaces" do - namespaces = { "xmlns:env" => "http://example.com" } - @soap.namespaces = namespaces - @soap.namespaces.should == namespaces - end - - describe "has a getter for namespaces" do - it "which defaults to include the SOAP 1.1 namespace" do - @soap.namespaces.should == { "xmlns:env" => Savon::SOAP::Namespace[1] } + it "should contain the namespace for SOAP 1.2 when defined globally" do + Savon::SOAP.version = 2 + @soap.to_xml.should include('xmlns:env="' + Savon::SOAP::Namespace[2] + '"') end - it "which contains the SOAP 1.2 namespace if specified" do + it "should contain the namespace for SOAP 1.2 when defined per request" do @soap.version = 2 - @soap.namespaces.should == { "xmlns:env" => Savon::SOAP::Namespace[2] } + @soap.to_xml.should include('xmlns:env="' + Savon::SOAP::Namespace[2] + '"') end - end - it "has both getter and setter for global namespaces" do - namespaces = { "some" => "namespace" } - Savon::SOAP.namespaces = namespaces - Savon::SOAP.namespaces.should == namespaces + it "should containg a xmlns:wsdl namespace defined via the :namespace shortcut method" do + @soap.namespace = "http://wsdl.example.com" + @soap.to_xml.should include('xmlns:wsdl="http://wsdl.example.com"') + end - Savon::SOAP.namespaces = {} - end + it "should accept custom namespaces when defined globally" do + Savon::SOAP.namespaces = @namespace + @soap.to_xml.should include("<env:Envelope " + @namespace_string) + end - it "has a convenience method for setting the 'xmlns:wsdl' namespace" do - @soap.namespaces.should == { "xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/" } + it "should accept custom namespaces when defined per request" do + @soap.namespaces = @namespace + @soap.to_xml.should include("<env:Envelope " + @namespace_string) + end - @soap.namespace = "http://example.com" - @soap.namespaces.should include("xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/") - @soap.namespaces.should include("xmlns:wsdl" => "http://example.com") - end + it "should merge global and per request namespaces" do + Savon::SOAP.namespaces = @namespaces + @soap.namespaces = @namespace + @soap.to_xml.should include( + 'xmlns:ns="http://example.com"', + 'xmlns:ns2="http://ns2.example.com"' + ) + end - it "has both getter and setter for the SOAP endpoint" do - soap_endpoint = URI EndpointHelper.soap_endpoint + # header - @soap.endpoint = soap_endpoint - @soap.endpoint.should == soap_endpoint - end + it "should not contain a header tag unless specified" do + @soap.to_xml.should_not include("<env:Header>") + end - it "has a getter for the SOAP version to use which defaults to SOAP 1.1" do - @soap.version.should == Savon::SOAP.version - end + it "should accept a custom (String) header defined globally" do + Savon::SOAP.header = "<key>value</key>" + @soap.to_xml.should include("<env:Header><key>value</key></env:Header>") + end - it "has a setter for specifying the SOAP version to use" do - @soap.version = 2 - @soap.version.should == 2 - end + it "should accept a custom (Hash) header defined globally" do + Savon::SOAP.header[:key] = "value" + @soap.to_xml.should include("<env:Header><key>value</key></env:Header>") + end - describe "to_xml" do - after { Savon::SOAP.version = 1 } + it "should accept a custom (String) header defined per request" do + @soap.header = "<key>value</key>" + @soap.to_xml.should include("<env:Header><key>value</key></env:Header>") + end - it "returns the XML for a SOAP request" do - @soap.namespaces["xmlns:wsdl"] = "http://v1_0.ws.auth.order.example.com/" - @soap.body = { :id => 666 } + it "should accept a custom (Hash) header defined per request" do + @soap.header[:key] = "value" + @soap.to_xml.should include("<env:Header><key>value</key></env:Header>") + end - xml = @soap.to_xml - xml.should include('xmlns:wsdl="http://v1_0.ws.auth.order.example.com/"') - xml.should include('xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"') - xml.should include('<wsdl:authenticate><id>666</id></wsdl:authenticate>') + it "should merge global and per request headers defined as Strings" do + Savon::SOAP.header = "<key2>other value</key2>" + @soap.header = "<key>value</key>" + @soap.to_xml.should include( + "<env:Header><key2>other value</key2><key>value</key></env:Header>" + ) end - it "does not include an empty header tag" do - @soap.to_xml.should_not include('env:Header') + it "should merge global and per request headers defined as Hashes" do + Savon::SOAP.header = { :key => "value", :key2 => "global value" } + @soap.header[:key2] = "request value" + @soap.to_xml.should include( + "<env:Header><key>value</key><key2>request value</key2></env:Header>" + ) end - - it "returns the appropriate XML for a SOAP Body's root node when parameters are present" do - @soap.input = "authenticate", { "protocol" => "tls", "version" => "1.2" } - @soap.body = { :id => 666 } - @soap.to_xml.should include('<wsdl:authenticate protocol="tls" version="1.2"><id>666</id></wsdl:authenticate>') + it "should use the :header method from a given WSSE object to include a WSSE header" do + wsse = "some compliant object" + wsse.stubs(:header).returns("<wsse>authentication</wsse>") + + @soap.wsse = wsse + @soap.to_xml.should include("<env:Header><wsse>authentication</wsse></env:Header>") end - it "caches the XML, returning the same Object every time" do - @soap.to_xml.object_id.should == @soap.to_xml.object_id + # input tag + + it "should contain a :wsdl namespaced input tag matching the :input property on instantiation" do + @soap = Savon::SOAP.new "someAction", "someInput", EndpointHelper.soap_endpoint + @soap.to_xml.should include('<wsdl:someInput>') end - it "uses the SOAP namespace for the specified SOAP version" do - @soap.version = 2 - @soap.to_xml.should include(Savon::SOAP::Namespace[2]) + it "should fall back to using the :action property whem :input is blank" do + @soap = Savon::SOAP.new "someAction", "", EndpointHelper.soap_endpoint + @soap.to_xml.should include('<wsdl:someAction>') end - it "uses the SOAP namespace for the default SOAP version otherwise" do - Savon::SOAP.version = 2 - @soap.to_xml.should include(Savon::SOAP::Namespace[2]) + it "should containg namespaces defined via an input tag Array containing the tag name and a Hash of namespaces" do + input = ["someInput", { "otherNs" => "http://otherns.example.com" }] + @soap = Savon::SOAP.new "someAction", input, EndpointHelper.soap_endpoint + @soap.to_xml.should include('<wsdl:someInput otherNs="http://otherns.example.com">') end - it "merges global and per request headers defined as Hashes" do - Savon::SOAP.header = { "API-KEY" => "secret", "SOME-KEY" => "something" } - @soap.header["SOME-KEY"] = "somethingelse" + # xml body - @soap.to_xml.should include("<API-KEY>secret</API-KEY>") - @soap.to_xml.should include("<SOME-KEY>somethingelse</SOME-KEY>") + it "should contain the SOAP body defined as a Hash" do + @soap.body = { :someTag => "some value" } + @soap.to_xml.should include("<someTag>some value</someTag>") end - it "joins global and per request headers defined as Strings" do - Savon::SOAP.header = "<API-KEY>secret</API-KEY>" - @soap.header = "<SOME-KEY>somethingelse</SOME-KEY>" + it "should contain the SOAP body defined as an Object responding to :to_s" do + @soap.body = "<someTag>some value</someTag>" + @soap.to_xml.should include(@soap.body) + end - @soap.to_xml.should include("<API-KEY>secret</API-KEY>") - @soap.to_xml.should include("<SOME-KEY>somethingelse</SOME-KEY>") + # xml + + it "should be a completely custom XML when specified" do + @soap.xml = "custom SOAP body" + @soap.to_xml.should == @soap.xml end - it "merges the global and per request namespaces" do - Savon::SOAP.namespaces = { "xmlns:wsdl" => "namespace", "xmlns:v1" => "v1namespace" } - @soap.namespaces["xmlns:v1"] = "newV1namespace" + # safety check - @soap.to_xml.should include('xmlns:wsdl="namespace"') - @soap.to_xml.should include('xmlns:v1="newV1namespace"') + it "should be a valid SOAP request" do + @soap.to_xml.should include( + @xml_declaration + + '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">' << + '<env:Body><wsdl:authenticate></wsdl:authenticate></env:Body>' << + '</env:Envelope>' + ) end end - -end +end \ No newline at end of file