-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include Rack::Response::Helpers in responses
- Loading branch information
Showing
8 changed files
with
131 additions
and
54 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
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,19 @@ | ||
require 'rack/response' | ||
|
||
module Airborne | ||
class Response < Struct.new(:raw) | ||
include Rack::Response::Helpers | ||
|
||
alias_method :success?, :successful? | ||
alias_method :missing?, :not_found? | ||
alias_method :error?, :server_error? | ||
|
||
def status | ||
raw.code | ||
end | ||
|
||
def method_missing(name, *args, &block) | ||
raw.send(name, *args, &block) | ||
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
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 |
---|---|---|
@@ -1,36 +1,30 @@ | ||
require 'spec_helper' | ||
|
||
describe 'client requester' do | ||
before do | ||
allow(RestClient).to receive(:send) | ||
RSpec::Mocks.space.proxy_for(self).remove_stub_if_present(:get) | ||
end | ||
|
||
after do | ||
allow(RestClient).to receive(:send).and_call_original | ||
Airborne.configure { |config| config.headers = {} } | ||
end | ||
|
||
it 'should set :content_type to :json by default' do | ||
get '/foo' | ||
stub_request(:get, 'http://www.example.com/foo') | ||
.with(headers: { 'Content-Type': 'application/json' }) | ||
|
||
expect(RestClient).to have_received(:send) | ||
.with(:get, 'http://www.example.com/foo', { content_type: :json }) | ||
get '/foo' | ||
end | ||
|
||
it 'should override headers with option[:headers]' do | ||
get '/foo', { content_type: 'application/x-www-form-urlencoded' } | ||
stub_request(:get, 'http://www.example.com/foo') | ||
.with(headers: { 'Content-Type': 'application/x-www-form-urlencoded' }) | ||
|
||
expect(RestClient).to have_received(:send) | ||
.with(:get, 'http://www.example.com/foo', { content_type: 'application/x-www-form-urlencoded' }) | ||
get '/foo', { content_type: 'application/x-www-form-urlencoded' } | ||
end | ||
|
||
it 'should override headers with airborne config headers' do | ||
Airborne.configure { |config| config.headers = { content_type: 'text/plain' } } | ||
|
||
get '/foo' | ||
stub_request(:get, 'http://www.example.com/foo') | ||
.with(headers: { 'Content-Type': 'text/plain' }) | ||
|
||
expect(RestClient).to have_received(:send) | ||
.with(:get, 'http://www.example.com/foo', { content_type: 'text/plain' }) | ||
get '/foo' | ||
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
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,53 @@ | ||
require 'spec_helper' | ||
|
||
describe 'response' do | ||
let(:headers) { {} } | ||
let(:status) { 200 } | ||
|
||
before { Airborne.configuration.rest_client_options = { max_redirects: 0 } } | ||
after { Airborne.configuration.rest_client_options = {} } | ||
|
||
before do | ||
mock_get('simple_get', headers, status) | ||
get '/simple_get' | ||
end | ||
|
||
context 'with 200 status' do | ||
let(:status) { 200 } | ||
|
||
it { expect(response.status).to eq 200 } | ||
it { expect(response).to be_successful } | ||
it { expect(response).to be_success } | ||
it { expect(response).to be_ok } | ||
end | ||
|
||
context 'with 404 status' do | ||
let(:status) { 404 } | ||
|
||
it { expect(response.status).to eq 404 } | ||
it { expect(response).to_not be_success } | ||
it { expect(response).to_not be_ok } | ||
it { expect(response).to be_client_error } | ||
it { expect(response).to be_not_found } | ||
it { expect(response).to be_missing } | ||
end | ||
|
||
context 'with 302 status' do | ||
let(:status) { 302 } | ||
let(:headers) { { 'Location' => '/' } } | ||
|
||
it { expect(response.status).to eq 302 } | ||
it { expect(response).to be_redirection } | ||
it { expect(response).to be_redirect } | ||
end | ||
|
||
context 'with 500 status' do | ||
let(:status) { 500 } | ||
|
||
it { expect(response.status).to eq 500 } | ||
it { expect(response).to_not be_success } | ||
it { expect(response).to_not be_ok } | ||
it { expect(response).to be_server_error } | ||
it { expect(response).to be_error } | ||
end | ||
end |