Skip to content

Commit

Permalink
Merge pull request #524 from alphagov/catch-invalid-uri-error
Browse files Browse the repository at this point in the history
Redirect to home page on invalid urls
  • Loading branch information
h-lame authored Aug 16, 2016
2 parents 0ce14f1 + ba7c090 commit 6f4e161
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 6f4e161

Please sign in to comment.