# @(#) MQMBID sn=mqkoa-L151208.15 su=_G3xYgJ26EeWqUvq4M3I6bA pn=appmsging/ruby/mqlight/spec/mqlight/util_spec.rb # # # Licensed Materials - Property of IBM # # 5725-P60 # # (C) Copyright IBM Corp. 2014 # # US Government Users Restricted Rights - Use, duplication or # disclosure restricted by GSA ADP Schedule Contract with # IBM Corp. # require 'spec_helper' describe Mqlight::Util do describe '.get_service_urls' do it 'fails if passed a non uri string' do expect do Mqlight::Util.get_service_urls('not a uri') end.to raise_error(URI::InvalidURIError) end it 'fails if passed a unsupported uri string' do expect do Mqlight::Util.get_service_urls('ftp://example.com') end.to raise_error(ArgumentError) end it 'makes an http request if passed an http uri string' do stub = stub_request(:get, 'http://example.com/blah') .to_return(body: "{\"service\":[\"amqp:\/\/example.com:5672\","\ "\"amqp:\/\/example.com:5673\"]}", status: 200) Mqlight::Util.get_service_urls('http://example.com/blah') expect(stub).to have_been_requested end it 'makes an https request if passed an https uri string' do stub = stub_request(:get, 'https://example.com/blah') .to_return(body: "{\"service\":[\"amqp:\/\/example.com:5672\","\ "\"amqp:\/\/example.com:5673\"]}", status: 200) Mqlight::Util.get_service_urls('https://example.com/blah') expect(stub).to have_been_requested end it 'makes an http request include GET params if passed' do stub = stub_request(:get, 'http://example.com/blah?serviceId=foo') .to_return(body: "{\"service\":[\"amqp:\/\/example.com:5672\","\ "\"amqp:\/\/example.com:5673\"]}", status: 200) Mqlight::Util.get_service_urls('http://example.com/blah?serviceId=foo') expect(stub).to have_been_requested end it 'makes an https request include GET params if passed' do stub = stub_request(:get, 'https://example.com/blah?serviceId=foo') .to_return(body: "{\"service\":[\"amqp:\/\/example.com:5672\","\ "\"amqp:\/\/example.com:5673\"]}", status: 200) Mqlight::Util.get_service_urls('https://example.com/blah?serviceId=foo') expect(stub).to have_been_requested end it 'fails if a http request returns a non-200 response' do stub = stub_request(:get, 'http://example.com/blah') .to_return(body: "{\"service\":[\"amqp:\/\/example.com:5672\","\ "\"amqp:\/\/example.com:5673\"]}", status: 400) expect do Mqlight::Util.get_service_urls('http://example.com/blah') end.to raise_error(Mqlight::NetworkError) expect(stub).to have_been_requested end it 'fails if a http request returns a non-json response' do stub = stub_request(:get, 'http://example.com/blah') .to_return(body: 'not json', status: 200) expect do Mqlight::Util.get_service_urls('http://example.com/blah') end.to raise_error(JSON::ParserError) expect(stub).to have_been_requested end end end