|
| 1 | +require 'spec_helper' |
| 2 | +require_relative '../../lib/http_head_check' |
| 3 | + |
| 4 | +RSpec.describe GeoDataHealthCheck::HttpHeadCheck do |
| 5 | + let(:test_url) { URI('http://example.com/endpoint') } |
| 6 | + let(:check) { described_class.new(test_url) } |
| 7 | + |
| 8 | + describe '#check' do |
| 9 | + it 'succeeds when response is 200 OK' do |
| 10 | + response = Net::HTTPOK.new('1.1', '200', 'OK') |
| 11 | + allow(check).to receive(:perform_request).and_return(response) |
| 12 | + |
| 13 | + check.check |
| 14 | + |
| 15 | + expect(check.message).to include('Http head check successful.') |
| 16 | + end |
| 17 | + |
| 18 | + it 'succeeds when response is a redirect' do |
| 19 | + response = Net::HTTPFound.new('1.1', '302', 'Found') |
| 20 | + allow(check).to receive(:perform_request).and_return(response) |
| 21 | + |
| 22 | + check.check |
| 23 | + |
| 24 | + expect(check.message).to include('Http head check successful.') |
| 25 | + end |
| 26 | + |
| 27 | + it 'fails when response is 404' do |
| 28 | + response = Net::HTTPNotFound.new('1.1', '404', 'Not Found') |
| 29 | + allow(check).to receive(:perform_request).and_return(response) |
| 30 | + |
| 31 | + check.check |
| 32 | + |
| 33 | + expect(check.message).to include('http head check responded, but returned unexpeced HTTP status: 404 Net::HTTPNotFound') |
| 34 | + end |
| 35 | + |
| 36 | + it 'fails when request raises an error' do |
| 37 | + error = StandardError.new('Connection error') |
| 38 | + allow(check).to receive(:perform_request).and_raise(error) |
| 39 | + |
| 40 | + check.check |
| 41 | + |
| 42 | + expect(check.message).to include('Error:') |
| 43 | + expect(check.message).to include('Connection error') |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe '#perform_request' do |
| 48 | + it 'calls head_request' do |
| 49 | + response = Net::HTTPOK.new('1.1', '200', 'OK') |
| 50 | + allow(check).to receive(:head_request).and_return(response) |
| 51 | + |
| 52 | + result = check.perform_request |
| 53 | + |
| 54 | + expect(result).to eq response |
| 55 | + end |
| 56 | + |
| 57 | + it 'raises ConnectionFailed on OpenTimeout' do |
| 58 | + allow(check).to receive(:head_request).and_raise(Net::OpenTimeout.new) |
| 59 | + |
| 60 | + expect do |
| 61 | + check.perform_request |
| 62 | + end.to raise_error(OkComputer::HttpCheck::ConnectionFailed) |
| 63 | + end |
| 64 | + |
| 65 | + it 'raises ConnectionFailed on ReadTimeout' do |
| 66 | + allow(check).to receive(:head_request).and_raise(Net::ReadTimeout.new) |
| 67 | + |
| 68 | + expect do |
| 69 | + check.perform_request |
| 70 | + end.to raise_error(OkComputer::HttpCheck::ConnectionFailed) |
| 71 | + end |
| 72 | + |
| 73 | + it 'raises ConnectionFailed on StandardError' do |
| 74 | + allow(check).to receive(:head_request).and_raise(StandardError.new) |
| 75 | + |
| 76 | + expect do |
| 77 | + check.perform_request |
| 78 | + end.to raise_error(OkComputer::HttpCheck::ConnectionFailed) |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments