Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 69 lines (54 sloc) 2.183 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
module WebMock
  module API
    extend self

    def stub_request(method, uri)
      WebMock::StubRegistry.instance.
        register_request_stub(WebMock::RequestStub.new(method, uri))
    end

    def remove_request_stub(method, uri)
      WebMock::StubRegistry.instance.
        unregister_request_stub(WebMock::RequestSignature.new(method, uri))
    end

    alias_method :stub_http_request, :stub_request

    def a_request(method, uri)
      WebMock::RequestPattern.new(method, uri)
    end

    class << self
      alias :request :a_request
    end


    def assert_requested(*args, &block)
      if not args[0].is_a?(WebMock::RequestStub)
        args = convert_uri_method_and_options_to_request_and_options(*args, &block)
      end
      assert_request_requested(*args, &block)
    end

    def assert_not_requested(*args, &block)
      if not args[0].is_a?(WebMock::RequestStub)
        args = convert_uri_method_and_options_to_request_and_options(*args, &block)
      end
      assert_request_not_requested(*args, &block)
    end

    def hash_including(expected)
      if defined?(::RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher)
        RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new(expected)
      elsif defined?(::Spec::Mocks::ArgumentMatchers::HashIncludingMatcher)
        Spec::Mocks::ArgumentMatchers::HashIncludingMatcher.new(expected)
      else
        WebMock::Matchers::HashIncludingMatcher.new(expected)
      end
    end

    private

    def convert_uri_method_and_options_to_request_and_options(*args, &block)
      request = WebMock::RequestPattern.new(*args).with(&block)
      [request, args[2] || {}]
    end

    def assert_request_requested(request, options = {})
      verifier = WebMock::RequestExecutionVerifier.new(request, options.delete(:times) || 1)
      WebMock::AssertionFailure.failure(verifier.failure_message) unless verifier.matches?
    end

    def assert_request_not_requested(request, options = {})
      verifier = WebMock::RequestExecutionVerifier.new(request, options.delete(:times))
      WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
    end

  end
end
Something went wrong with that request. Please try again.