Sha256: e05a87df37db67cc5840febf3cddd283133b45cbf624f50a3704f96209512676

Contents?: true

Size: 1.61 KB

Versions: 16

Compression:

Stored size: 1.61 KB

Contents

require 'mocha/parameter_matchers/base'
require 'uri'
require 'cgi'

module Mocha
  module ParameterMatchers
    # Matches a URI without regard to the ordering of parameters in the query string.
    #
    # @param [String] uri URI to match.
    # @return [EquivalentUri] parameter matcher.
    #
    # @see Expectation#with
    #
    # @example Actual URI is equivalent.
    #   object = mock()
    #   object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
    #   object.method_1('http://example.com/foo?b=2&a=1')
    #   # no error raised
    #
    # @example Actual URI is not equivalent.
    #   object = mock()
    #   object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
    #   object.method_1('http://example.com/foo?a=1&b=3')
    #   # error raised, because the query parameters were different
    def equivalent_uri(uri)
      EquivalentUri.new(uri)
    end

    # Parameter matcher which matches URIs with equivalent query strings.
    class EquivalentUri < Base
      # @private
      def initialize(uri)
        @uri = URI.parse(uri)
      end

      # @private
      def matches?(available_parameters)
        actual = explode(URI.parse(available_parameters.shift))
        expected = explode(@uri)
        actual == expected
      end

      # @private
      def mocha_inspect
        "equivalent_uri(#{@uri.mocha_inspect})"
      end

      private

      # @private
      def explode(uri)
        query_hash = CGI.parse(uri.query || '')
        URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(query: query_hash)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/mocha-2.7.1/lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.7.1 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.7.0 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.6.1 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.6.0 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.5.0 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.4.5 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.4.4 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.4.3 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.4.2 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.4.1 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.4.0 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.2.0 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.1.0 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.0.4 lib/mocha/parameter_matchers/equivalent_uri.rb
mocha-2.0.3 lib/mocha/parameter_matchers/equivalent_uri.rb