Skip to content

Commit

Permalink
Merge pull request #83 from llhhaa/fix_pathparam_regex
Browse files Browse the repository at this point in the history
Make path param regex more strict
  • Loading branch information
bwillis committed Mar 11, 2020
2 parents ebd1a07 + a9851cd commit 057ad3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/versioncake/strategies/path_parameter_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PathParameterStrategy < ExtractionStrategy
def execute(request)
version = nil
request.path.split('/').find do |part|
next unless match = part.match(%r{v(?<version>\d+)})
next unless match = part.match(%r{\Av(?<version>\d+)\z})
version = match[:version]
break
end
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/strategies/path_parameter_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
it { is_expected.to eq 11 }
end

context "a request with a substring matching /v\d+/ returns nil" do
context "as a postpended string" do
let(:request) { instance_double('Request', path: 'parameter/aav11/parameter') }

it { is_expected.to be_nil }
end

context "as a prepended string" do
let(:request) { instance_double('Request', path: 'parameter/v11aa/parameter') }

it { is_expected.to be_nil }
end

context "as an interstital string" do
let(:request) { instance_double('Request', path: 'parameter/aav11aa/parameter') }

it { is_expected.to be_nil }
end
end

context "a request without an api_version path parameter returns nil" do
let(:request) { instance_double('Request', path: 'parameter/parameter') }

Expand Down

0 comments on commit 057ad3a

Please sign in to comment.