Skip to content

Commit

Permalink
Merge pull request #29 from ScottyRJames/match-path-with-special-char…
Browse files Browse the repository at this point in the history
…acters

Add functionality to use paths that contains special parentheses characters
  • Loading branch information
LindseySaari authored Oct 27, 2022
2 parents 34488d1 + f6c2c79 commit e4b71ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/betamocks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ def service_by_host_port(env)
end

def matches_path(endpoint, method, path)
/\A#{endpoint[:path].gsub('/', '\/').gsub('*', '[^\/]*')}\z/ =~ path && endpoint[:method] == method
replacement_map = {
'/' => '\/',
'(' => '\(',
')' => '\)',
'*' => '[^\/]*'
}

endpoint_path = endpoint[:path]
replacement_map.each_pair do |original, replacement|
endpoint_path = endpoint_path.gsub(original, replacement)
end

/\A#{endpoint_path}\z/ =~ path && endpoint[:method] == method
end

def matches_request_params(endpoint, env)
Expand Down
2 changes: 1 addition & 1 deletion lib/betamocks/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Betamocks
VERSION = '0.9.0'
VERSION = '0.9.1'
end
10 changes: 10 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@
end
end
end

context 'with regex special characters' do
let(:env) { double('Faraday::Env') }
let(:url) { URI("http://animal.pics/get_animals(class='reptilia',pagination=true)") }

it 'returns the special character URL' do
endpoint = Betamocks.configuration.find_endpoint(env)
expect(endpoint).to eq(method: :get, path: "/get_animals(class='reptilia',pagination=true)", file_path: '/pics/reptiles')
end
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/support/betamocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
:uid_location: body
:uid_locator: '<AnimalType>Gorilla<\/AnimalType><Id>(\d{8})'
:optional_code_locator: '<Quality>"(HI-DEF|LO-DEF)"</Quality>'
- :method: :get
:path: "/get_animals(class='reptilia',pagination=true)"
:file_path: "/pics/reptiles"

# garbage.day
- :base_uri: garbage.day:80
Expand Down

0 comments on commit e4b71ca

Please sign in to comment.