Skip to content

Commit c0b8678

Browse files
committed
Add rspec tests for endpoint http head checks
1 parent bb8d737 commit c0b8678

3 files changed

Lines changed: 136 additions & 2 deletions

File tree

lib/endpoint_url.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ class EndpointUrl
22
class << self
33
def geoserver(server_name)
44
url = Rails.configuration.x.servers[server_name]
5-
url && "#{url.chomp('/')}/wms?service=WMS&request=GetCapabilities"
5+
url && "#{url.sub(%r{/+\z}, '')}/wms?service=WMS&request=GetCapabilities"
66
end
77

88
def spatial_server(server_name)
99
url = Rails.configuration.x.servers[server_name]
10-
url && "#{url.chomp('/')}/public/berkeley-status/data.zip"
10+
url && "#{url.sub(%r{/+\z}, '')}/public/berkeley-status/data.zip"
1111
end
1212
end
1313
end

spec/lib/endpoint_url_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'rails_helper'
2+
3+
describe EndpointUrl do
4+
describe '.geoserver' do
5+
context 'when server is configured' do
6+
before do
7+
allow(Rails.configuration.x.servers).to receive(:[]).with('test_server').and_return('https://example.com//')
8+
end
9+
10+
it 'returns WMS endpoint URL' do
11+
result = EndpointUrl.geoserver('test_server')
12+
expect(result).to eq('https://example.com/wms?service=WMS&request=GetCapabilities')
13+
end
14+
15+
end
16+
17+
context 'when server is not configured' do
18+
before do
19+
allow(Rails.configuration.x.servers).to receive(:[]).with('missing_server').and_return(nil)
20+
end
21+
22+
it 'returns nil' do
23+
result = EndpointUrl.geoserver('missing_server')
24+
expect(result).to be_nil
25+
end
26+
end
27+
end
28+
29+
describe '.spatial_server' do
30+
context 'when server is configured' do
31+
before do
32+
allow(Rails.configuration.x.servers).to receive(:[]).with('test_server').and_return('https://example.com//')
33+
end
34+
35+
it 'returns spatial server data zip endpoint URL' do
36+
result = EndpointUrl.spatial_server('test_server')
37+
expect(result).to eq('https://example.com/public/berkeley-status/data.zip')
38+
end
39+
40+
end
41+
42+
context 'when server is not configured' do
43+
before do
44+
allow(Rails.configuration.x.servers).to receive(:[]).with('missing_server').and_return(nil)
45+
end
46+
47+
it 'returns nil' do
48+
result = EndpointUrl.spatial_server('missing_server')
49+
expect(result).to be_nil
50+
end
51+
end
52+
end
53+
end

spec/lib/http_head_check_spec.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)