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 | 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 | ... | ... |