Skip to content

Commit

Permalink
use Utils.normalize_path() for Stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian D. Burns authored and technoweenie committed Dec 31, 2012
1 parent a27c952 commit 5d6faf7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
12 changes: 2 additions & 10 deletions lib/faraday/adapter/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def match(request_method, path, headers, body)
return false if !@stack.key?(request_method)
stack = @stack[request_method]
consumed = (@consumed[request_method] ||= [])
path = normalize_path(path)

if stub = matches?(stack, path, headers, body)
consumed << stack.delete(stub)
Expand Down Expand Up @@ -86,20 +85,13 @@ def verify_stubbed_calls
protected

def new_stub(request_method, path, headers = {}, body=nil, &block)
(@stack[request_method] ||= []) << Stub.new(normalize_path(path), headers, body, block)
normalized_path = Faraday::Utils.normalize_path(path)
(@stack[request_method] ||= []) << Stub.new(normalized_path, headers, body, block)
end

def matches?(stack, path, headers, body)
stack.detect { |stub| stub.matches?(path, headers, body) }
end

# ensure leading + trailing slash
def normalize_path(path)
path = '/' + path if path.index('/') != 0
path = path.sub('?', '/?')
path = path + '/' unless $&
path.gsub('//', '/')
end
end

class Stub < Struct.new(:path, :params, :headers, :body, :block)
Expand Down
5 changes: 3 additions & 2 deletions lib/faraday/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ def URI(url)
end
end

# Receives a URL and returns just the path with the query string sorted.
# Receives a String or URI and returns just the path with the query string sorted.
def normalize_path(url)
(url.path != "" ? url.path : "/") +
url = URI(url)
(url.path.start_with?('/') ? url.path : '/' + url.path) +
(url.query ? "?#{sort_query_params(url.query)}" : "")
end

Expand Down

0 comments on commit 5d6faf7

Please sign in to comment.