Skip to content

Commit

Permalink
better handling of question marks
Browse files Browse the repository at this point in the history
  • Loading branch information
epugh committed Nov 1, 2023
1 parent 6a05b78 commit c821cee
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 137 deletions.
15 changes: 11 additions & 4 deletions app/controllers/proxy_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ class ProxyController < ApplicationController
def fetch
url_param = proxy_url_params

puts "HERE IS THE url_param:#{url_param}"

uri = URI.parse(url_param)
url_without_path = "#{uri.scheme}://#{uri.host}"
url_without_path += ":#{uri.port}" unless uri.port.nil?

puts "url_without_path: #{url_without_path}"
connection = Faraday.new(url: url_without_path) do |faraday|
# Configure the connection options, such as headers or middleware
# faraday.response :logger, nil, { headers: true, bodies: true }
faraday.response :logger, nil, { headers: true, bodies: true, errors: true }
faraday.response :logger, nil, { headers: true, bodies: false, errors: true }
faraday.ssl.verify = false
faraday.request :url_encoded

Expand Down Expand Up @@ -55,8 +57,13 @@ def fetch
# we get http://localhost:3000/proxy/fetch?url=http://myserver.com/search?query=text&rows=10
# which means the parameter "query=text" is lost because the URL is parsed and this part is dropped,
# so here we add this one parameter back in if we have it.
if url_param.include?('?') && !url_param.ends_with?('?')
extra_query_param = url_param.split('?')[1].split('=')
puts "url_param.include?('?'): #{url_param.include?('?')}"
puts "!url_param.ends_with?('?'): #{!url_param.ends_with?('?')}"
if url_param.include?('?')
# sometimes our url looks like http://myserver.com/search?q=tiger
# But it could also be http://myserver.com/search?q=tiger? and that needs handling via the special .split
extra_query_param = url_param.split('?', 2).last.split('=')

req.params[extra_query_param.first] = extra_query_param.second
end
unless body_params.empty?
Expand Down
14 changes: 14 additions & 0 deletions test/controllers/proxy_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class ProxyControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test 'should be able to handle a get with a ? character in the query' do
get proxy_fetch_url params: {
url: 'http://solr.quepid.com:8983/solr/statedecoded/select?q=tiger?', fl: 'id,text', rows: 10, start: 0
}
assert_response :success
end

test 'should be able to handle a get with spaces in the query' do
get proxy_fetch_url params: {
url: 'http://solr.quepid.com:8983/solr/statedecoded/select?q=can I own a tiger', fl: 'id,text', rows: 10, start: 0
}
assert_response :success
end

test 'should be able to handle a post' do
json_data = { query: 'trek', key2: 'value2' }.to_json

Expand Down
2 changes: 1 addition & 1 deletion test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class UserTest < ActiveSupport::TestCase

new_user = User.create(email: '[email protected]', password: 'password')
assert_includes new_user.errors.messages[:email], 'has already been taken'

new_user = User.create(email: '[email protected]', password: 'password')
assert_includes new_user.errors.messages[:email], 'has already been taken'
end
Expand Down
Loading

0 comments on commit c821cee

Please sign in to comment.