johnreitano-savon 0.7.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/CHANGELOG +124 -0
  2. data/README.textile +75 -0
  3. data/Rakefile +45 -0
  4. data/lib/savon/client.rb +84 -0
  5. data/lib/savon/core_ext/datetime.rb +8 -0
  6. data/lib/savon/core_ext/hash.rb +78 -0
  7. data/lib/savon/core_ext/net_http.rb +20 -0
  8. data/lib/savon/core_ext/object.rb +21 -0
  9. data/lib/savon/core_ext/string.rb +47 -0
  10. data/lib/savon/core_ext/symbol.rb +8 -0
  11. data/lib/savon/core_ext/uri.rb +10 -0
  12. data/lib/savon/core_ext.rb +3 -0
  13. data/lib/savon/request.rb +149 -0
  14. data/lib/savon/response.rb +99 -0
  15. data/lib/savon/soap.rb +176 -0
  16. data/lib/savon/wsdl.rb +122 -0
  17. data/lib/savon/wsse.rb +136 -0
  18. data/lib/savon.rb +34 -0
  19. data/spec/basic_spec_helper.rb +12 -0
  20. data/spec/endpoint_helper.rb +22 -0
  21. data/spec/fixtures/response/response_fixture.rb +36 -0
  22. data/spec/fixtures/response/xml/authentication.xml +14 -0
  23. data/spec/fixtures/response/xml/multi_ref.xml +39 -0
  24. data/spec/fixtures/response/xml/soap_fault.xml +8 -0
  25. data/spec/fixtures/response/xml/soap_fault12.xml +18 -0
  26. data/spec/fixtures/wsdl/wsdl_fixture.rb +37 -0
  27. data/spec/fixtures/wsdl/xml/authentication.xml +63 -0
  28. data/spec/fixtures/wsdl/xml/namespaced_actions.xml +307 -0
  29. data/spec/fixtures/wsdl/xml/no_namespace.xml +115 -0
  30. data/spec/http_stubs.rb +23 -0
  31. data/spec/integration/http_basic_auth_spec.rb +16 -0
  32. data/spec/integration/server.rb +51 -0
  33. data/spec/savon/client_spec.rb +77 -0
  34. data/spec/savon/core_ext/datetime_spec.rb +12 -0
  35. data/spec/savon/core_ext/hash_spec.rb +138 -0
  36. data/spec/savon/core_ext/net_http_spec.rb +38 -0
  37. data/spec/savon/core_ext/object_spec.rb +40 -0
  38. data/spec/savon/core_ext/string_spec.rb +68 -0
  39. data/spec/savon/core_ext/symbol_spec.rb +11 -0
  40. data/spec/savon/core_ext/uri_spec.rb +15 -0
  41. data/spec/savon/request_spec.rb +89 -0
  42. data/spec/savon/response_spec.rb +137 -0
  43. data/spec/savon/savon_spec.rb +23 -0
  44. data/spec/savon/soap_spec.rb +171 -0
  45. data/spec/savon/wsdl_spec.rb +84 -0
  46. data/spec/savon/wsse_spec.rb +132 -0
  47. data/spec/spec_helper.rb +5 -0
  48. metadata +218 -0
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Symbol do
4
+
5
+ describe "to_soap_key" do
6
+ it "converts the Symbol from snake_case to a lowerCamelCase String" do
7
+ :lower_camel_case.to_soap_key.should == "lowerCamelCase"
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe URI::HTTP do
4
+
5
+ describe "ssl?" do
6
+ it "returns true for https URI's" do
7
+ URI("https://example.com").ssl?.should be_true
8
+ end
9
+
10
+ it "returns false for non-https URI's" do
11
+ URI("http://example.com").ssl?.should be_false
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,89 @@
1
+ require "spec_helper"
2
+
3
+ describe Savon::Request do
4
+ before { @request = Savon::Request.new EndpointHelper.wsdl_endpoint }
5
+
6
+ it "contains the ContentType for each supported SOAP version" do
7
+ Savon::SOAPVersions.each do |soap_version|
8
+ Savon::Request::ContentType[soap_version].should be_a(String)
9
+ Savon::Request::ContentType[soap_version].should_not be_empty
10
+ end
11
+ end
12
+
13
+ # defaults to log request and response. disabled for spec execution
14
+
15
+ it "has both getter and setter for whether to log (global setting)" do
16
+ Savon::Request.log = true
17
+ Savon::Request.log?.should be_true
18
+ Savon::Request.log = false
19
+ Savon::Request.log?.should be_false
20
+ end
21
+
22
+ it "defaults to use a Logger instance for logging" do
23
+ Savon::Request.logger.should be_a(Logger)
24
+ end
25
+
26
+ it "has both getter and setter for the logger to use (global setting)" do
27
+ Savon::Request.logger = nil
28
+ Savon::Request.logger.should be_nil
29
+ Savon::Request.logger = Logger.new STDOUT
30
+ end
31
+
32
+ it "defaults to :debug for logging" do
33
+ Savon::Request.log_level.should == :debug
34
+ end
35
+
36
+ it "has both getter and setter for the log level to use (global setting)" do
37
+ Savon::Request.log_level = :info
38
+ Savon::Request.log_level.should == :info
39
+ Savon::Request.log_level = :debug
40
+ end
41
+
42
+ it "is initialized with a SOAP endpoint String" do
43
+ Savon::Request.new EndpointHelper.wsdl_endpoint
44
+ end
45
+
46
+ it "has a getter for the SOAP endpoint URI" do
47
+ @request.endpoint.should == URI(EndpointHelper.wsdl_endpoint)
48
+ end
49
+
50
+ it "should have a getter for the proxy URI" do
51
+ @request.proxy.should == URI("")
52
+ end
53
+
54
+ it "should have a getter for the HTTP headers which defaults to an empty Hash" do
55
+ @request.headers.should == {}
56
+ end
57
+
58
+ it "should have a setter for the HTTP headers" do
59
+ headers = { "some" => "thing" }
60
+
61
+ @request.headers = headers
62
+ @request.headers.should == headers
63
+ end
64
+
65
+ it "should return the Net::HTTP object" do
66
+ @request.http.should be_kind_of(Net::HTTP)
67
+ end
68
+
69
+ it "should have a method for setting HTTP basic auth credentials" do
70
+ @request.basic_auth "user", "password"
71
+ end
72
+
73
+ it "retrieves the WSDL document and returns the Net::HTTP response" do
74
+ wsdl_response = @request.wsdl
75
+
76
+ wsdl_response.should be_a(Net::HTTPResponse)
77
+ wsdl_response.body.should == WSDLFixture.authentication
78
+ end
79
+
80
+ it "executes a SOAP request and returns the Net::HTTP response" do
81
+ soap = Savon::SOAP.new
82
+ soap.endpoint = URI EndpointHelper.wsdl_endpoint
83
+ soap_response = @request.soap soap
84
+
85
+ soap_response.should be_a(Net::HTTPResponse)
86
+ soap_response.body.should == ResponseFixture.authentication
87
+ end
88
+
89
+ end
@@ -0,0 +1,137 @@
1
+ require "spec_helper"
2
+
3
+ describe Savon::Response do
4
+ before { @response = Savon::Response.new http_response_mock }
5
+
6
+ it "defaults to raises both Savon::SOAPFault and Savon::HTTPError" do
7
+ Savon::Response.raise_errors?.should be_true
8
+ end
9
+
10
+ it "has both getter and setter for whether to raise errors (global setting)" do
11
+ Savon::Response.raise_errors = false
12
+ Savon::Response.raise_errors?.should == false
13
+ Savon::Response.raise_errors = true
14
+ end
15
+
16
+ describe "initialize" do
17
+ it "expects a Net::HTTPResponse" do
18
+ Savon::Response.new http_response_mock
19
+ end
20
+
21
+ it "raises a Savon::SOAPFault in case of a SOAP fault" do
22
+ lambda { savon_response_with :soap_fault }.should raise_error(Savon::SOAPFault)
23
+ end
24
+
25
+ it "does not raise a Savon::SOAPFault in case the default is turned off" do
26
+ Savon::Response.raise_errors = false
27
+ savon_response_with :soap_fault
28
+ Savon::Response.raise_errors = true
29
+ end
30
+
31
+ it "raises a Savon::HTTPError in case of an HTTP error" do
32
+ lambda { savon_response_with :http_error }.should raise_error(Savon::HTTPError)
33
+ end
34
+
35
+ it "does not raise a Savon::HTTPError in case the default is turned off" do
36
+ Savon::Response.raise_errors = false
37
+ savon_response_with :http_error
38
+ Savon::Response.raise_errors = true
39
+ end
40
+ end
41
+
42
+ describe "soap_fault?" do
43
+ before { Savon::Response.raise_errors = false }
44
+
45
+ it "does not return true in case the response seems to be ok" do
46
+ @response.soap_fault?.should_not be_true
47
+ end
48
+
49
+ it "returns true in case of a SOAP fault" do
50
+ savon_response_with(:soap_fault).soap_fault?.should be_true
51
+ end
52
+
53
+ after { Savon::Response.raise_errors = true }
54
+ end
55
+
56
+ describe "soap_fault" do
57
+ before { Savon::Response.raise_errors = false }
58
+
59
+ it "returns the SOAP fault message in case of a SOAP fault" do
60
+ savon_response_with(:soap_fault).soap_fault.
61
+ should == "(soap:Server) Fault occurred while processing."
62
+ end
63
+
64
+ it "returns the SOAP fault message in case of a SOAP 1.2 fault" do
65
+ savon_response_with(:soap_fault12).soap_fault.
66
+ should == "(soap:Sender) Sender Timeout"
67
+ end
68
+
69
+ after { Savon::Response.raise_errors = true }
70
+ end
71
+
72
+ describe "http_error?" do
73
+ before { Savon::Response.raise_errors = false }
74
+
75
+ it "does not return true in case the response seems to be ok" do
76
+ @response.http_error?.should_not be_true
77
+ end
78
+
79
+ it "returns true in case of an HTTP error" do
80
+ savon_response_with(:http_error).http_error?.should be_true
81
+ end
82
+
83
+ after { Savon::Response.raise_errors = true }
84
+ end
85
+
86
+ describe "http_error" do
87
+ before { Savon::Response.raise_errors = false }
88
+
89
+ it "returns the HTTP error message in case of an HTTP error" do
90
+ savon_response_with(:http_error).http_error.should == "Not found (404)"
91
+ end
92
+
93
+ after { Savon::Response.raise_errors = true }
94
+ end
95
+
96
+ it "should return the SOAP response body as a Hash" do
97
+ @response.to_hash[:authenticate_response][:return].should ==
98
+ ResponseFixture.authentication(:to_hash)
99
+ end
100
+
101
+ it "should return a Hash for a SOAP multiRef response" do
102
+ @response = Savon::Response.new http_response_mock(200, ResponseFixture.multi_ref, "OK")
103
+
104
+ @response.to_hash[:list_response].should be_a(Hash)
105
+ @response.to_hash[:multi_ref].should be_an(Array)
106
+ end
107
+
108
+ it "should return the raw SOAP response body" do
109
+ @response.to_xml.should == ResponseFixture.authentication
110
+ @response.to_s.should == ResponseFixture.authentication
111
+ end
112
+
113
+ it "should return the Net::HTTP response object" do
114
+ @response.http.should be_a(Mocha::Mock)
115
+ @response.http.should respond_to(:code)
116
+ @response.http.should respond_to(:message)
117
+ @response.http.should respond_to(:body)
118
+ end
119
+
120
+ def savon_response_with(error_type)
121
+ mock = case error_type
122
+ when :soap_fault then http_response_mock 200, ResponseFixture.soap_fault
123
+ when :soap_fault12 then http_response_mock 200, ResponseFixture.soap_fault12
124
+ when :http_error then http_response_mock 404, "", "Not found"
125
+ end
126
+ Savon::Response.new mock
127
+ end
128
+
129
+ def http_response_mock(code = 200, body = nil, message = "OK")
130
+ body ||= ResponseFixture.authentication
131
+ mock = mock "Net::HTTPResponse"
132
+ mock.stubs :code => code.to_s, :message => message,
133
+ :content_type => "text/html", :body => body
134
+ mock
135
+ end
136
+
137
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe Savon do
4
+
5
+ it "contains an Array of supported SOAP versions" do
6
+ Savon::SOAPVersions.should be_an(Array)
7
+ Savon::SOAPVersions.should_not be_empty
8
+ end
9
+
10
+ it "contains the xs:dateTime format" do
11
+ Savon::SOAPDateTimeFormat.should be_a(String)
12
+ Savon::SOAPDateTimeFormat.should_not be_empty
13
+
14
+ DateTime.new(2012, 03, 22, 16, 22, 33).strftime(Savon::SOAPDateTimeFormat).
15
+ should == "2012-03-22T16:22:33"
16
+ end
17
+
18
+ it "contains a Regexp matching the xs:dateTime format" do
19
+ Savon::SOAPDateTimeRegexp.should be_a(Regexp)
20
+ (Savon::SOAPDateTimeRegexp === "2012-03-22T16:22:33").should be_true
21
+ end
22
+
23
+ end
@@ -0,0 +1,171 @@
1
+ require "spec_helper"
2
+
3
+ describe Savon::SOAP do
4
+ before do
5
+ @soap = Savon::SOAP.new
6
+ @soap.action = WSDLFixture.authentication(:operations)[:authenticate][:action]
7
+ end
8
+
9
+ it "contains the SOAP namespace for each supported SOAP version" do
10
+ Savon::SOAPVersions.each do |soap_version|
11
+ Savon::SOAP::SOAPNamespace[soap_version].should be_a(String)
12
+ Savon::SOAP::SOAPNamespace[soap_version].should_not be_empty
13
+ end
14
+ end
15
+
16
+ it "contains the Content-Types for each supported SOAP version" do
17
+ Savon::SOAPVersions.each do |soap_version|
18
+ Savon::SOAP::ContentType[soap_version].should be_a(String)
19
+ Savon::SOAP::ContentType[soap_version].should_not be_empty
20
+ end
21
+ end
22
+
23
+ it "defaults to SOAP 1.1" do
24
+ Savon::SOAP.version.should == 1
25
+ end
26
+
27
+ it "has both getter and setter for the SOAP version to use (global setting)" do
28
+ [2, 1].each do |soap_version|
29
+ Savon::SOAP.version = soap_version
30
+ Savon::SOAP.version.should == soap_version
31
+ end
32
+ end
33
+
34
+ it "has a setter for the Savon::WSSE" do
35
+ @soap.wsse = Savon::WSSE.new
36
+ end
37
+
38
+ it "is has both getter and setter for the SOAP action" do
39
+ @soap.action.should == WSDLFixture.authentication(:operations)[:authenticate][:action]
40
+
41
+ @soap.action = "someAction"
42
+ @soap.action.should == "someAction"
43
+ end
44
+
45
+ it "has a setter for the SOAP input" do
46
+ @soap.input = "FindUserRequest"
47
+ end
48
+
49
+ it "has both getter and setter for global SOAP headers" do
50
+ header = { "some" => "header" }
51
+ Savon::SOAP.header = header
52
+ Savon::SOAP.header.should == header
53
+
54
+ Savon::SOAP.header = {}
55
+ end
56
+
57
+ it "has both getter and setter for the SOAP header" do
58
+ @soap.header.should be_a(Hash)
59
+ @soap.header.should be_empty
60
+
61
+ @soap.header = { "specialAuthKey" => "secret" }
62
+ @soap.header.should == { "specialAuthKey" => "secret" }
63
+ end
64
+
65
+ it "has a getter for the SOAP body, expecting a Hash or an XML String" do
66
+ @soap.body = { :id => 666 }
67
+ @soap.body = "<id>666</id>"
68
+ end
69
+
70
+ it "has a setter for specifying a Hash of namespaces" do
71
+ namespaces = { "xmlns:env" => "http://example.com" }
72
+ @soap.namespaces = namespaces
73
+ @soap.namespaces.should == namespaces
74
+ end
75
+
76
+ describe "has a getter for namespaces" do
77
+ it "which defaults to include the SOAP 1.1 namespace" do
78
+ @soap.namespaces.should == { "xmlns:env" => Savon::SOAP::SOAPNamespace[1] }
79
+ end
80
+
81
+ it "which contains the SOAP 1.2 namespace if specified" do
82
+ @soap.version = 2
83
+ @soap.namespaces.should == { "xmlns:env" => Savon::SOAP::SOAPNamespace[2] }
84
+ end
85
+ end
86
+
87
+ it "has both getter and setter for global namespaces" do
88
+ namespaces = { "some" => "namespace" }
89
+ Savon::SOAP.namespaces = namespaces
90
+ Savon::SOAP.namespaces.should == namespaces
91
+
92
+ Savon::SOAP.namespaces = {}
93
+ end
94
+
95
+ it "has a convenience method for setting the 'xmlns:wsdl' namespace" do
96
+ @soap.namespaces.should == { "xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/" }
97
+
98
+ @soap.namespace = "http://example.com"
99
+ @soap.namespaces.should include("xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/")
100
+ @soap.namespaces.should include("xmlns:wsdl" => "http://example.com")
101
+ end
102
+
103
+ it "has both getter and setter for the SOAP endpoint" do
104
+ @soap.endpoint.should be_nil
105
+
106
+ soap_endpoint = URI EndpointHelper.soap_endpoint
107
+ @soap.endpoint = soap_endpoint
108
+ @soap.endpoint.should == soap_endpoint
109
+ end
110
+
111
+ it "has a getter for the SOAP version to use which defaults to SOAP 1.1" do
112
+ @soap.version.should == Savon::SOAP.version
113
+ end
114
+
115
+ it "has a setter for specifying the SOAP version to use" do
116
+ @soap.version = 2
117
+ @soap.version.should == 2
118
+ end
119
+
120
+ describe "to_xml" do
121
+ after { Savon::SOAP.version = 1 }
122
+
123
+ it "returns the XML for a SOAP request" do
124
+ @soap.namespaces["xmlns:wsdl"] = "http://v1_0.ws.auth.order.example.com/"
125
+ @soap.body = { :id => 666 }
126
+
127
+ @soap.to_xml.should include('xmlns:wsdl="http://v1_0.ws.auth.order.example.com/"')
128
+ @soap.to_xml.should include('xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"')
129
+ @soap.to_xml.should include('<wsdl:authenticate><id>666</id></wsdl:authenticate>')
130
+ end
131
+
132
+ it "caches the XML, returning the same Object every time" do
133
+ @soap.to_xml.object_id.should == @soap.to_xml.object_id
134
+ end
135
+
136
+ it "uses the SOAP namespace for the specified SOAP version" do
137
+ @soap.version = 2
138
+ @soap.to_xml.should include(Savon::SOAP::SOAPNamespace[2])
139
+ end
140
+
141
+ it "uses the SOAP namespace for the default SOAP version otherwise" do
142
+ Savon::SOAP.version = 2
143
+ @soap.to_xml.should include(Savon::SOAP::SOAPNamespace[2])
144
+ end
145
+
146
+ it "merges global and per request headers defined as Hashes" do
147
+ Savon::SOAP.header = { "API-KEY" => "secret", "SOME-KEY" => "something" }
148
+ @soap.header["SOME-KEY"] = "somethingelse"
149
+
150
+ @soap.to_xml.should include("<API-KEY>secret</API-KEY>")
151
+ @soap.to_xml.should include("<SOME-KEY>somethingelse</SOME-KEY>")
152
+ end
153
+
154
+ it "joins global and per request headers defined as Strings" do
155
+ Savon::SOAP.header = "<API-KEY>secret</API-KEY>"
156
+ @soap.header = "<SOME-KEY>somethingelse</SOME-KEY>"
157
+
158
+ @soap.to_xml.should include("<API-KEY>secret</API-KEY>")
159
+ @soap.to_xml.should include("<SOME-KEY>somethingelse</SOME-KEY>")
160
+ end
161
+
162
+ it "merges the global and per request namespaces" do
163
+ Savon::SOAP.namespaces = { "xmlns:wsdl" => "namespace", "xmlns:v1" => "v1namespace" }
164
+ @soap.namespaces["xmlns:v1"] = "newV1namespace"
165
+
166
+ @soap.to_xml.should include('xmlns:wsdl="namespace"')
167
+ @soap.to_xml.should include('xmlns:v1="newV1namespace"')
168
+ end
169
+ end
170
+
171
+ end