Skip to content

Commit

Permalink
fix: check size of path segments against each configured route (#3)
Browse files Browse the repository at this point in the history
* fix: check size of path segments against each configured route

* docs: update README.md
  • Loading branch information
matteoredz committed Mar 31, 2023
1 parent d9ac412 commit 248d6ca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
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

0 comments on commit 248d6ca

Please sign in to comment.