savon 2.18.1 → 3.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require "nokogiri"
4
3
 
5
4
  module Savon
6
5
  class LogMessage
7
- MIME_HEADER_PREFIX = /\A\s*[A-Za-z0-9-]+:/
8
6
 
9
7
  def initialize(message, filters = [], pretty_print = false)
10
8
  @message = message
@@ -13,67 +11,20 @@ module Savon
13
11
  end
14
12
 
15
13
  def to_s
16
- message = @message
17
- begin
18
- message_is_xml = xml_start(message)
19
- rescue ArgumentError
20
- # The message encoding holds invalid bytes (e.g. a binary multipart
21
- # body mistagged as UTF-8): search the bytes instead of raising.
22
- message = message.b
23
- message_is_xml = xml_start(message)
24
- end
14
+ message_is_xml = @message =~ /^</
25
15
  has_filters = @filters.any?
26
16
  pretty_print = @pretty_print
27
17
 
28
18
  return @message unless message_is_xml
29
19
  return @message unless has_filters || pretty_print
30
20
 
31
- # The message may be multipart, cut off extra lines before open tag
32
- # and stop at the next MIME boundary, so the attachments that follow the
33
- # XML part do not make the document invalid.
34
- xml_end = message.index(/\r?\n--/, message_is_xml) || message.length
35
- document = Nokogiri.XML(message[message_is_xml...xml_end])
21
+ document = Nokogiri.XML(@message)
36
22
  document = apply_filter(document) if has_filters
37
23
  document.to_xml(nokogiri_options)
38
24
  end
39
25
 
40
26
  private
41
27
 
42
- # The index of the XML document in the message. Multipart messages have MIME
43
- # headers before the XML part, and these can contain '<' (Content-ID: <...>).
44
- # If the first '<' is on a header line, the document starts at the first '<'
45
- # after the next blank line. A '<' before any blank line that is not on a
46
- # header line belongs to the document itself (e.g. a declaration-less
47
- # envelope with blank lines inside it).
48
- #
49
- # A fixed set of searches, plus a loop that only advances forward past
50
- # nested header tags, so the search stays linear in the message size.
51
- # Log input is attacker-influenced on the response side, so logging must
52
- # never be a slow path.
53
- def xml_start(message)
54
- message.index("<?xml") || first_tag(message)
55
- end
56
-
57
- def first_tag(message)
58
- first = message.index("<")
59
- return first unless first && mime_header_line?(message, first)
60
-
61
- pos = first
62
- loop do
63
- blank_line = message.index(/\r?\n\r?\n/, pos)
64
- tag = blank_line && message.index("<", blank_line)
65
- return first unless tag
66
- return tag unless mime_header_line?(message, tag)
67
-
68
- pos = tag
69
- end
70
- end
71
-
72
- def mime_header_line?(message, index)
73
- line_start = (message.rindex("\n", index) || -1) + 1
74
- message[line_start...index].match?(MIME_HEADER_PREFIX)
75
- end
76
-
77
28
  def apply_filter(document)
78
29
  return document unless document.errors.empty?
79
30
 
@@ -95,7 +46,8 @@ module Savon
95
46
  end
96
47
 
97
48
  def nokogiri_options
98
- @pretty_print ? { indent: 2 } : { save_with: Nokogiri::XML::Node::SaveOptions::AS_XML }
49
+ @pretty_print ? { :indent => 2 } : { :save_with => Nokogiri::XML::Node::SaveOptions::AS_XML }
99
50
  end
51
+
100
52
  end
101
53
  end
data/lib/savon/message.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require "savon/qualified_message"
4
3
  require "gyoku"
5
4
 
6
5
  module Savon
7
6
  class Message
7
+
8
8
  def initialize(message_tag, namespace_identifier, types, used_namespaces, message, element_form_default, key_converter, unwrap)
9
9
  @message_tag = message_tag
10
10
  @namespace_identifier = namespace_identifier
@@ -18,33 +18,21 @@ module Savon
18
18
  end
19
19
 
20
20
  def to_s
21
- return @message.to_s unless @message.is_a? Hash
21
+ return @message.to_s unless @message.kind_of? Hash
22
22
 
23
23
  if @element_form_default == :qualified
24
24
  @message = QualifiedMessage.new(@types, @used_namespaces, @key_converter).to_hash(@message, [@message_tag.to_s])
25
- elsif schema_order?(@message)
26
- unqualified = QualifiedMessage.new(@types, @used_namespaces, @key_converter, qualify: false)
27
- @message = unqualified.to_hash(@message, [@message_tag.to_s])
28
25
  end
29
26
 
30
27
  gyoku_options = {
31
- element_form_default: @element_form_default,
32
- namespace: @namespace_identifier,
33
- key_converter: @key_converter,
34
- unwrap: @unwrap
28
+ :element_form_default => @element_form_default,
29
+ :namespace => @namespace_identifier,
30
+ :key_converter => @key_converter,
31
+ :unwrap => @unwrap
35
32
  }
36
33
 
37
34
  Gyoku.xml(@message, gyoku_options)
38
35
  end
39
36
 
40
- private
41
-
42
- def schema_order?(value)
43
- case value
44
- when Hash then QualifiedMessage.order_option(value) == :use_schema || value.each_value.any? { |nested| schema_order?(nested) }
45
- when Array then value.any? { |nested| schema_order?(nested) }
46
- else false
47
- end
48
- end
49
37
  end
50
38
  end
@@ -1,18 +1,11 @@
1
1
  # frozen_string_literal: true
2
-
3
- require "httpi"
4
- require "savon/transport/response"
2
+ require "faraday"
5
3
 
6
4
  module Savon
7
- # A single test expectation set up by Savon's mock interface.
8
- # One expectation covers one operation call in one test.
9
- #
10
- # Records the expected operation name and message, captures what was
11
- # actually called, and either returns a synthetic response or raises
12
- # an error on mismatch.
13
5
  class MockExpectation
6
+
14
7
  def initialize(operation_name)
15
- @expected = { operation_name: operation_name }
8
+ @expected = { :operation_name => operation_name }
16
9
  @actual = nil
17
10
  end
18
11
 
@@ -22,15 +15,15 @@ module Savon
22
15
  end
23
16
 
24
17
  def returns(response)
25
- response = { code: 200, headers: {}, body: response } if response.is_a?(String)
18
+ response = { :code => 200, :headers => {}, :body => response } if response.kind_of?(String)
26
19
  @response = response
27
20
  self
28
21
  end
29
22
 
30
- def actual(operation_name, _builder, _globals, locals)
23
+ def actual(operation_name, builder, globals, locals)
31
24
  @actual = {
32
- operation_name: operation_name,
33
- message: locals[:message]
25
+ :operation_name => operation_name,
26
+ :message => locals[:message]
34
27
  }
35
28
  end
36
29
 
@@ -44,49 +37,42 @@ module Savon
44
37
  verify_message!
45
38
  end
46
39
 
47
- # Builds and returns a Transport::Response from the configured response hash.
48
- #
49
- # @return [Transport::Response]
50
- # @raise [ExpectationError] if no response was configured for this expectation
51
40
  def response!
52
41
  unless @response
53
42
  raise ExpectationError, "This expectation was not set up with a response."
54
43
  end
55
-
56
- Transport::Response.new(@response[:code], @response[:headers], @response[:body])
44
+ env = Faraday::Env.from(status: @response[:code], response_headers: @response[:headers], response_body: @response[:body])
45
+ Faraday::Response.new(env)
57
46
  end
58
47
 
59
48
  private
60
49
 
61
50
  def verify_operation_name!
62
- return if @expected[:operation_name] == @actual[:operation_name]
63
-
64
- raise ExpectationError, "Expected a request to the #{@expected[:operation_name].inspect} operation.\n" \
65
- "Received a request to the #{@actual[:operation_name].inspect} operation instead."
51
+ unless @expected[:operation_name] == @actual[:operation_name]
52
+ raise ExpectationError, "Expected a request to the #{@expected[:operation_name].inspect} operation.\n" \
53
+ "Received a request to the #{@actual[:operation_name].inspect} operation instead."
54
+ end
66
55
  end
67
56
 
68
57
  def verify_message!
69
58
  return if @expected[:message].eql? :any
59
+ unless equals_except_any(@expected[:message], @actual[:message])
60
+ expected_message = " with this message: #{@expected[:message].inspect}" if @expected[:message]
61
+ expected_message ||= " with no message."
70
62
 
71
- return if equals_except_any(@expected[:message], @actual[:message])
63
+ actual_message = " with this message: #{@actual[:message].inspect}" if @actual[:message]
64
+ actual_message ||= " with no message."
72
65
 
73
- expected_message = " with this message: #{@expected[:message].inspect}" if @expected[:message]
74
- expected_message ||= " with no message."
75
-
76
- actual_message = " with this message: #{@actual[:message].inspect}" if @actual[:message]
77
- actual_message ||= " with no message."
78
-
79
- raise ExpectationError, "Expected a request to the #{@expected[:operation_name].inspect} operation\n#{expected_message}\n" \
80
- "Received a request to the #{@actual[:operation_name].inspect} operation\n#{actual_message}"
66
+ raise ExpectationError, "Expected a request to the #{@expected[:operation_name].inspect} operation\n#{expected_message}\n" \
67
+ "Received a request to the #{@actual[:operation_name].inspect} operation\n#{actual_message}"
68
+ end
81
69
  end
82
70
 
83
71
  def equals_except_any(msg_expected, msg_real)
84
- # === allows RSpec matchers (e.g. include(:key)) to be used as expected values
85
- return true if msg_expected === msg_real # rubocop:disable Style/CaseEquality
86
- return false if msg_expected.nil? || msg_real.nil? # If both are nil has returned true
87
-
72
+ return true if msg_expected === msg_real
73
+ return false if (msg_expected.nil? || msg_real.nil?) # If both are nil has returned true
88
74
  msg_expected.each do |key, expected_value|
89
- next if expected_value == :any && msg_real.include?(key)
75
+ next if (expected_value == :any && msg_real.include?(key))
90
76
  return false if expected_value != msg_real[key]
91
77
  end
92
78
  true
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require "savon/mock"
4
3
 
5
4
  module Savon
6
5
  module SpecHelper
6
+
7
7
  class Interface
8
+
8
9
  def mock!
9
10
  Savon.observers << self
10
11
  end
@@ -26,12 +27,14 @@ module Savon
26
27
  def notify(operation_name, builder, globals, locals)
27
28
  expectation = expectations.shift
28
29
 
29
- raise ExpectationError, "Unexpected request to the #{operation_name.inspect} operation." unless expectation
30
-
31
- expectation.actual(operation_name, builder, globals, locals)
30
+ if expectation
31
+ expectation.actual(operation_name, builder, globals, locals)
32
32
 
33
- expectation.verify!
34
- expectation.response!
33
+ expectation.verify!
34
+ expectation.response!
35
+ else
36
+ raise ExpectationError, "Unexpected request to the #{operation_name.inspect} operation."
37
+ end
35
38
  rescue ExpectationError
36
39
  @expectations.clear
37
40
  raise
@@ -39,12 +42,12 @@ module Savon
39
42
 
40
43
  def verify!
41
44
  return if expectations.empty?
42
-
43
45
  expectations.each(&:verify!)
44
46
  rescue ExpectationError
45
47
  @expectations.clear
46
48
  raise
47
49
  end
50
+
48
51
  end
49
52
 
50
53
  def savon
@@ -55,5 +58,6 @@ module Savon
55
58
  super if defined? super
56
59
  savon.verify!
57
60
  end
61
+
58
62
  end
59
63
  end
data/lib/savon/mock.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  module Savon
4
3
  class ExpectationError < StandardError; end
5
4
  end
data/lib/savon/model.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  module Savon
4
3
  module Model
4
+
5
5
  def self.extended(base)
6
6
  base.setup
7
7
  end
@@ -13,13 +13,11 @@ module Savon
13
13
 
14
14
  # Accepts one or more SOAP operations and generates both class and instance methods named
15
15
  # after the given operations. Each generated method accepts an optional SOAP message Hash.
16
- # Raises ArgumentError for reserved model method names, before defining any methods.
17
16
  def operations(*operations)
18
- operations.map { |operation| [operation, operation_method_name(operation)] }.each do |operation, method_name|
19
- define_class_operation(operation, method_name)
20
- define_instance_operation(method_name)
17
+ operations.each do |operation|
18
+ define_class_operation(operation)
19
+ define_instance_operation(operation)
21
20
  end
22
- operations
23
21
  end
24
22
 
25
23
  def all_operations
@@ -29,43 +27,27 @@ module Savon
29
27
  private
30
28
 
31
29
  # Defines a class-level SOAP operation.
32
- def define_class_operation(operation, method_name)
33
- class_operation_module.define_method(method_name) do |locals = {}|
34
- client.call operation, locals
35
- end
30
+ def define_class_operation(operation)
31
+ class_operation_module.module_eval %{
32
+ def #{StringUtils.snakecase(operation.to_s)}(locals = {})
33
+ client.call #{operation.inspect}, locals
34
+ end
35
+ }
36
36
  end
37
37
 
38
38
  # Defines an instance-level SOAP operation.
39
- def define_instance_operation(method_name)
40
- instance_operation_module.define_method(method_name) do |locals = {}|
41
- self.class.public_send(method_name, locals)
42
- end
43
- end
44
-
45
- # Returns the generated Ruby method name for a SOAP operation.
46
- def operation_method_name(operation)
47
- method_name = StringUtils.snakecase(operation.to_s).to_sym
48
- # Re-registering an operation stays allowed; any other name must not shadow a method the model already responds to.
49
- return method_name if generated_operation_names.include?(method_name)
50
-
51
- reserved_methods = instance_methods + private_instance_methods +
52
- singleton_class.instance_methods + singleton_class.private_instance_methods
53
- raise ArgumentError, "SOAP operation #{operation.inspect} conflicts with reserved Savon::Model method #{method_name.inspect}" if reserved_methods.include?(method_name)
54
-
55
- method_name
56
- end
57
-
58
- # Names of operations already turned into methods on this model. The builder methods
59
- # (client, global, raise_initialization_error!) live in the same modules but are not
60
- # operations, so they stay reserved.
61
- def generated_operation_names
62
- (class_operation_module.instance_methods(false) + instance_operation_module.instance_methods(false)).uniq -
63
- %i[client global raise_initialization_error!]
39
+ def define_instance_operation(operation)
40
+ instance_operation_module.module_eval %{
41
+ def #{StringUtils.snakecase(operation.to_s)}(locals = {})
42
+ self.class.#{StringUtils.snakecase(operation.to_s)} locals
43
+ end
44
+ }
64
45
  end
65
46
 
66
47
  # Class methods.
67
48
  def class_operation_module
68
- @class_operation_module ||= Module.new do
49
+ @class_operation_module ||= Module.new {
50
+
69
51
  def client(globals = {})
70
52
  @client ||= Savon::Client.new(globals)
71
53
  rescue InitializationError
@@ -78,22 +60,26 @@ module Savon
78
60
 
79
61
  def raise_initialization_error!
80
62
  raise InitializationError,
81
- "Expected the model to be initialized with either a WSDL document or the SOAP endpoint and target namespace options.\n" \
82
- "Make sure to setup the model by calling the .client class method before calling the .global method.\n\n" \
83
- "client(wsdl: '/Users/me/project/service.wsdl') # to use a local WSDL document\n" \
84
- "client(wsdl: 'http://example.com?wsdl') # to use a remote WSDL document\n" \
85
- "client(endpoint: 'http://example.com', namespace: 'http://v1.example.com') # if you don't have a WSDL document"
63
+ "Expected the model to be initialized with either a WSDL document or the SOAP endpoint and target namespace options.\n" \
64
+ "Make sure to setup the model by calling the .client class method before calling the .global method.\n\n" \
65
+ "client(wsdl: '/Users/me/project/service.wsdl') # to use a local WSDL document\n" \
66
+ "client(wsdl: 'http://example.com?wsdl') # to use a remote WSDL document\n" \
67
+ "client(endpoint: 'http://example.com', namespace: 'http://v1.example.com') # if you don't have a WSDL document"
86
68
  end
87
- end.tap { |mod| extend(mod) }
69
+
70
+ }.tap { |mod| extend(mod) }
88
71
  end
89
72
 
90
73
  # Instance methods.
91
74
  def instance_operation_module
92
- @instance_operation_module ||= Module.new do
75
+ @instance_operation_module ||= Module.new {
76
+
93
77
  def client
94
78
  self.class.client
95
79
  end
96
- end.tap { |mod| include(mod) }
80
+
81
+ }.tap { |mod| include(mod) }
97
82
  end
83
+
98
84
  end
99
85
  end