-
Notifications
You must be signed in to change notification settings - Fork 0
Extend OkComputer to enable a HEAD check #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06e345b
extend okcomputer::httpcheck for head requests
jason-raitz 8c1f02f
update metadata and add spec
jason-raitz 367d787
remove unnecessary reference
jason-raitz 1a70da9
Add timeout support to URIs request methods and update tests
jason-raitz 310ed43
Update changelog for version 0.2.1 and fix a coverage issue
jason-raitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ~> 3.3 | ||
| 3.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| Dir.glob(File.expand_path('util/*.rb', __dir__)).sort.each(&method(:require)) | ||
| Dir.glob(File.expand_path('util/*.rb', __dir__)).each(&method(:require)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| require 'berkeley_library/util/uris' | ||
|
|
||
| module BerkeleyLibrary | ||
| module Util | ||
| # :nocov: | ||
| if defined?(::OkComputer) | ||
| class HeadCheck < ::OkComputer::HttpCheck | ||
jason-raitz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def perform_request | ||
| headers = {} | ||
| if basic_auth_options.any? | ||
| user, password = basic_auth_options | ||
| headers['Authorization'] = "Basic #{Base64.strict_encode64("#{user}:#{password}")}" | ||
| end | ||
|
|
||
| options = { headers: headers, log: false } | ||
| options[:timeout] = request_timeout.to_i if request_timeout | ||
|
|
||
| URIs.head_response(url, **options) | ||
| rescue StandardError => e | ||
| raise OkComputer::HttpCheck::ConnectionFailed, e | ||
| end | ||
| end | ||
| end | ||
| # :nocov: | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| require 'spec_helper' | ||
| require 'okcomputer' | ||
| require 'berkeley_library/util/uris/head_check' | ||
| require 'base64' | ||
|
|
||
| module BerkeleyLibrary | ||
| module Util | ||
| RSpec.describe HeadCheck do | ||
| let(:url) { 'http://example.com' } | ||
| let(:check) { described_class.new(url) } | ||
| let(:mock_response) { instance_double(RestClient::Response) } | ||
|
|
||
| before do | ||
| allow(BerkeleyLibrary::Util::URIs).to receive(:head_response).and_return(mock_response) | ||
| end | ||
|
|
||
| describe '#perform_request' do | ||
| context 'without basic auth' do | ||
| it 'does not add Authorization header' do | ||
| check.perform_request | ||
| expect(BerkeleyLibrary::Util::URIs).not_to have_received(:head_response).with(anything, hash_including('Authorization' => anything), anything) | ||
| end | ||
|
|
||
| it 'calls URIs.head_response with the correct URL' do | ||
| check.perform_request | ||
| expect(BerkeleyLibrary::Util::URIs).to have_received(:head_response).with(URI(url), headers: {}, log: false, timeout: 5) | ||
| end | ||
| end | ||
|
|
||
| context 'with basic auth' do | ||
| let(:user) { 'user' } | ||
| let(:password) { 'pass' } | ||
|
|
||
| # Stub the configuration on the instance directly | ||
| before do | ||
| allow(check).to receive(:basic_auth_options).and_return([user, password]) | ||
| end | ||
|
|
||
| it 'adds the Authorization header' do | ||
| expected_headers = { 'Authorization' => "Basic #{Base64.strict_encode64("#{user}:#{password}")}" } | ||
|
|
||
| check.perform_request | ||
| expect(BerkeleyLibrary::Util::URIs).to have_received(:head_response).with(URI(url), headers: expected_headers, log: false, timeout: 5) | ||
| end | ||
| end | ||
|
|
||
| context 'when URIs.head_response raises an error' do | ||
| let(:error_message) { 'Something went wrong' } | ||
|
|
||
| before do | ||
| allow(BerkeleyLibrary::Util::URIs).to receive(:head_response).and_raise(StandardError, error_message) | ||
| end | ||
|
|
||
| it 'raises an OkComputer::HttpCheck::ConnectionFailed error' do | ||
| expect { check.perform_request }.to raise_error(OkComputer::HttpCheck::ConnectionFailed, error_message) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.