Commit 48a6cd84fd5edfaace88f906025963637f5c3c21
Exists in
colab
and in
4 other branches
Merge pull request #224 from mezuro/handle_unknown_format
Page not found handler robust to unknown formats
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,9 +32,13 @@ class ApplicationController < ActionController::Base | ||
| 32 | protected | 32 | protected |
| 33 | 33 | ||
| 34 | def not_found | 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 | end | 42 | end |
| 39 | end | 43 | end |
| 40 | 44 |
spec/controllers/projects_controller_spec.rb
| @@ -61,9 +61,9 @@ describe ProjectsController, :type => :controller do | @@ -61,9 +61,9 @@ describe ProjectsController, :type => :controller do | ||
| 61 | 61 | ||
| 62 | describe 'show' do | 62 | describe 'show' do |
| 63 | let(:project) { FactoryGirl.build(:project_with_id) } | 63 | let(:project) { FactoryGirl.build(:project_with_id) } |
| 64 | + let(:repository) { FactoryGirl.build(:repository) } | ||
| 64 | 65 | ||
| 65 | context 'when the project exists' do | 66 | context 'when the project exists' do |
| 66 | - let(:repository) { FactoryGirl.build(:repository) } | ||
| 67 | before :each do | 67 | before :each do |
| 68 | Project.expects(:find).with(project.id).returns(project) | 68 | Project.expects(:find).with(project.id).returns(project) |
| 69 | project.expects(:repositories).returns(repository) | 69 | project.expects(:repositories).returns(repository) |
| @@ -76,11 +76,25 @@ describe ProjectsController, :type => :controller do | @@ -76,11 +76,25 @@ describe ProjectsController, :type => :controller do | ||
| 76 | context 'when the project does not exists' do | 76 | context 'when the project does not exists' do |
| 77 | before :each do | 77 | before :each do |
| 78 | Project.expects(:find).with(project.id).raises(KalibroClient::Errors::RecordNotFound) | 78 | Project.expects(:find).with(project.id).raises(KalibroClient::Errors::RecordNotFound) |
| 79 | - get :show, :id => project.id | ||
| 80 | end | 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 | end | 96 | end |
| 97 | + | ||
| 84 | end | 98 | end |
| 85 | 99 | ||
| 86 | describe 'destroy' do | 100 | describe 'destroy' do |