webtrap 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6588474ffa8dde283728eb5f92c5eff22fe4dcba
4
- data.tar.gz: 6bbd81d456fb3a44a72c95a570c2b24dfb5df1a1
3
+ metadata.gz: a15e9e6f37a30d98232e8f3d32903ae6a8bc6a90
4
+ data.tar.gz: 8b30c18bed81414f5ee9fcc77eba9960ab0b37ae
5
5
  SHA512:
6
- metadata.gz: de34495dfafe249c736524599a365bd9ad3e5dce325fa1421dadd04d65e5f248a8b20ec0f5e664d7d25b74ce73fc5f55165742ee8fe18accecd11d2b7dd2032a
7
- data.tar.gz: 98a88a5212a5769ce493862ae11db94f4a54f7b273c0f2880dc081a6cb84dc300b092f52a6d5ecd63970b845dd11437905da019b964b770578fb8a67795ee81a
6
+ metadata.gz: 147dafeaf072c3603589c96954649695afe74fad643fd7664531b998fc3f404620695990a14ecbeab3ddf7361c6ef3a5bb0c61f8243b72ac8b840e8e680aa4b4
7
+ data.tar.gz: ed986d9cae9ca4bd8f98457a38948eb7d316e97065c2cfe3dce6737f10bffa6553a2ecc5e1867bd1592bbb25ed231dfa3cd828c4608ee38493bfa32f057b3bf9
data/.travis.yml CHANGED
@@ -5,12 +5,10 @@ cache:
5
5
  - ../bundle
6
6
  bundler_args: "--standalone --path ../bundle"
7
7
  rvm:
8
- - 2.0.0
9
- - 2.1
10
8
  - 2.2.5
11
9
  - 2.3.3
12
10
  - 2.4.0
13
11
  - ruby-head
14
- before_install: gem install bundler -v 1.13.6
12
+ before_install: gem install bundler
15
13
  script:
16
- - bundle exec cucumber
14
+ - bundle exec rake
data/Gemfile CHANGED
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "http://rubygems.org"
3
+ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
7
  group :documentation do
8
+ gem "relish", "~> 0.7.1"
8
9
  gem "yard", "~> 0.9.8"
9
10
  end
data/Gemfile.lock CHANGED
@@ -1,19 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- webtrap (0.0.0)
4
+ webtrap (0.0.1)
5
5
  equivalent-xml (~> 0.6.0)
6
+ rack (~> 2.0)
6
7
  rspec (~> 3.0)
7
- sinatra (~> 1.0)
8
8
  webmock (~> 2.0)
9
9
 
10
10
  GEM
11
- remote: http://rubygems.org/
11
+ remote: https://rubygems.org/
12
12
  specs:
13
13
  addressable (2.5.0)
14
14
  public_suffix (~> 2.0, >= 2.0.2)
15
+ archive-tar-minitar (0.6.1)
16
+ minitar (~> 0.6)
17
+ minitar-cli (~> 0.6)
15
18
  ast (2.3.0)
16
19
  builder (3.2.3)
20
+ coderay (1.1.1)
17
21
  crack (0.4.3)
18
22
  safe_yaml (~> 1.0.0)
19
23
  cucumber (2.4.0)
@@ -28,24 +32,54 @@ GEM
28
32
  gherkin (~> 4.0)
29
33
  cucumber-wire (0.0.1)
30
34
  diff-lcs (1.3)
35
+ domain_name (0.5.20170223)
36
+ unf (>= 0.0.5, < 1.0.0)
31
37
  equivalent-xml (0.6.0)
32
38
  nokogiri (>= 1.4.3)
33
39
  gherkin (4.0.0)
34
40
  hashdiff (0.3.2)
41
+ hashie (3.5.5)
42
+ http-cookie (1.0.3)
43
+ domain_name (~> 0.5)
44
+ json (2.0.3)
45
+ method_source (0.8.2)
46
+ mime-types (3.1)
47
+ mime-types-data (~> 3.2015)
48
+ mime-types-data (3.2016.0521)
35
49
  mini_portile2 (2.1.0)
50
+ minitar (0.6.1)
51
+ minitar-cli (0.6.1)
52
+ minitar (~> 0.6.0)
53
+ powerbar (~> 1.0)
36
54
  multi_json (1.12.1)
37
55
  multi_test (0.1.2)
56
+ netrc (0.11.0)
38
57
  nokogiri (1.6.8.1)
39
58
  mini_portile2 (~> 2.1.0)
40
59
  parser (2.4.0.0)
41
60
  ast (~> 2.2)
61
+ powerbar (1.0.18)
62
+ hashie (>= 1.1.0)
42
63
  powerpack (0.1.1)
64
+ pry (0.10.4)
65
+ coderay (~> 1.1.0)
66
+ method_source (~> 0.8.1)
67
+ slop (~> 3.4)
68
+ pry-remote (0.1.8)
69
+ pry (~> 0.9)
70
+ slop (~> 3.0)
43
71
  public_suffix (2.0.5)
44
- rack (1.6.5)
45
- rack-protection (1.5.3)
46
- rack
72
+ rack (2.0.1)
47
73
  rainbow (2.2.1)
48
- rake (10.4.2)
74
+ rake (12.0.0)
75
+ relish (0.7.1)
76
+ archive-tar-minitar (>= 0.5.2)
77
+ json (>= 1.4.6)
78
+ rest-client (>= 1.7.2)
79
+ rest-client (2.0.1)
80
+ http-cookie (>= 1.0.2, < 2.0)
81
+ mime-types (>= 1.16, < 4.0)
82
+ netrc (~> 0.8)
49
83
  rspec (3.5.0)
50
84
  rspec-core (~> 3.5.0)
51
85
  rspec-expectations (~> 3.5.0)
@@ -67,11 +101,10 @@ GEM
67
101
  unicode-display_width (~> 1.0, >= 1.0.1)
68
102
  ruby-progressbar (1.8.1)
69
103
  safe_yaml (1.0.4)
70
- sinatra (1.4.8)
71
- rack (~> 1.5)
72
- rack-protection (~> 1.4)
73
- tilt (>= 1.3, < 3)
74
- tilt (2.0.6)
104
+ slop (3.6.0)
105
+ unf (0.1.4)
106
+ unf_ext
107
+ unf_ext (0.0.7.2)
75
108
  unicode-display_width (1.1.3)
76
109
  webmock (2.3.2)
77
110
  addressable (>= 2.3.6)
@@ -85,10 +118,12 @@ PLATFORMS
85
118
  DEPENDENCIES
86
119
  bundler (~> 1.13)
87
120
  cucumber (~> 2.0)
88
- rake (~> 10.0)
121
+ pry-remote (~> 0.1.8)
122
+ rake (~> 12.0)
123
+ relish (~> 0.7.1)
89
124
  rubocop (~> 0.47.1)
90
125
  webtrap!
91
126
  yard (~> 0.9.8)
92
127
 
93
128
  BUNDLED WITH
94
- 1.13.6
129
+ 1.14.5
data/README.md CHANGED
@@ -5,7 +5,11 @@ WebTrap
5
5
  [![Build Status](https://travis-ci.org/pfac/webtrap.svg?branch=master)](https://travis-ci.org/pfac/webtrap)
6
6
  [![Dependency Status](https://gemnasium.com/badges/github.com/pfac/webtrap.svg)](https://gemnasium.com/github.com/pfac/webtrap)
7
7
  [![Code Climate](https://codeclimate.com/github/pfac/webtrap/badges/gpa.svg)](https://codeclimate.com/github/pfac/webtrap)
8
- [![Inch CI](https://inch-ci.org/github/pfac/webtrap.svg?branch=master)](https://inch-ci.org/github/pfac/webtrap)
8
+
9
+ Documentation:
10
+ [![Usage Documentation](https://img.shields.io/badge/usage-0.1.0-brightgreen.svg?style=flat)](http://www.relishapp.com/pfac/webtrap/docs)
11
+ [![API Documentation](https://img.shields.io/badge/api-0.1.0-brightgreen.svg?style=flat)](http://www.rubydoc.info/gems/webtrap)
12
+ [![Documentation Coverage](https://inch-ci.org/github/pfac/webtrap.svg?branch=master)](https://inch-ci.org/github/pfac/webtrap)
9
13
 
10
14
  WebTrap allows you to write tests that assert on outgoing requests. This allows
11
15
  you to verify that such requests match the documentation of external services
@@ -1,71 +1,102 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "webmock/rspec"
4
+ require "webtrap/shared"
5
+ require "rack"
4
6
 
5
7
  module WebTrap
6
8
  module RSpec
7
9
  module Matchers
8
10
  # @api private
9
- # Provides the implementation for `send_request`.
11
+ # Provides the implementation for <code>send_request</code>.
12
+ #
10
13
  # Not intended to be instantiated directly.
14
+ #
15
+ # @see http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers/MatcherProtocol RSpec Matcher Protocol
11
16
  class SendRequest
17
+ # Initialize a new matcher.
18
+ #
19
+ # Unless more constraints are chained, this matcher will pass as long
20
+ # as any HTTP request gets intercepted.
21
+ #
22
+ # @see Shared::Validators::RequestSentValidator
23
+ def initialize
24
+ add_validator(Shared::Validators::RequestSentValidator.new)
25
+ end
26
+
12
27
  # @api public
13
28
  # Specifies the XML payload of the request.
14
29
  #
15
30
  # The expectation will pass only if a request is sent with a payload
16
- # that is considered equivalent to the reference. For further details
17
- # see the {https://github.com/mbklein/equivalent-xml equivalent-xml}
18
- # gem.
31
+ # that is considered equivalent to the reference.
19
32
  #
20
- # @param xml [] The reference XML payload.
21
- # @return [SendRequestWithXml]
22
- # The matcher to verify that a request is with a payload equivalent
23
- # to the reference.
33
+ # @param xml [String]
34
+ # The reference XML payload.
35
+ # @return [SendRequest]
36
+ # This matcher instance, to allow further chaining.
37
+ # @see Shared::Validators::EquivalentXmlContentValidator
24
38
  def with_xml(xml)
25
- SendRequestWithXml.new(xml)
39
+ add_validator(Shared::Validators::EquivalentXmlContentValidator.new(xml))
40
+ self
26
41
  end
27
42
 
28
43
  # @api private
29
- # Checks if the provided Proc sends an HTTP request.
44
+ # Whether a request was intercepted that matches all validators.
45
+ #
46
+ # Executes the provided proc, intercepting all transmitted HTTP requests
47
+ # and running them through the set of validators.
48
+ #
30
49
  # @param transmission_proc [Proc]
31
- # The proc that should send an HTTP request.
32
- # @return
33
- # Whether an HTTP request was sent.
34
- # @see {RSpec::Matchers::MatcherProtocol#matches?}
50
+ # The proc that is expected to send the requests.
51
+ # @return {Boolean}
35
52
  def matches?(transmission_proc)
36
53
  perform_transmission(transmission_proc)
37
- request_sent?
54
+ failure_message.nil?
38
55
  end
39
56
 
40
57
  # @api private
41
- # Message to be shown if the expectation fails to pass.
58
+ # Message to be shown if no request is intercepted for which all
59
+ # validators are successful.
60
+ #
42
61
  # @return [String]
43
62
  def failure_message
44
- "expected block to send an HTTP request, but nothing was sent out"
63
+ return if failed_validator.nil?
64
+ failed_validator.failure_message
45
65
  end
46
66
 
47
67
  # @api private
48
68
  # Allows the matcher to be used with block expectations.
49
- # @return [TrueClass]
69
+ #
70
+ # @return [true]
50
71
  def supports_block_expectations?
51
72
  true
52
73
  end
53
74
 
54
75
  private
55
76
 
77
+ def validators
78
+ @_validators ||= []
79
+ end
80
+
81
+ def add_validator(validator)
82
+ validators << validator
83
+ end
84
+
85
+ def failed_validator
86
+ validators.find(&:failed?)
87
+ end
88
+
56
89
  def perform_transmission(transmission_proc)
57
90
  WebMock.disable_net_connect!
58
- begin
59
- transmission_proc.call
60
- @request_sent = false
61
- rescue WebMock::NetConnectNotAllowedError
62
- @request_sent = true
63
- end
91
+ WebMock::API.stub_request(:any, /.*/).to_rack(app)
92
+
93
+ transmission_proc.call
94
+
64
95
  WebMock.allow_net_connect!
65
96
  end
66
97
 
67
- def request_sent?
68
- @request_sent
98
+ def app
99
+ @_app ||= Shared::RackApp.new(validators)
69
100
  end
70
101
  end
71
102
  end
@@ -1,17 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "webtrap/rspec/matchers/send_request"
4
- require "webtrap/rspec/matchers/send_request_with_xml"
5
-
6
3
  module WebTrap
7
4
  module RSpec
8
- # WebTrap::RSpec::Matchers provides the set of matchers available to define
9
- # expections about outgoing requests.
5
+ # Set of matchers available to define expections about outgoing requests.
10
6
  module Matchers
11
- # Passes if the block sends any HTTP request.
7
+ autoload :SendRequest, "webtrap/rspec/matchers/send_request"
8
+
9
+ # Passes if the expectation block sends an HTTP request.
10
+ #
11
+ # Constraints to specify the expected request can be chained.
12
12
  #
13
13
  # @return [SendRequest]
14
- # The matcher to verify that the request is sent.
14
+ # The matcher instance to verify that a request is sent.
15
15
  def send_request
16
16
  SendRequest.new
17
17
  end
data/lib/webtrap/rspec.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "webtrap/server"
4
-
5
3
  require "webtrap/rspec/matchers"
6
4
 
7
5
  module WebTrap
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # Rack applications used to intercept HTTP requests and apply a set of
5
+ # validators on them.
6
+ class WebTrap::Shared::RackApp
7
+ # Instantiate a new Rack application with the provided set of validators.
8
+ #
9
+ # @param validators [Array]
10
+ # Set of validators used on the intercepted request to assert its validity.
11
+ # @example Intercept any request
12
+ # require "webmock"
13
+ # # ...
14
+ # WebMock::API.stub_request(:any, /.*/).to_rack RackApp.new(validators)
15
+ def initialize(validators)
16
+ @validators = validators
17
+ end
18
+
19
+ # Handle an HTTP request.
20
+ #
21
+ # @param request [Hash]
22
+ # Request environment, as defined by the Rack spec.
23
+ # @return [Array<Fixnum, Hash, Array>]
24
+ # An empty successful response.
25
+ # @see http://www.rubydoc.info/github/rack/rack/master/file/SPEC
26
+ # Rack spec
27
+ def call(request)
28
+ @validators.find do |v|
29
+ v.validate(request).failed?
30
+ end
31
+ [200, {}, []]
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "equivalent-xml"
4
+
5
+ # @api private
6
+ # Validator for asserting a request was sent with an equivalent XML payload.
7
+ #
8
+ # @see https://github.com/mbklein/equivalent-xml
9
+ # equivalent-xml gem
10
+ class WebTrap::Shared::Validators::EquivalentXmlContentValidator
11
+ # Instantiate a new validator with the provided payload.
12
+ #
13
+ # @param xml [String]
14
+ # The XML payload requests will be compared against.
15
+ def initialize(xml)
16
+ @failed = true
17
+ @xml = xml
18
+ end
19
+
20
+ # Whether no request with an equivalent payload was validated.
21
+ #
22
+ # @return [Boolean]
23
+ def failed?
24
+ @failed
25
+ end
26
+
27
+ # The message to be used if no request is validated with an equivalent
28
+ # payload.
29
+ #
30
+ # @return [String]
31
+ def failure_message
32
+ "expected block to send an HTTP request with XML body, but payload was not equivalent"
33
+ end
34
+
35
+ # Validate if the request has an equivalent XML payload.
36
+ #
37
+ # @param request [Hash]
38
+ # Request environment passed by {WebTrap::Shared::RackApp#call}.
39
+ # @return [EquivalentXmlContentValidator]
40
+ # This validator instance.
41
+ def validate(request)
42
+ @failed = EquivalentXml.equivalent?(xml, request["rack.input"].string)
43
+ self
44
+ end
45
+
46
+ private
47
+
48
+ attr_reader :xml
49
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # Validator for asserting whether a request was sent.
5
+ class WebTrap::Shared::Validators::RequestSentValidator
6
+ # Initialize a new validator.
7
+ def initialize
8
+ @failed = true
9
+ end
10
+
11
+ # Whether no request was validated.
12
+ #
13
+ # @return [Boolean]
14
+ def failed?
15
+ @failed
16
+ end
17
+
18
+ # The message to be used if no request is validated.
19
+ #
20
+ # @return [String]
21
+ def failure_message
22
+ "expected block to send an HTTP request, but nothing was sent out"
23
+ end
24
+
25
+ # Validate a request.
26
+ #
27
+ # Since validators are run against intercepted requests this validator will
28
+ # succeed for any request.
29
+ #
30
+ # @return [RequestSentValidator]
31
+ # This validator instance.
32
+ def validate(_)
33
+ @failed = false
34
+ self
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebTrap
4
+ module Shared
5
+ # @api private
6
+ # Validators used to assert constraints over the intercepted HTTP requests.
7
+ module Validators
8
+ autoload :EquivalentXmlContentValidator, "webtrap/shared/validators/equivalent_xml_content_validator"
9
+ autoload :RequestSentValidator, "webtrap/shared/validators/request_sent_validator"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "webtrap/shared/validators"
4
+
5
+ module WebTrap
6
+ # @api private
7
+ # Shared components used by all implementations for specific testing tools.
8
+ module Shared
9
+ autoload :RackApp, "webtrap/shared/rack_app"
10
+ end
11
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module WebTrap
4
4
  # Current version of the WebTrap gem.
5
- VERSION = "0.0.1"
5
+ VERSION = "0.1.0"
6
6
  end
data/webtrap.gemspec CHANGED
@@ -36,12 +36,13 @@ Gem::Specification.new do |spec|
36
36
  spec.require_paths = ["lib"]
37
37
 
38
38
  spec.add_dependency "equivalent-xml", "~> 0.6.0"
39
+ spec.add_dependency "rack", "~> 2.0"
39
40
  spec.add_dependency "rspec", "~> 3.0"
40
- spec.add_dependency "sinatra", "~> 1.0"
41
41
  spec.add_dependency "webmock", "~> 2.0"
42
42
 
43
43
  spec.add_development_dependency "bundler", "~> 1.13"
44
44
  spec.add_development_dependency "cucumber", "~> 2.0"
45
- spec.add_development_dependency "rake", "~> 10.0"
45
+ spec.add_development_dependency "pry-remote", "~> 0.1.8"
46
+ spec.add_development_dependency "rake", "~> 12.0"
46
47
  spec.add_development_dependency "rubocop", "~> 0.47.1"
47
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webtrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Costa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-26 00:00:00.000000000 Z
11
+ date: 2017-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: equivalent-xml
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.6.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: rack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.0'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.0'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sinatra
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.0'
47
+ version: '3.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.0'
54
+ version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: webmock
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,20 +94,34 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-remote
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.8
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.8
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - "~>"
102
116
  - !ruby/object:Gem::Version
103
- version: '10.0'
117
+ version: '12.0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
- version: '10.0'
124
+ version: '12.0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rubocop
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -149,8 +163,11 @@ files:
149
163
  - lib/webtrap/rspec.rb
150
164
  - lib/webtrap/rspec/matchers.rb
151
165
  - lib/webtrap/rspec/matchers/send_request.rb
152
- - lib/webtrap/rspec/matchers/send_request_with_xml.rb
153
- - lib/webtrap/server.rb
166
+ - lib/webtrap/shared.rb
167
+ - lib/webtrap/shared/rack_app.rb
168
+ - lib/webtrap/shared/validators.rb
169
+ - lib/webtrap/shared/validators/equivalent_xml_content_validator.rb
170
+ - lib/webtrap/shared/validators/request_sent_validator.rb
154
171
  - lib/webtrap/version.rb
155
172
  - webtrap.gemspec
156
173
  homepage: https://github.com/pfac/webtrap
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "equivalent-xml"
4
- require "webmock/rspec"
5
-
6
- module WebTrap
7
- module RSpec
8
- module Matchers
9
- # @api private
10
- # Used to specify the XML payload of the request.
11
- class SendRequestWithXml < SendRequest
12
- # Reference to be compared with the payload of outgoing XML requests.
13
- attr_reader :expected_payload
14
-
15
- # Allows the parameterization of the matcher with the reference payload.
16
- # @param expected_payload []
17
- # Reference to be compared with the payload of outgoing XML requests.
18
- def initialize(expected_payload)
19
- @expected_payload = expected_payload
20
- end
21
-
22
- # @api private
23
- # Checks if the provided Proc sends a request with an XML payload
24
- # equivalent to the expected reference.
25
- # @param transmission_proc [Proc]
26
- # The proc that should send a valid request.
27
- # @return
28
- # Whether a request was sent with an equivalent XML payload.
29
- # @see {RSpec::Matchers::MatcherProtocol#matches?}
30
- def matches?(transmission_proc)
31
- perform_transmission(transmission_proc)
32
- request_sent? && valid_payload?
33
- end
34
-
35
- # @api private
36
- # Message to be shown if the expectation fails to pass.
37
- # @return [String]
38
- # @see {RSpec::Matchers::MatcherProtocol#failure_message}
39
- def failure_message
40
- return super unless request_sent?
41
-
42
- "expected block to send an HTTP request with XML body, but payload was not equivalent"
43
- end
44
-
45
- # @api private
46
- # Allows the matcher to be used with block expectations.
47
- # @return [TrueClass]
48
- # @see {RSpec::Matchers::MatcherProtocol#supports_block_expectations?}
49
- def supports_block_expectations?
50
- true
51
- end
52
-
53
- private
54
-
55
- def perform_transmission(transmission_proc)
56
- WebMock.disable_net_connect!
57
- WebMock::API.stub_request(:post, /.*/).to_rack(server)
58
-
59
- transmission_proc.call
60
-
61
- WebMock.allow_net_connect!
62
- end
63
-
64
- def server
65
- expected_payload = self.expected_payload
66
-
67
- @_server ||= ::Class.new(::WebTrap::Server) do
68
- @payload_was_valid = false
69
- @request_received = false
70
-
71
- post "*" do
72
- actual_payload = request.body.string
73
-
74
- self.class.request_received = true
75
- self.class.payload_was_valid = EquivalentXml.equivalent?(actual_payload, expected_payload)
76
- end
77
- end
78
- end
79
-
80
- def request_sent?
81
- super || server.request_received?
82
- end
83
-
84
- def valid_payload?
85
- server.payload_was_valid?
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "sinatra"
4
-
5
- module WebTrap
6
- # Base class for creating fake web application to capture outgoing web
7
- # requests and assert on them.
8
- #
9
- # Descendants of this class are in charge of setting the required rules to
10
- # correctly intercept the target requests.
11
- #
12
- # Currently it is only used by SendRequestWithXml.
13
- class Server < Sinatra::Base
14
- @payload_was_valid = false
15
- @request_received = false
16
-
17
- class << self
18
- # Set whether the payload met the matcher constraints.
19
- # @param value [Boolean]
20
- attr_writer :payload_was_valid
21
-
22
- # Set whether a request was received at all.
23
- # @param value [Boolean]
24
- attr_writer :request_received
25
- end
26
-
27
- # Whether the payload met the matcher constraints.
28
- # @return [Boolean] `false` by default.
29
- def self.payload_was_valid?
30
- @payload_was_valid
31
- end
32
-
33
- # Whether a request was received at all.
34
- # @return [Boolean] `false` by default.
35
- def self.request_received?
36
- @request_received
37
- end
38
- end
39
- end