Skip to content
Browse files

Strengthen specs with specs for a succeeded HTTP request, but failed …

…at the gateway.
  • Loading branch information...
1 parent dbe25b1 commit e5dd443c0aba8f28524e188306baf99e6fb0eaff @alloy alloy committed Aug 2, 2010
Showing with 101 additions and 64 deletions.
  1. +10 −13 lib/mollie/sms.rb
  2. +40 −51 spec/sms_spec.rb
  3. +51 −0 spec/spec_helper.rb
View
23 lib/mollie/sms.rb
@@ -29,7 +29,7 @@ def password=(password)
@password = Digest::MD5.hexdigest(password)
end
- def request_params
+ def default_params
{
'username' => @username,
'md5_password' => @password,
@@ -48,7 +48,7 @@ def request_params
attr_reader :params
def initialize(telephone_number = nil, body = nil, extra_params = {})
- @params = self.class.request_params.merge(extra_params)
+ @params = self.class.default_params.merge(extra_params)
self.telephone_number = telephone_number if telephone_number
self.body = body if body
end
@@ -96,30 +96,27 @@ def initialize(http_response)
end
def params
- @params ||= http_request_succeeded? ?
- Hash.from_xml(@http_response.read_body)['response']['item'] : {}
+ @params ||= http_failure? ? {} : Hash.from_xml(@http_response.read_body)['response']['item']
end
def result_code
- params['resultcode'].to_i if http_request_succeeded?
+ (http_failure? ? @http_response.code : params['resultcode']).to_i
end
def message
- params['resultmessage']
+ http_failure? ? "[HTTP: #{@http_response.code}] #{@http_response.message}" : params['resultmessage']
end
def success?
- http_request_succeeded? && params['success'] == 'true'
+ !http_failure? && params['success'] == 'true'
end
- def inspect
- "#<#{self.class.name} #{ success? ? 'succeeded' : 'failed' } (#{result_code}) `#{message}'>"
+ def http_failure?
+ !@http_response.is_a?(Net::HTTPSuccess)
end
- private
-
- def http_request_succeeded?
- @http_response.is_a?(Net::HTTPSuccess)
+ def inspect
+ "#<#{self.class.name} #{ success? ? 'succeeded' : 'failed' } (#{result_code}) `#{message}'>"
end
end
end
View
91 spec/sms_spec.rb
@@ -3,7 +3,7 @@
Mollie::SMS.username = 'AstroRadio'
Mollie::SMS.password = 'secret'
-Mollie::SMS.originator = 'Fingertips'
+Mollie::SMS.originator = 'Astro INC'
describe "Mollie::SMS" do
it "holds the gateway uri" do
@@ -19,7 +19,7 @@
end
it "holds the originator" do
- Mollie::SMS.originator.should == 'Fingertips'
+ Mollie::SMS.originator.should == 'Astro INC'
end
it "returns the default charset" do
@@ -41,11 +41,11 @@
Mollie::SMS.gateway.should == Mollie::SMS::GATEWAYS['basic']
end
- it "returns a hash of params for a request" do
- Mollie::SMS.request_params.should == {
+ it "returns a hash of default params for a request" do
+ Mollie::SMS.default_params.should == {
'username' => 'AstroRadio',
'md5_password' => Digest::MD5.hexdigest('secret'),
- 'originator' => 'Fingertips',
+ 'originator' => 'Astro INC',
'gateway' => '2',
'charset' => 'UTF-8',
'type' => 'normal'
@@ -83,54 +83,14 @@
end
it "returns the request params with all string keys and values" do
- params = Mollie::SMS.request_params.merge(
+ params = Mollie::SMS.default_params.merge(
'recipients' => '+31612345678',
'message' => "The stars tell me you will have chicken noodle soup for breakfast."
)
@sms.params.should == params
end
end
-module Net
- class HTTP
- class << self
- attr_accessor :posted, :stubbed_response
-
- def reset!
- @posted = nil
- @stubbed_response = nil
- end
- end
-
- def host
- @address
- end
-
- def start
- yield self
- end
-
- def request(request)
- self.class.posted = [self, request]
- self.class.stubbed_response
- end
- end
-end
-
-class ResponseStub
-end
-
-SUCCESS_BODY = %{
-<?xml version="1.0" ?>
-<response>
- <item type="sms">
- <recipients>1</recipients>
- <success>true</success>
- <resultcode>10</resultcode>
- <resultmessage>Message successfully sent.</resultmessage>
- </item>
-</response>}
-
describe "When sending a Mollie::SMS message" do
before do
@sms = Mollie::SMS.new
@@ -195,7 +155,32 @@ class ResponseStub
end
end
-describe "A Mollie::SMS::Response instance, for a failed request" do
+describe "A Mollie::SMS::Response instance, for a request that failed at the gateway" do
+ before do
+ @http_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ @http_response.stubs(:read_body).returns(FAILURE_BODY)
+ @http_response.add_field('Content-type', 'application/xml')
+ @response = Mollie::SMS::Response.new(@http_response)
+ end
+
+ it "returns that the request was not a success" do
+ @response.should.not.be.success
+ end
+
+ it "returns that this is a *not* HTTP failure" do
+ @response.should.not.be.http_failure
+ end
+
+ it "returns the result_code" do
+ @response.result_code.should == 20
+ end
+
+ it "returns the message corresponding to the result code" do
+ @response.message.should == "No username given."
+ end
+end
+
+describe "A Mollie::SMS::Response instance, for a failed HTTP request" do
before do
@http_response = Net::HTTPBadRequest.new('1.1', '400', 'Bad request')
@response = Mollie::SMS::Response.new(@http_response)
@@ -209,11 +194,15 @@ class ResponseStub
@response.should.not.be.success
end
- it "returns nil as the result_code" do
- @response.result_code.should == nil
+ it "returns that this is a HTTP failure" do
+ @response.should.be.http_failure
+ end
+
+ it "returns the HTTP response code as the result_code" do
+ @response.result_code.should == 400
end
- it "returns nil as the message" do
- @response.message.should == nil
+ it "returns the HTTP error message as the message" do
+ @response.message.should == "[HTTP: 400] Bad request"
end
end
View
51 spec/spec_helper.rb
@@ -5,3 +5,54 @@
$:.unshift File.expand_path("../../lib", __FILE__)
Bacon.summary_on_exit
+
+module Net
+ class HTTP
+ class << self
+ attr_accessor :posted, :stubbed_response
+
+ def reset!
+ @posted = nil
+ @stubbed_response = nil
+ end
+ end
+
+ def host
+ @address
+ end
+
+ def start
+ yield self
+ end
+
+ def request(request)
+ self.class.posted = [self, request]
+ self.class.stubbed_response
+ end
+ end
+end
+
+class ResponseStub
+end
+
+SUCCESS_BODY = %{
+<?xml version="1.0" ?>
+<response>
+ <item type="sms">
+ <recipients>1</recipients>
+ <success>true</success>
+ <resultcode>10</resultcode>
+ <resultmessage>Message successfully sent.</resultmessage>
+ </item>
+</response>}
+
+FAILURE_BODY = %{
+<?xml version="1.0"?>
+<response>
+ <item type="sms">
+ <recipients>1</recipients>
+ <success>false</success>
+ <resultcode>20</resultcode>
+ <resultmessage>No username given.</resultmessage>
+ </item>
+</response>}

0 comments on commit e5dd443

Please sign in to comment.
Something went wrong with that request. Please try again.