-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Mailgunner | ||
class Response | ||
def initialize(http_response, options = {}) | ||
@http_response = http_response | ||
|
||
@json = options.fetch(:json) { JSON } | ||
end | ||
|
||
def method_missing(name, *args, &block) | ||
@http_response.send(name, *args, &block) | ||
end | ||
|
||
def respond_to_missing?(name, include_private = false) | ||
@http_response.respond_to?(name) | ||
end | ||
|
||
def ok? | ||
code.to_i == 200 | ||
end | ||
|
||
def client_error? | ||
(400 .. 499).include?(code.to_i) | ||
end | ||
|
||
def server_error? | ||
(500 .. 599).include?(code.to_i) | ||
end | ||
|
||
def json? | ||
self['Content-Type'].split(';').first == 'application/json' | ||
end | ||
|
||
def object | ||
@object ||= @json.parse(body) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require 'minitest/autorun' | ||
require 'mocha/setup' | ||
require 'mailgunner' | ||
|
||
describe 'Mailgunner::Response' do | ||
before do | ||
@http_response = mock() | ||
|
||
@response = Mailgunner::Response.new(@http_response) | ||
end | ||
|
||
it 'delegates missing methods to the http response object' do | ||
@http_response.stubs(:code).returns('200') | ||
|
||
@response.code.must_equal('200') | ||
end | ||
|
||
describe 'ok query method' do | ||
it 'returns true if the status code is 200' do | ||
@http_response.expects(:code).returns('200') | ||
|
||
@response.ok?.must_equal(true) | ||
end | ||
|
||
it 'returns false otherwise' do | ||
@http_response.expects(:code).returns('400') | ||
|
||
@response.ok?.must_equal(false) | ||
end | ||
end | ||
|
||
describe 'client_error query method' do | ||
it 'returns true if the status code is 4xx' do | ||
@http_response.stubs(:code).returns(%w(400 401 402 404).sample) | ||
|
||
@response.client_error?.must_equal(true) | ||
end | ||
|
||
it 'returns false otherwise' do | ||
@http_response.stubs(:code).returns('200') | ||
|
||
@response.client_error?.must_equal(false) | ||
end | ||
end | ||
|
||
describe 'server_error query method' do | ||
it 'returns true if the status code is 5xx' do | ||
@http_response.stubs(:code).returns(%w(500 502 503 504).sample) | ||
|
||
@response.server_error?.must_equal(true) | ||
end | ||
|
||
it 'returns false otherwise' do | ||
@http_response.stubs(:code).returns('200') | ||
|
||
@response.server_error?.must_equal(false) | ||
end | ||
end | ||
|
||
describe 'json query method' do | ||
it 'returns true if the response has a json content type' do | ||
@http_response.expects(:[]).with('Content-Type').returns('application/json;charset=utf-8') | ||
|
||
@response.json?.must_equal(true) | ||
end | ||
|
||
it 'returns false otherwise' do | ||
@http_response.expects(:[]).with('Content-Type').returns('text/html') | ||
|
||
@response.json?.must_equal(false) | ||
end | ||
end | ||
|
||
describe 'object method' do | ||
it 'decodes the response body as json and returns a hash' do | ||
@http_response.expects(:body).returns('{"foo":"bar"}') | ||
|
||
@response.object.must_equal({'foo' => 'bar'}) | ||
end | ||
end | ||
end | ||
|
||
describe 'Mailgunner::Response initialized with an alternative json implementation' do | ||
before do | ||
@json = mock() | ||
|
||
@http_response = stub | ||
|
||
@response = Mailgunner::Response.new(@http_response, :json => @json) | ||
end | ||
|
||
describe 'object method' do | ||
it 'uses the alternative json implementation to parse the response body' do | ||
@http_response.stubs(:body).returns(response_body = '{"foo":"bar"}') | ||
|
||
@json.expects(:parse).with(response_body) | ||
|
||
@response.object | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters