diff --git a/lib/active_resource/http_mock.rb b/lib/active_resource/http_mock.rb index dcdb587aa5..ac38833597 100644 --- a/lib/active_resource/http_mock.rb +++ b/lib/active_resource/http_mock.rb @@ -58,12 +58,12 @@ def initialize(responses) end [ :post, :patch, :put, :get, :delete, :head ].each do |method| - # def post(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - # @responses[Request.new(:post, path, nil, request_headers)] = Response.new(body || "", status, response_headers) + # def post(path, request_headers = {}, body = nil, status = 200, response_headers = {}, options: {}) + # @responses[Request.new(:post, path, nil, request_headers, options)] = Response.new(body || "", status, response_headers) # end module_eval <<-EOE, __FILE__, __LINE__ + 1 - def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - request = Request.new(:#{method}, path, nil, request_headers) + def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}, options = {}) + request = Request.new(:#{method}, path, nil, request_headers, options) response = Response.new(body || "", status, response_headers) delete_duplicate_responses(request) @@ -244,8 +244,8 @@ def net_connection_disabled? { true => %w(post patch put), false => %w(get delete head) }.each do |has_body, methods| methods.each do |method| - # def post(path, body, headers) - # request = ActiveResource::Request.new(:post, path, body, headers) + # def post(path, body, headers, options = {}) + # request = ActiveResource::Request.new(:post, path, body, headers, options) # self.class.requests << request # if response = self.class.responses.assoc(request) # response[1] @@ -254,8 +254,8 @@ def net_connection_disabled? # end # end module_eval <<-EOE, __FILE__, __LINE__ + 1 - def #{method}(path, #{'body, ' if has_body}headers) - request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers) + def #{method}(path, #{'body, ' if has_body}headers, options = {}) + request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers, options) self.class.requests << request if response = self.class.responses.assoc(request) response[1] @@ -279,18 +279,29 @@ def inspect_responses # :nodoc: class Request attr_accessor :path, :method, :body, :headers - def initialize(method, path, body = nil, headers = {}) - @method, @path, @body, @headers = method, path, body, headers + def initialize(method, path, body = nil, headers = {}, options = {}) + @method, @path, @body, @headers, @options = method, path, body, headers, options end def ==(req) - path == req.path && method == req.method && headers_match?(req) + if @options && @options[:omit_query_in_path] + remove_query_params_from_path == req.remove_query_params_from_path && method == req.method && headers_match?(req) + else + path == req.path && method == req.method && headers_match?(req) + end end def to_s "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>" end + # Removes query parameters from the path. + # + # @return [String] the path without query parameters + def remove_query_params_from_path + path.split("?").first + end + private def headers_match?(req) # Ignore format header on equality if it's not defined diff --git a/test/cases/http_mock_test.rb b/test/cases/http_mock_test.rb index 008cef4dfe..5c02e497a2 100644 --- a/test/cases/http_mock_test.rb +++ b/test/cases/http_mock_test.rb @@ -193,6 +193,16 @@ class HttpMockTest < ActiveSupport::TestCase assert_equal 2, ActiveResource::HttpMock.responses.length end + test "omits query parameters from the URL when options[:omit_query_params] is true" do + ActiveResource::HttpMock.respond_to do |mock| + mock.get("/endpoint", {}, "Response", 200, {}, { omit_query_in_path: true }) + end + + response = request(:get, "/endpoint?param1=value1¶m2=value2") + + assert_equal "Response", response.body + end + def request(method, path, headers = {}, body = nil) if method.in?([:patch, :put, :post]) @http.send(method, path, body, headers)