-
Notifications
You must be signed in to change notification settings - Fork 124
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
15 changed files
with
340 additions
and
105 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 |
---|---|---|
@@ -1,27 +1,29 @@ | ||
require 'airborne/rspec_settings' | ||
require 'airborne/optional_hash_type_expectations' | ||
require 'airborne/path_matcher' | ||
require 'airborne/request_expectations' | ||
require 'airborne/rest_client_requester' | ||
require 'airborne/rack_test_requester' | ||
require 'airborne/faraday_requester' | ||
require 'airborne/base' | ||
|
||
RSpec.configure do |config| | ||
config.add_setting :base_url | ||
config.add_setting :match_expected | ||
config.add_setting :match_actual | ||
config.add_setting :match_expected_default, default: true | ||
config.add_setting :match_actual_default, default: false | ||
config.add_setting :headers | ||
config.add_setting :rack_app | ||
config.add_setting :requester_type | ||
config.add_setting :requester_module | ||
config.before do |example| | ||
config.match_expected = example.metadata[:match_expected].nil? ? | ||
Airborne.configuration.match_expected_default? : example.metadata[:match_expected] | ||
config.match_actual = example.metadata[:match_actual].nil? ? | ||
Airborne.configuration.match_actual_default? : example.metadata[:match_actual] | ||
|
||
module Airborne | ||
class << self | ||
def configuration | ||
RSpec.configuration | ||
end | ||
|
||
def configure | ||
RSpec.configure do |config| | ||
yield config | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Include last since it depends on the configuration already being added | ||
config.include Airborne | ||
Airborne.configure do |config| | ||
config.include Airborne::Base | ||
config.filter_run_when_matching :focus | ||
config.order = :random | ||
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 |
---|---|---|
@@ -1,82 +1,82 @@ | ||
require 'json' | ||
require 'active_support' | ||
require 'active_support/core_ext/hash/indifferent_access' | ||
require 'base64' | ||
|
||
module Airborne | ||
class InvalidJsonError < StandardError; end | ||
|
||
include RequestExpectations | ||
|
||
attr_reader :response, :headers, :body | ||
|
||
def self.configure | ||
RSpec.configure do |config| | ||
yield config | ||
module Base | ||
class InvalidJsonError < StandardError; end | ||
|
||
include RequestExpectations | ||
|
||
attr_reader :response, :headers, :body | ||
|
||
def self.included(base) | ||
if Airborne.configuration.use_faraday | ||
base.send(:include, Airborne::FaradayRequester) | ||
elsif Airborne.configuration.requester_module | ||
base.send(:include, Airborne.configuration.requester_module) | ||
elsif Airborne.configuration.rack_app | ||
base.send(:include, RackTestRequester) | ||
else | ||
base.send(:include, RestClientRequester) | ||
end | ||
end | ||
end | ||
|
||
def self.included(base) | ||
if !Airborne.configuration.requester_module.nil? | ||
base.send(:include, Airborne.configuration.requester_module) | ||
elsif !Airborne.configuration.rack_app.nil? | ||
base.send(:include, RackTestRequester) | ||
else | ||
base.send(:include, RestClientRequester) | ||
def get(path, body = nil, headers = nil, &block) | ||
@response = make_request(:get, path, body, headers, &block) | ||
end | ||
end | ||
|
||
def self.configuration | ||
RSpec.configuration | ||
end | ||
|
||
def get(url, headers = nil) | ||
@response = make_request(:get, url, headers: headers) | ||
end | ||
def post(path, post_body = nil, headers = nil, &block) | ||
@response = make_request(:post, path, post_body, headers, &block) | ||
end | ||
|
||
def post(url, post_body = nil, headers = nil) | ||
@response = make_request(:post, url, body: post_body, headers: headers) | ||
end | ||
def patch(path, patch_body = nil, headers = nil, &block) | ||
@response = make_request(:patch, path, patch_body, headers, &block) | ||
end | ||
|
||
def patch(url, patch_body = nil, headers = nil) | ||
@response = make_request(:patch, url, body: patch_body, headers: headers) | ||
end | ||
def put(path, put_body = nil, headers = nil, &block) | ||
@response = make_request(:put, path, put_body, headers, &block) | ||
end | ||
|
||
def put(url, put_body = nil, headers = nil) | ||
@response = make_request(:put, url, body: put_body, headers: headers) | ||
end | ||
def delete(path, delete_body = nil, headers = nil, &block) | ||
@response = make_request(:delete, path, delete_body, headers, &block) | ||
end | ||
|
||
def delete(url, delete_body = nil, headers = nil) | ||
@response = make_request(:delete, url, body: delete_body, headers: headers) | ||
end | ||
def head(path, body = nil, headers = nil, &block) | ||
@response = make_request(:head, path, body, headers, &block) | ||
end | ||
|
||
def head(url, headers = nil) | ||
@response = make_request(:head, url, headers: headers) | ||
end | ||
def options(path, body = nil, headers = nil, &block) | ||
@response = make_request(:options, path, body, headers, &block) | ||
end | ||
|
||
def options(url, headers = nil) | ||
@response = make_request(:options, url, headers: headers) | ||
end | ||
def response | ||
@response | ||
end | ||
|
||
def response | ||
@response | ||
end | ||
def headers | ||
HashWithIndifferentAccess.new(response.headers) | ||
end | ||
|
||
def headers | ||
HashWithIndifferentAccess.new(response.headers) | ||
end | ||
def basic_auth(login, pass) | ||
val = Base64.encode64([login, pass].join(':')).gsub!("\n", '') | ||
"Basic #{val}" | ||
end | ||
|
||
def body | ||
response.body | ||
end | ||
def body | ||
response.body | ||
end | ||
|
||
def json_body | ||
JSON.parse(response.body, symbolize_names: true) rescue fail InvalidJsonError, 'Api request returned invalid json' | ||
end | ||
def json_body | ||
JSON.parse(response.body, symbolize_names: true) rescue fail InvalidJsonError, 'Api request returned invalid json' | ||
end | ||
|
||
private | ||
private | ||
|
||
def get_url(url) | ||
base = Airborne.configuration.base_url || '' | ||
base + url | ||
def get_url(url) | ||
base = Airborne.configuration.base_url || '' | ||
base + url | ||
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,35 @@ | ||
require 'faraday' | ||
|
||
module Airborne | ||
module FaradayRequester | ||
def conn | ||
@conn ||= Faraday.new do |c| | ||
c.request :url_encoded | ||
c.adapter Faraday.default_adapter | ||
end | ||
end | ||
|
||
def make_request(method_name, path, body = {}, headers ={}, &block) | ||
conn.send(method_name, path, body) do |req| | ||
conn.url_prefix = base_url | ||
req.path = path | ||
req.headers.merge!(headers) | ||
yield req, conn if block_given? | ||
req.headers.merge!(conn.headers) | ||
end | ||
end | ||
|
||
private | ||
def base_url | ||
Airborne.configuration.base_url | ||
end | ||
end | ||
end | ||
|
||
module FaradayResponse | ||
def code | ||
status | ||
end | ||
end | ||
|
||
Faraday::Response.send(:include, FaradayResponse) |
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,22 @@ | ||
RSpec.configure do |config| | ||
config.add_setting :base_url | ||
config.add_setting :match_expected_default, default: true | ||
config.add_setting :match_actual_default, default: false | ||
config.add_setting :match_expected, default: config.match_expected_default | ||
config.add_setting :match_actual, default: config.match_actual_default | ||
config.add_setting :headers | ||
config.add_setting :rack_app | ||
config.add_setting :requester_type | ||
config.add_setting :requester_module | ||
config.add_setting :use_faraday, default: true | ||
config.around(:example, match_expected: !config.match_expected_default) do |example| | ||
config.match_expected = !config.match_expected_default | ||
example.run | ||
config.match_expected = config.match_expected_default | ||
end | ||
config.around(:example, match_actual: !config.match_actual_default) do |example| | ||
config.match_actual = !config.match_actual_default | ||
example.run | ||
config.match_actual = config.match_actual_default | ||
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
Oops, something went wrong.