Skip to content

Commit c7fc06f

Browse files
authored
Merge pull request #395 from muhammadnawzad/main
[Enhancement] Allows HttpMock to ignore query params from url path if given the option
2 parents 209f276 + ad22d2d commit c7fc06f

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lib/active_resource/http_mock.rb

+22-11
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ def initialize(responses)
5858
end
5959

6060
[ :post, :patch, :put, :get, :delete, :head ].each do |method|
61-
# def post(path, request_headers = {}, body = nil, status = 200, response_headers = {})
62-
# @responses[Request.new(:post, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
61+
# def post(path, request_headers = {}, body = nil, status = 200, response_headers = {}, options: {})
62+
# @responses[Request.new(:post, path, nil, request_headers, options)] = Response.new(body || "", status, response_headers)
6363
# end
6464
module_eval <<-EOE, __FILE__, __LINE__ + 1
65-
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
66-
request = Request.new(:#{method}, path, nil, request_headers)
65+
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}, options = {})
66+
request = Request.new(:#{method}, path, nil, request_headers, options)
6767
response = Response.new(body || "", status, response_headers)
6868
6969
delete_duplicate_responses(request)
@@ -244,8 +244,8 @@ def net_connection_disabled?
244244
{ true => %w(post patch put),
245245
false => %w(get delete head) }.each do |has_body, methods|
246246
methods.each do |method|
247-
# def post(path, body, headers)
248-
# request = ActiveResource::Request.new(:post, path, body, headers)
247+
# def post(path, body, headers, options = {})
248+
# request = ActiveResource::Request.new(:post, path, body, headers, options)
249249
# self.class.requests << request
250250
# if response = self.class.responses.assoc(request)
251251
# response[1]
@@ -254,8 +254,8 @@ def net_connection_disabled?
254254
# end
255255
# end
256256
module_eval <<-EOE, __FILE__, __LINE__ + 1
257-
def #{method}(path, #{'body, ' if has_body}headers)
258-
request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers)
257+
def #{method}(path, #{'body, ' if has_body}headers, options = {})
258+
request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers, options)
259259
self.class.requests << request
260260
if response = self.class.responses.assoc(request)
261261
response[1]
@@ -279,18 +279,29 @@ def inspect_responses # :nodoc:
279279
class Request
280280
attr_accessor :path, :method, :body, :headers
281281

282-
def initialize(method, path, body = nil, headers = {})
283-
@method, @path, @body, @headers = method, path, body, headers
282+
def initialize(method, path, body = nil, headers = {}, options = {})
283+
@method, @path, @body, @headers, @options = method, path, body, headers, options
284284
end
285285

286286
def ==(req)
287-
path == req.path && method == req.method && headers_match?(req)
287+
if @options && @options[:omit_query_in_path]
288+
remove_query_params_from_path == req.remove_query_params_from_path && method == req.method && headers_match?(req)
289+
else
290+
path == req.path && method == req.method && headers_match?(req)
291+
end
288292
end
289293

290294
def to_s
291295
"<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>"
292296
end
293297

298+
# Removes query parameters from the path.
299+
#
300+
# @return [String] the path without query parameters
301+
def remove_query_params_from_path
302+
path.split("?").first
303+
end
304+
294305
private
295306
def headers_match?(req)
296307
# Ignore format header on equality if it's not defined

test/cases/http_mock_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ class HttpMockTest < ActiveSupport::TestCase
193193
assert_equal 2, ActiveResource::HttpMock.responses.length
194194
end
195195

196+
test "omits query parameters from the URL when options[:omit_query_params] is true" do
197+
ActiveResource::HttpMock.respond_to do |mock|
198+
mock.get("/endpoint", {}, "Response", 200, {}, { omit_query_in_path: true })
199+
end
200+
201+
response = request(:get, "/endpoint?param1=value1&param2=value2")
202+
203+
assert_equal "Response", response.body
204+
end
205+
196206
def request(method, path, headers = {}, body = nil)
197207
if method.in?([:patch, :put, :post])
198208
@http.send(method, path, body, headers)

0 commit comments

Comments
 (0)