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

FI-1659: Follow redirects #231

Merged
merged 3 commits into from
Aug 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PATH
dry-container (= 0.8.0)
dry-system (= 0.18.1)
faraday (~> 1.2)
faraday_middleware (~> 1.2)
fhir_client (>= 5.0.3)
fhir_models (~> 4.2.0)
hanami-controller (~> 1.3)
Expand Down Expand Up @@ -116,6 +117,8 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)
faraday (~> 1.0)
fhir_client (5.0.3)
activesupport (>= 3)
addressable (>= 2.3)
Expand Down
1 change: 1 addition & 0 deletions inferno_core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'dry-container', '0.8.0'
spec.add_runtime_dependency 'dry-system', '0.18.1'
spec.add_runtime_dependency 'faraday', '~> 1.2'
spec.add_runtime_dependency 'faraday_middleware', '~> 1.2'
spec.add_runtime_dependency 'fhir_client', '>= 5.0.3'
spec.add_runtime_dependency 'fhir_models', '~> 4.2.0'
spec.add_runtime_dependency 'hanami-controller', '~> 1.3'
Expand Down
18 changes: 14 additions & 4 deletions lib/inferno/dsl/http_client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'faraday_middleware'

require_relative 'request_storage'
require_relative 'tcp_exception_handler'

Expand Down Expand Up @@ -75,14 +77,22 @@ def get(url = '', client: :default, name: nil, **options)
if client
client.get(url, nil, options[:headers])
elsif url.match?(%r{\Ahttps?://})
Faraday.get(url, nil, options[:headers])
connection.get(url, nil, options[:headers])
else
raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
end
end
end
end

# @private
def connection
Faraday.new do |f|
f.request :url_encoded
f.use FaradayMiddleware::FollowRedirects
end
end

# Perform an HTTP POST request
#
# @param url [String] if this request is using a defined client, this will
Expand All @@ -103,7 +113,7 @@ def post(url = '', body: nil, client: :default, name: nil, **options)
if client
client.post(url, body, options[:headers])
elsif url.match?(%r{\Ahttps?://})
Faraday.post(url, body, options[:headers])
connection.post(url, body, options[:headers])
else
raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
end
Expand All @@ -128,7 +138,7 @@ def delete(url = '', client: :default, name: :nil, **options)
if client
client.delete(url, nil, options[:headers])
elsif url.match?(%r{\Ahttps?://})
Faraday.delete(url, nil, options[:headers])
connection.delete(url, nil, options[:headers])
else
raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
end
Expand Down Expand Up @@ -167,7 +177,7 @@ def stream(block, url = '', limit = 100, client: :default, name: nil, **options)
if client
response = client.get(url, nil, options[:headers]) { |req| req.options.on_data = collector }
elsif url.match?(%r{\Ahttps?://})
response = Faraday.get(url, nil, options[:headers]) { |req| req.options.on_data = collector }
response = connection.get(url, nil, options[:headers]) { |req| req.options.on_data = collector }
else
raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
end
Expand Down
7 changes: 6 additions & 1 deletion lib/inferno/dsl/http_client_builder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'faraday_middleware'

module Inferno
module DSL
# This module contains the HTTP DSL available to test writers.
Expand All @@ -12,7 +14,10 @@ def build(runnable, block)
params = { url: url }
params.merge!(headers: headers) if headers

Faraday.new(params)
Faraday.new(params) do |f|
f.request :url_encoded
f.use FaradayMiddleware::FollowRedirects
end
end

# Define the base url for an HTTP client. A string or symbol can be
Expand Down
44 changes: 44 additions & 0 deletions spec/inferno/dsl/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ def setup_default_client
setup_default_client
end

it 'follows redirects' do
original_url = 'http://example.com'
new_url = 'http://example.com/abc'
original_request =
stub_request(:get, original_url)
.to_return(status: 302, headers: { 'Location' => new_url })
new_request =
stub_request(:get, new_url)
.to_return(status: 200, body: 'BODY')

group.get(original_url)
expect(original_request).to have_been_made.once
expect(new_request).to have_been_made.once

expect(group.requests.length).to eq(1)

request = group.request
expect(request.url).to eq(new_url)
expect(request.status).to eq(200)
expect(request.response_body).to eq('BODY')
end

context 'without a url argument' do
let(:stub_get_request) do
stub_request(:get, base_url)
Expand Down Expand Up @@ -273,6 +295,28 @@ def setup_default_client
end.to raise_error(Faraday::ConnectionFailed, 'not a TCP error')
end
end

it 'follows redirects' do
original_url = 'http://example.com'
new_url = 'http://example.com/abc'
original_request =
stub_request(:get, original_url)
.to_return(status: 302, headers: { 'Location' => new_url })
new_request =
stub_request(:get, new_url)
.to_return(status: 200, body: 'BODY')

group.get(original_url)
expect(original_request).to have_been_made.once
expect(new_request).to have_been_made.once

expect(group.requests.length).to eq(1)

request = group.request
expect(request.url).to eq(new_url)
expect(request.status).to eq(200)
expect(request.response_body).to eq('BODY')
end
end
end

Expand Down