Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.2.1 (2026-02-02)

- Add `URIs::HeadCheck`, an OkComputer check that performs a HEAD request
to verify the availability of a URL, with optional basic authentication.

# 0.2.0 (2025-07-24)

- Update to support Ruby 3.3+.
Expand Down
1 change: 1 addition & 0 deletions berkeley_library-util.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'ci_reporter_rspec', '~> 1.0'
spec.add_development_dependency 'colorize', '~> 1.0'
spec.add_development_dependency 'dotenv', '~> 2.7'
spec.add_development_dependency 'okcomputer', '~> 1.19'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.10'
spec.add_development_dependency 'rubocop', '~> 1.78.0'
Expand Down
6 changes: 3 additions & 3 deletions lib/berkeley_library/util/module_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module BerkeleyLibrary
module Util
class ModuleInfo
NAME = 'berkeley_library-util'.freeze
AUTHORS = ['David Moles', 'maría a. matienzo'].freeze
AUTHOR_EMAILS = ['dmoles@berkeley.edu', 'matienzo@berkeley.edu'].freeze
AUTHORS = ['David Moles', 'maría a. matienzo', 'Jason Raitz'].freeze
AUTHOR_EMAILS = ['dmoles@berkeley.edu', 'matienzo@berkeley.edu', 'raitz@berkeley.edu'].freeze
SUMMARY = 'Miscellaneous Ruby utilities for the UC Berkeley Library'.freeze
DESCRIPTION = 'A collection of miscellaneous Ruby routines for the UC Berkeley Library.'.freeze
LICENSE = 'MIT'.freeze
VERSION = '0.2.0'.freeze
VERSION = '0.2.1'.freeze
HOMEPAGE = 'https://github.com/BerkeleyLibrary/util'.freeze
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/berkeley_library/util/uris/head_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'berkeley_library/util/uris'

module BerkeleyLibrary
module Util
# :nocov:
if defined?(::OkComputer)
class HeadCheck < ::OkComputer::HttpCheck
Comment thread
jason-raitz marked this conversation as resolved.

def perform_request
headers = {}
if basic_auth_options.any?
user, password = basic_auth_options
headers['Authorization'] = "Basic #{Base64.strict_encode64("#{user}:#{password}")}"
end

URIs.head_response(url, headers: headers, log: false)
rescue StandardError => e
raise OkComputer::HttpCheck::ConnectionFailed, e.message
end
end
end
# :nocov:
end
end
61 changes: 61 additions & 0 deletions spec/berkeley_library/util/uris/head_check_spec.rb
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)
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)
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
Loading