Skip to content

Commit 6459ea8

Browse files
committed
Return 404 when action doesn't exist
1 parent 1bb4f35 commit 6459ea8

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

lib/inertia_rails/middleware.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def redirect_status?(status)
4747
end
4848

4949
def non_get_redirectable_method?
50-
['PUT', 'PATCH', 'DELETE'].include? request_method
50+
%w[PUT PATCH DELETE].include? request_method
5151
end
5252

5353
def inertia_non_post_redirect?(status)
@@ -63,7 +63,7 @@ def get?
6363
end
6464

6565
def controller
66-
@env["action_controller.instance"]
66+
@env['action_controller.instance']
6767
end
6868

6969
def request_method
@@ -83,7 +83,7 @@ def version_stale?
8383
end
8484

8585
def server_version
86-
controller&.send(:inertia_configuration)&.version
86+
(controller&.send(:inertia_configuration) || InertiaRails.configuration).version
8787
end
8888

8989
def coerce_version(version)
@@ -92,7 +92,7 @@ def coerce_version(version)
9292

9393
def force_refresh(request)
9494
request.flash.keep
95-
Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
95+
Rack::Response.new('', 409, { 'X-Inertia-Location' => request.original_url }).finish
9696
end
9797

9898
def copy_xsrf_to_csrf!

spec/inertia/middleware_spec.rb

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
1+
# frozen_string_literal: true
2+
13
RSpec.describe InertiaRails::Middleware, type: :request do
2-
context 'the version is stale' do
3-
it 'tells the client to refresh' do
4-
get empty_test_path, headers: {'X-Inertia' => true, 'X-Inertia-Version' => 'blkajdf'}
4+
context 'the version is set' do
5+
with_inertia_config version: '1.0'
6+
7+
it 'tells the client with stale version to refresh' do
8+
get empty_test_path, headers: { 'X-Inertia' => true, 'X-Inertia-Version' => 'stale' }
59

610
expect(response.status).to eq 409
711
expect(response.headers['X-Inertia-Location']).to eq request.original_url
812
end
13+
14+
it 'returns page when version is up to date' do
15+
get empty_test_path, headers: { 'X-Inertia' => true, 'X-Inertia-Version' => '1.0' }
16+
17+
expect(response.status).to eq 200
18+
end
19+
20+
it 'returns response for non-inertia requests' do
21+
get empty_test_path, headers: { 'X-Inertia-Version' => 'stale' }
22+
23+
expect(response.status).to eq 200
24+
end
25+
26+
it 'returns 404 on unknown route' do
27+
expect do
28+
get '/unknown_route', headers: { 'X-Inertia' => true, 'X-Inertia-Version' => '1.0' }
29+
end.to raise_error(ActionController::RoutingError)
30+
end
931
end
1032

1133
context 'a redirect status was passed with an http method that preserves itself on 302 redirect' do
1234
subject { response.status }
1335

1436
context 'PUT' do
15-
before { put redirect_test_path, headers: {'X-Inertia' => true} }
37+
before { put redirect_test_path, headers: { 'X-Inertia' => true } }
1638

1739
it { is_expected.to eq 303 }
1840
end
1941

2042
context 'PATCH' do
21-
before { patch redirect_test_path, headers: {'X-Inertia' => true} }
43+
before { patch redirect_test_path, headers: { 'X-Inertia' => true } }
2244

2345
it { is_expected.to eq 303 }
2446
end
2547

2648
context 'DELETE' do
27-
before { delete redirect_test_path, headers: {'X-Inertia' => true} }
49+
before { delete redirect_test_path, headers: { 'X-Inertia' => true } }
2850

2951
it { is_expected.to eq 303 }
3052
end
@@ -47,12 +69,4 @@
4769
expect(statusses.uniq).to eq([303])
4870
end
4971
end
50-
51-
context 'a request not originating from inertia' do
52-
it 'is ignored' do
53-
get empty_test_path, headers: {'X-Inertia-Version' => 'blkajdf'}
54-
55-
expect(response.status).to eq 200
56-
end
57-
end
5872
end

0 commit comments

Comments
 (0)