lib/webtrap/rspec/matchers/send_request.rb in webtrap-0.0.1 vs lib/webtrap/rspec/matchers/send_request.rb in webtrap-0.1.0

- old
+ new

@@ -1,72 +1,103 @@ # frozen_string_literal: true require "webmock/rspec" +require "webtrap/shared" +require "rack" module WebTrap module RSpec module Matchers # @api private - # Provides the implementation for `send_request`. + # Provides the implementation for <code>send_request</code>. + # # Not intended to be instantiated directly. + # + # @see http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers/MatcherProtocol RSpec Matcher Protocol class SendRequest + # Initialize a new matcher. + # + # Unless more constraints are chained, this matcher will pass as long + # as any HTTP request gets intercepted. + # + # @see Shared::Validators::RequestSentValidator + def initialize + add_validator(Shared::Validators::RequestSentValidator.new) + end + # @api public # Specifies the XML payload of the request. # # The expectation will pass only if a request is sent with a payload - # that is considered equivalent to the reference. For further details - # see the {https://github.com/mbklein/equivalent-xml equivalent-xml} - # gem. + # that is considered equivalent to the reference. # - # @param xml [] The reference XML payload. - # @return [SendRequestWithXml] - # The matcher to verify that a request is with a payload equivalent - # to the reference. + # @param xml [String] + # The reference XML payload. + # @return [SendRequest] + # This matcher instance, to allow further chaining. + # @see Shared::Validators::EquivalentXmlContentValidator def with_xml(xml) - SendRequestWithXml.new(xml) + add_validator(Shared::Validators::EquivalentXmlContentValidator.new(xml)) + self end # @api private - # Checks if the provided Proc sends an HTTP request. + # Whether a request was intercepted that matches all validators. + # + # Executes the provided proc, intercepting all transmitted HTTP requests + # and running them through the set of validators. + # # @param transmission_proc [Proc] - # The proc that should send an HTTP request. - # @return - # Whether an HTTP request was sent. - # @see {RSpec::Matchers::MatcherProtocol#matches?} + # The proc that is expected to send the requests. + # @return {Boolean} def matches?(transmission_proc) perform_transmission(transmission_proc) - request_sent? + failure_message.nil? end # @api private - # Message to be shown if the expectation fails to pass. + # Message to be shown if no request is intercepted for which all + # validators are successful. + # # @return [String] def failure_message - "expected block to send an HTTP request, but nothing was sent out" + return if failed_validator.nil? + failed_validator.failure_message end # @api private # Allows the matcher to be used with block expectations. - # @return [TrueClass] + # + # @return [true] def supports_block_expectations? true end private + def validators + @_validators ||= [] + end + + def add_validator(validator) + validators << validator + end + + def failed_validator + validators.find(&:failed?) + end + def perform_transmission(transmission_proc) WebMock.disable_net_connect! - begin - transmission_proc.call - @request_sent = false - rescue WebMock::NetConnectNotAllowedError - @request_sent = true - end + WebMock::API.stub_request(:any, /.*/).to_rack(app) + + transmission_proc.call + WebMock.allow_net_connect! end - def request_sent? - @request_sent + def app + @_app ||= Shared::RackApp.new(validators) end end end end end