Skip to content

Commit

Permalink
Redirect to home page on invalid urls
Browse files Browse the repository at this point in the history
Every now and again we get invalid urls which causes the check for
unknown formats to blow up. In these circumstances we should catch
the invalid URI error and redirect to the home page.
  • Loading branch information
pixeltrix committed Aug 15, 2016
1 parent 0ce14f1 commit ba7c090
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def url_without_format
URI.parse(request.original_url).tap do |uri|
uri.path = File.join(File.dirname(request.path), File.basename(request.path, '.*'))
end.to_s
rescue URI::InvalidURIError => e
home_url
end

def redirect_to_url_without_format
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ def index
expect(controller.class.helpers).to respond_to :public_petition_facets
end
end

context "when the url has an invalid format" do
let(:exception) { URI::InvalidURIError.new }

before do
allow(request).to receive(:format).and_return(nil)
allow(request).to receive(:original_url).and_return("https://petition.parliament.uk/petitions.json]")
end

it "redirects to the home page" do
get :index
expect(response).to redirect_to("https://petition.parliament.uk/")
end
end
end

0 comments on commit ba7c090

Please sign in to comment.