Skip to content

Commit

Permalink
Handle non ascii characters in proxy (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
epugh authored Jun 26, 2024
1 parent d13a5ba commit 2911c2c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ruby '3.3.2'
gem 'activerecord-import', '>= 1.0.7'
gem 'active_storage_db'
gem 'acts_as_list', '>= 1.0.1'
gem 'addressable', '~> 2.8'
gem 'ancestry'
gem 'angular-rails-templates', '>= 1.0.0.beta'
gem 'apipie-rails', '~> 1.2'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ DEPENDENCIES
active_storage_db
activerecord-import (>= 1.0.7)
acts_as_list (>= 1.0.1)
addressable (~> 2.8)
ancestry
angular-rails-templates (>= 1.0.0.beta)
annotate
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/proxy_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'faraday'
require 'faraday/follow_redirects'

require 'addressable/uri'

# rubocop:disable Layout/LineLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
Expand All @@ -23,7 +25,8 @@ def fetch

proxy_debug = 'true' == params[:proxy_debug]

uri = URI.parse(url_param)
# we use Addressable::URI instead of straight up URI to support non ascii characters like café
uri = Addressable::URI.parse(url_param)
url_without_path = "#{uri.scheme}://#{uri.host}"
url_without_path += ":#{uri.port}" unless uri.port.nil?

Expand Down
3 changes: 3 additions & 0 deletions app/views/search_endpoints/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,6 @@
background-color: red;
}
</style>


<p/>
13 changes: 5 additions & 8 deletions app/views/search_endpoints/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

<%= render 'form', search_endpoint: @search_endpoint %>

<br>

<%= button_to 'Cancel', @search_endpoint, method: :get %>

<br>

<%= button_to 'Delete Search Endpoint', @search_endpoint, method: :delete, data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>

<div>
<%= link_to "Cancel", @search_endpoint %> |
<%= link_to "Back to search endpoints", search_endpoints_path %>
<p/>
<%= button_to "Destroy this search endpoint", @search_endpoint, method: :delete, class: 'btn btn-sm btn-danger' %>
</div>
7 changes: 4 additions & 3 deletions app/views/search_endpoints/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</div>

<%= render "form", search_endpoint: @search_endpoint %>
<br>
<%= button_to 'Back to Search Endpoints', search_endpoints_path, method: :get %>
<br>

<div>
<%= link_to "Back to search endpoints", search_endpoints_path %>
</div>
4 changes: 2 additions & 2 deletions app/views/search_endpoints/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Search Endpoints represents a search engine (or API!) that is receiving queries
<div>
<%= link_to "Edit this search endpoint", edit_search_endpoint_path(@search_endpoint) %> |
<%= link_to "Back to search endpoints", search_endpoints_path %>

<%= button_to "Destroy this search endpoint", @search_endpoint, method: :delete %>
<p/>
<%= button_to "Destroy this search endpoint", @search_endpoint, method: :delete, class: 'btn btn-sm btn-danger' %>
</div>
1 change: 1 addition & 0 deletions lib/tasks/sample_data.thor
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class SampleData < Thor
osc.members << osc_member_user unless osc.members.include?(osc_member_user)
osc.members << realistic_activity_user unless osc.members.include?(realistic_activity_user)
osc.cases << tens_of_queries_case unless osc.members.include?(tens_of_queries_case)
osc.search_endpoints << statedecoded_solr_endpoint unless osc.search_endpoints.include?(statedecoded_solr_endpoint)
print_step 'End of seeding teams................'

# Books
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/proxy_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class ProxyControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test 'should be able to handle a get with non ASCII characters' do
get proxy_fetch_url params: {
url: 'http://solr.quepid.com:8983/solr/statedecoded/select?q=At dusk, the café transformed into an impromptu stage', 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
14 changes: 14 additions & 0 deletions test/support/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ def setup
# Demonstrate server error
stub_request(:get, 'https://localhost:9999/')
.to_raise(Faraday::ConnectionFailed.new('Failed to connect'))

# Testing out handline of café as a non ascii character
stub_request(:get, 'http://solr.quepid.com:8983/solr/statedecoded/select?fl=id,text&q=At%20dusk,%20the%20caf%C3%A9%20transformed%20into%20an%20impromptu%20stage&rows=10&start=0')
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Cookie' => '',
'Https' => 'off',
'User-Agent' => 'Faraday v2.9.0',
}
)
.to_return(status: 200, body: '', headers: {})
end

# rubocop:enable Metrics/MethodLength
Expand Down

0 comments on commit 2911c2c

Please sign in to comment.