Commit 32ad2aae7355715737d313df107f32136ecdac2c
1 parent
07a3ceb8
Exists in
colab
and in
4 other branches
Page not found handler robust to unknown formats
Generally rails will deal with this, but since we are already on a rescue it will not handle to avoid an infinite loop. So we need to deal with the error.
Showing
2 changed files
with
24 additions
and
6 deletions
Show diff stats
app/controllers/application_controller.rb
... | ... | @@ -32,9 +32,13 @@ class ApplicationController < ActionController::Base |
32 | 32 | protected |
33 | 33 | |
34 | 34 | def not_found |
35 | - respond_to do |format| | |
36 | - format.html { render file: "#{Rails.root}/public/404", layout: false, status: :not_found } | |
37 | - format.json { head :not_found } | |
35 | + begin | |
36 | + respond_to do |format| | |
37 | + format.html { render file: "#{Rails.root}/public/404", layout: false, status: :not_found } | |
38 | + format.json { head :not_found } | |
39 | + end | |
40 | + rescue ActionController::UnknownFormat | |
41 | + render status: 404, text: "The page you were looking for doesn't exist (404)" | |
38 | 42 | end |
39 | 43 | end |
40 | 44 | ... | ... |
spec/controllers/projects_controller_spec.rb
... | ... | @@ -61,9 +61,9 @@ describe ProjectsController, :type => :controller do |
61 | 61 | |
62 | 62 | describe 'show' do |
63 | 63 | let(:project) { FactoryGirl.build(:project_with_id) } |
64 | + let(:repository) { FactoryGirl.build(:repository) } | |
64 | 65 | |
65 | 66 | context 'when the project exists' do |
66 | - let(:repository) { FactoryGirl.build(:repository) } | |
67 | 67 | before :each do |
68 | 68 | Project.expects(:find).with(project.id).returns(project) |
69 | 69 | project.expects(:repositories).returns(repository) |
... | ... | @@ -76,11 +76,25 @@ describe ProjectsController, :type => :controller do |
76 | 76 | context 'when the project does not exists' do |
77 | 77 | before :each do |
78 | 78 | Project.expects(:find).with(project.id).raises(KalibroClient::Errors::RecordNotFound) |
79 | - get :show, :id => project.id | |
80 | 79 | end |
81 | 80 | |
82 | - it { is_expected.to respond_with(:not_found) } | |
81 | + context 'when the request format is known' do | |
82 | + before :each do | |
83 | + get :show, :id => project.id | |
84 | + end | |
85 | + | |
86 | + it { is_expected.to respond_with(:not_found) } | |
87 | + end | |
88 | + | |
89 | + context 'when the request format is unknown' do | |
90 | + before :each do | |
91 | + get :show, id: project.id, format: :txt | |
92 | + end | |
93 | + | |
94 | + it { is_expected.to respond_with(:not_found) } | |
95 | + end | |
83 | 96 | end |
97 | + | |
84 | 98 | end |
85 | 99 | |
86 | 100 | describe 'destroy' do | ... | ... |