Sha256: aecca2289b6e34d3d6be97d38e9e3afa282acf0150cacf51b899b3dcc795670e

Contents?: true

Size: 1.75 KB

Versions: 57

Compression:

Stored size: 1.75 KB

Contents

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

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 [QueryStringMatches] parameter matcher.
    #
    # @see Expectation#with
    #
    # @example Actual URI has equivalent query string.
    #   object = mock()
    #   object.expects(:method_1).with(has_equivalent_query_string('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 does not have equivalent query string.
    #   object = mock()
    #   object.expects(:method_1).with(has_equivalent_query_string('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 has_equivalent_query_string(uri)
      QueryStringMatches.new(uri)
    end

    # Parameter matcher which matches URIs with equivalent query strings.
    class QueryStringMatches < 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
        "has_equivalent_query_string(#{@uri.mocha_inspect})"
      end

    private
      # @private
      def explode(uri)
        query_hash = (uri.query || '').split('&').inject({}){ |h, kv| h.merge(Hash[*kv.split('=')]) }
        URI::Generic::COMPONENT.inject({}){ |h, k| h.merge(k => uri.__send__(k)) }.merge(:query => query_hash)
      end

    end
  end
end

Version data entries

57 entries across 46 versions & 5 rubygems

Version Path
mocha-1.2.1 lib/mocha/parameter_matchers/query_string.rb
mocha-1.2.0 lib/mocha/parameter_matchers/query_string.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/parameter_matchers/query_string.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/parameter_matchers/query_string.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/parameter_matchers/query_string.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/parameter_matchers/query_string.rb
mocha-1.1.0 lib/mocha/parameter_matchers/query_string.rb
mocha-1.0.0 lib/mocha/parameter_matchers/query_string.rb
mocha-1.0.0.alpha lib/mocha/parameter_matchers/query_string.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/mocha-0.14.0/lib/mocha/parameter_matchers/query_string.rb
challah-1.0.0 vendor/bundle/gems/mocha-0.14.0/lib/mocha/parameter_matchers/query_string.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/mocha-0.14.0/lib/mocha/parameter_matchers/query_string.rb
mocha-0.14.0 lib/mocha/parameter_matchers/query_string.rb
challah-1.0.0.beta3 vendor/bundle/gems/mocha-0.13.3/lib/mocha/parameter_matchers/query_string.rb
mocha-0.14.0.alpha lib/mocha/parameter_matchers/query_string.rb
challah-1.0.0.beta2 vendor/bundle/gems/mocha-0.13.3/lib/mocha/parameter_matchers/query_string.rb
challah-1.0.0.beta vendor/bundle/gems/mocha-0.13.3/lib/mocha/parameter_matchers/query_string.rb
mocha-0.13.3 lib/mocha/parameter_matchers/query_string.rb
mocha-0.12.10 lib/mocha/parameter_matchers/query_string.rb
mocha-0.12.9 lib/mocha/parameter_matchers/query_string.rb