Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check size of path segments against each configured route #3

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ module MyApp
Rack::IdempotencyKey,
store: Rack::IdempotencyKey::MemoryStore.new,
routes: [
{ path: "/posts", method: "POST" },
{ path: "/posts/*", method: "PATCH" }
{ path: "/posts", method: "POST" },
{ path: "/posts/*", method: "PATCH" },
{ path: "/posts/*/comments", method: "POST" }
]
)
end
Expand Down Expand Up @@ -120,8 +121,9 @@ Each route entry must be compliant with what follows:

```ruby
routes: [
{ path: "/posts", method: "POST" },
{ path: "/posts/*", method: "PATCH" }
{ path: "/posts", method: "POST" },
{ path: "/posts/*", method: "PATCH" },
{ path: "/posts/*/comments", method: "POST" }
]
```

Expand Down
3 changes: 2 additions & 1 deletion lib/rack/idempotency_key/idempotent_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def idempotency_key
attr_reader :request, :routes

def matching_route?(route_path)
same_segments? segments(route_path)
route_segments = segments route_path
path_segments.size == route_segments.size && same_segments?(route_segments)
end

def matching_method?(route_method)
Expand Down
18 changes: 13 additions & 5 deletions spec/rack/idempotency_key/idempotent_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
let(:routes) { [] }
let(:idempotency_key) { "123456789" }

shared_context "with idempotency key in place" do
before { env["HTTP_IDEMPOTENCY_KEY"] = idempotency_key }
end

describe "#allowed?" do
context "with idempotency key over an allowed method and a matching route" do
include_context "with idempotency key in place"

let(:env_opts) { { method: "POST" } }
let(:env_uri) { "/posts" }
let(:routes) { [{ path: "/posts", method: "POST" }] }

before { env["HTTP_IDEMPOTENCY_KEY"] = idempotency_key }

it { is_expected.to be_allowed }
end

Expand All @@ -35,16 +39,20 @@
end

context "with a not allowed request method" do
include_context "with idempotency key in place"

let(:env_uri) { "/posts" }
let(:routes) { [{ path: "/posts", method: "GET" }] }

it { is_expected.not_to be_allowed }
end

context "without a matching route" do
include_context "with idempotency key in place"

let(:env_opts) { { method: "POST" } }
let(:env_uri) { "/posts/1/authors" }
let(:routes) { [{ path: "/posts", method: "POST" }] }
let(:routes) { [{ path: "/posts/*", method: "POST" }] }

it { is_expected.not_to be_allowed }
end
Expand Down Expand Up @@ -100,7 +108,7 @@

describe "#idempotency_key?" do
context "with the idempotency key" do
before { env["HTTP_IDEMPOTENCY_KEY"] = idempotency_key }
include_context "with idempotency key in place"

it { is_expected.to be_idempotency_key }
end
Expand All @@ -112,7 +120,7 @@

describe "#idempotency_key" do
context "with the idempotency key" do
before { env["HTTP_IDEMPOTENCY_KEY"] = idempotency_key }
include_context "with idempotency key in place"

it { expect(idempotent_request.idempotency_key).to eq(idempotency_key) }
end
Expand Down