Sha256: 56f57132fc6636dbb8c34b5ff0002b0207f67be9af75c2434d4a6d5bbc867e78

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require "equivalent-xml"
require "webmock/rspec"

module WebTrap
  module RSpec
    module Matchers
      class SendRequestWithXml < SendRequest
        attr_reader :expected_payload

        def initialize(expected_payload)
          @expected_payload = expected_payload
        end

        def matches?(transmission_proc)
          perform_transmission(transmission_proc)
          request_sent? && valid_payload?
        end

        def failure_message
          return super unless request_sent?

          "expected block to send an HTTP request with XML body, but payload was not equivalent"
        end

        def supports_block_expectations?
          true
        end

        private

        def perform_transmission(transmission_proc)
          WebMock.disable_net_connect!
          WebMock::API.stub_request(:post, /.*/).to_rack(server)

          transmission_proc.call

          WebMock.allow_net_connect!
        end

        def server
          expected_payload = self.expected_payload

          @_server ||= ::Class.new(::WebTrap::Server) do
            @payload_was_valid = false
            @request_received = false

            post "*" do
              actual_payload = request.body.string

              self.class.request_received = true
              self.class.payload_was_valid = EquivalentXml.equivalent?(actual_payload, expected_payload)
            end
          end
        end

        def request_sent?
          super || server.request_received?
        end

        def valid_payload?
          server.payload_was_valid?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webtrap-0.0.0 lib/webtrap/rspec/matchers/send_request_with_xml.rb