Skip to content

Commit

Permalink
Merge pull request #395 from muhammadnawzad/main
Browse files Browse the repository at this point in the history
[Enhancement] Allows HttpMock to ignore query params from url path if given the option
  • Loading branch information
rafaelfranca authored Jan 24, 2024
2 parents 209f276 + ad22d2d commit c7fc06f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/active_resource/http_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/cases/http_mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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&param2=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)
Expand Down

0 comments on commit c7fc06f

Please sign in to comment.