Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent invalid query strings in referrers from erroring #239

Merged
merged 1 commit into from May 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/helpers/referrer_helper.rb
Expand Up @@ -21,7 +21,7 @@ def extract_search_term(referrer)
uri = URI.parse(referrer)
params = Rack::Utils.parse_query(uri.query)
params['q'] if params['q'].present?
rescue URI::InvalidURIError
rescue URI::InvalidURIError, ArgumentError
nil
end
end
18 changes: 14 additions & 4 deletions spec/helpers/referrer_helper_spec.rb
Expand Up @@ -19,12 +19,22 @@
expect(friendly_referrer('https://www.gov.uk/bank-holidays')).to eq('/bank-holidays')
expect(friendly_referrer('https://www.gov.uk/')).to eq('/')
end

it('can handle incorrectly encoded query strings') do
expect(friendly_referrer('https://www.gov.uk/search?q=Taxed%20to%2')).to eq('/search')
end
end

it('can extract a search term from a referrer') do
expect(extract_search_term('https://www.gov.uk/search?q=Bank+holidays')).to eq('Bank holidays')
expect(extract_search_term('http://www.google.co.uk/search?q=public+holidays+in+uk&hl=en-GB&gbv=2&oq=&gs_l=')).to eq('public holidays in uk')
expect(extract_search_term('http://www.bing.com/search?q=4th+May+2015+Bank+Holiday&FORM=QSRE1')).to eq('4th May 2015 Bank Holiday')
context('extract_search_term') do
it('can extract a search term from a referrer') do
expect(extract_search_term('https://www.gov.uk/search?q=Bank+holidays')).to eq('Bank holidays')
expect(extract_search_term('http://www.google.co.uk/search?q=public+holidays+in+uk&hl=en-GB&gbv=2&oq=&gs_l=')).to eq('public holidays in uk')
expect(extract_search_term('http://www.bing.com/search?q=4th+May+2015+Bank+Holiday&FORM=QSRE1')).to eq('4th May 2015 Bank Holiday')
end

it('can handle incorrectly encoded query strings') do
expect(extract_search_term('https://www.gov.uk/search?q=Taxed%20to%2')).to eq(nil)
end
end

end