1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
require 'spec_helper'
describe 'ConnectionString' do
include Mongo::ConnectionString
CONNECTION_STRING_TESTS.each do |file|
spec = Mongo::ConnectionString::Spec.new(file)
context(spec.description) do
before(:all) do
module Mongo
class Address
private
alias :original_initialize_resolver! :initialize_resolver!
def initialize_resolver!(timeout, ssl_options)
family = (host == 'localhost') ? ::Socket::AF_INET : ::Socket::AF_UNSPEC
info = ::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM)
FAMILY_MAP[info.first[4]].new(info[3], port, host)
end
end
class Server
# The constructor keeps the same API, but does not instantiate a
# monitor and run it.
alias :original_initialize :initialize
def initialize(address, cluster, monitoring, event_listeners, options = {})
@address = address
@cluster = cluster
@monitoring = monitoring
@options = options.freeze
@monitor = Monitor.new(address, event_listeners, options)
end
# Disconnect simply needs to return true since we have no monitor and
# no connection.
alias :original_disconnect! :disconnect!
def disconnect!; true; end
end
end
end
after(:all) do
module Mongo
# Return the implementations to their originals for the other
# tests in the suite.
class Address
alias :initialize_resolver! :original_initialize_resolver!
remove_method(:original_initialize_resolver!)
end
class Server
alias :initialize :original_initialize
remove_method(:original_initialize)
alias :disconnect! :original_disconnect!
remove_method(:original_disconnect!)
end
end
end
spec.tests.each_with_index do |test, index|
context "when a #{test.description} is provided" do
context 'when the uri is invalid', unless: test.valid? do
it 'raises an error' do
expect{
test.uri
}.to raise_exception(Mongo::Error::InvalidURI)
end
end
context 'when the uri should warn', if: test.warn? do
before do
expect(Mongo::Logger.logger).to receive(:warn)
end
it 'warns' do
expect(test.client).to be_a(Mongo::Client)
end
end
context 'when the uri is valid', if: test.valid? do
it 'does not raise an exception' do
expect(test.uri).to be_a(Mongo::URI)
end
it 'creates a client with the correct hosts' do
expect(test.client).to have_hosts(test)
end
it 'creates a client with the correct authentication properties' do
expect(test.client).to match_auth(test)
end
it 'creates a client with the correct options' do
expect(test.client).to match_options(test)
end
end
end
end
end
end
end
|