From 62568209a4c2ea4257fe7e86b30b1e35ec2f4652 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Thu, 29 Mar 2012 17:40:14 -0400 Subject: [PATCH] Require a certificate to use Connection --- lib/grocer/connection.rb | 16 +++++++++++----- lib/grocer/no_certificate_error.rb | 4 ++++ spec/grocer/connection_spec.rb | 5 +++++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 lib/grocer/no_certificate_error.rb diff --git a/lib/grocer/connection.rb b/lib/grocer/connection.rb index 213288b..986d518 100644 --- a/lib/grocer/connection.rb +++ b/lib/grocer/connection.rb @@ -1,4 +1,5 @@ require 'forwardable' +require 'grocer/no_certificate_error' require 'grocer/ssl_connection' module Grocer @@ -27,11 +28,16 @@ def write(content) private def ssl - @ssl_connection ||= - Grocer::SSLConnection.new(certificate: certificate, - passphrase: passphrase, - gateway: gateway, - port: port) + @ssl_connection ||= build_connection + end + + def build_connection + fail Grocer::NoCertificateErrror unless certificate + + Grocer::SSLConnection.new(certificate: certificate, + passphrase: passphrase, + gateway: gateway, + port: port) end def with_open_connection(&block) diff --git a/lib/grocer/no_certificate_error.rb b/lib/grocer/no_certificate_error.rb new file mode 100644 index 0000000..dda8dd9 --- /dev/null +++ b/lib/grocer/no_certificate_error.rb @@ -0,0 +1,4 @@ +module Grocer + class NoCertificateErrror < StandardError + end +end diff --git a/spec/grocer/connection_spec.rb b/spec/grocer/connection_spec.rb index eedf24e..9e60804 100644 --- a/spec/grocer/connection_spec.rb +++ b/spec/grocer/connection_spec.rb @@ -40,6 +40,11 @@ subject.port.should == connection_options[:port] end + it 'requires a certificate' do + connection_options[:certificate] = nil + -> { subject.read }.should raise_error(Grocer::NoCertificateErrror) + end + context 'an open SSLConnection' do before do ssl.stubs(:connected?).returns(true)