diff --git a/README.md b/README.md index 071efa1..9e1ef37 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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" } ] ``` diff --git a/lib/rack/idempotency_key/idempotent_request.rb b/lib/rack/idempotency_key/idempotent_request.rb index 5705087..b4412d2 100644 --- a/lib/rack/idempotency_key/idempotent_request.rb +++ b/lib/rack/idempotency_key/idempotent_request.rb @@ -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) diff --git a/spec/rack/idempotency_key/idempotent_request_spec.rb b/spec/rack/idempotency_key/idempotent_request_spec.rb index 63b66cf..cbda024 100644 --- a/spec/rack/idempotency_key/idempotent_request_spec.rb +++ b/spec/rack/idempotency_key/idempotent_request_spec.rb @@ -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 @@ -35,6 +39,8 @@ 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" }] } @@ -42,9 +48,11 @@ 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 @@ -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 @@ -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