|
| 1 | +require 'rails_helper' |
| 2 | +require_relative '../../lib/http_head_check' |
| 3 | + |
| 4 | +RSpec.describe GeoDataHealthCheck::HttpHeadCheck do |
| 5 | + let(:url) { 'https://example.com/endpoint' } |
| 6 | + subject(:check) { described_class.new(url) } |
| 7 | + |
| 8 | + describe 'initialization' do |
| 9 | + it 'sets the URL' do |
| 10 | + expect(check.url).to eq url |
| 11 | + end |
| 12 | + |
| 13 | + it 'sets default timeout to 5 seconds' do |
| 14 | + expect(check.request_timeout).to eq 5 |
| 15 | + end |
| 16 | + |
| 17 | + it 'allows custom timeout' do |
| 18 | + check = described_class.new(url, 10) |
| 19 | + expect(check.request_timeout).to eq 10 |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + describe '#check' do |
| 24 | + context 'when request returns 200 OK' do |
| 25 | + it 'marks check as successful' do |
| 26 | + response = Net::HTTPOK.new('1.1', 200, 'OK') |
| 27 | + allow(check).to receive(:perform_request).and_return(response) |
| 28 | + |
| 29 | + check.check |
| 30 | + |
| 31 | + expect(check.message).to include('Http head check successful.') |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'when request returns 500 error' do |
| 36 | + it 'marks check as failed' do |
| 37 | + response = Net::HTTPInternalServerError.new('1.1', 500, 'Error') |
| 38 | + allow(check).to receive(:perform_request).and_return(response) |
| 39 | + |
| 40 | + check.check |
| 41 | + |
| 42 | + expect(check.message).to include('Error') |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + end |
| 47 | + |
| 48 | + describe '#perform_request' do |
| 49 | + it 'Net::OpenTimeout with ConnectionFailed and formated message' do |
| 50 | + check_with_timeout = described_class.new(url, 7) |
| 51 | + allow(check_with_timeout).to receive(:head_request).and_raise(Net::OpenTimeout.new('open timeout')) |
| 52 | + |
| 53 | + expect { check_with_timeout.perform_request }.to raise_error( |
| 54 | + GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, |
| 55 | + a_string_including('did not respond within 7 seconds: open timeout') |
| 56 | + ) |
| 57 | + end |
| 58 | + |
| 59 | + it 'Net::ReadTimeout with ConnectionFailed and formated message' do |
| 60 | + check_with_timeout = described_class.new(url, 9) |
| 61 | + allow(check_with_timeout).to receive(:head_request).and_raise(Net::ReadTimeout.new('read timeout')) |
| 62 | + |
| 63 | + expect { check_with_timeout.perform_request }.to raise_error( |
| 64 | + GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, |
| 65 | + a_string_including('did not respond within 9 seconds: Net::ReadTimeout') |
| 66 | + ) |
| 67 | + end |
| 68 | + |
| 69 | + it 'StandardError and passes through the message' do |
| 70 | + err_msg = 'generic failure' |
| 71 | + allow(check).to receive(:head_request).and_raise(StandardError, err_msg) |
| 72 | + |
| 73 | + expect { check.perform_request }.to raise_error( |
| 74 | + GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, |
| 75 | + err_msg |
| 76 | + ) |
| 77 | + end |
| 78 | + |
| 79 | + it 'wraps ArgumentError with ConnectionFailed and includes URL and error class' do |
| 80 | + bad_check = described_class.new(url) |
| 81 | + allow(bad_check).to receive(:head_request).and_raise(ArgumentError, 'invalid URI') |
| 82 | + |
| 83 | + expect { bad_check.perform_request }.to raise_error( |
| 84 | + GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, |
| 85 | + a_string_including("Invalid URL format for '#{url}'", 'ArgumentError', 'invalid URI') |
| 86 | + ) |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments