airborne 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +32 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +5 -0
- data/README.md +67 -11
- data/airborne.gemspec +1 -1
- data/integration/rails/request_spec.rb +55 -0
- data/integration/rails/spec_helper.rb +35 -0
- data/lib/airborne/base.rb +7 -34
- data/lib/airborne/path_matcher.rb +5 -2
- data/lib/airborne/request_expectations.rb +19 -3
- data/lib/airborne/request_methods.rb +33 -0
- data/lib/airborne/rest_client_requester.rb +9 -3
- data/lib/airborne.rb +5 -0
- data/spec/airborne/expectations/empty_arrays_spec.rb +57 -0
- data/spec/airborne/expectations/string_keys_spec.rb +41 -0
- data/spec/airborne/request_encoding_spec.rb +73 -0
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 92b6138c2b98896b6957238ec85c22db1938be163f0ed298923c4d3841dcbfee
|
|
4
|
+
data.tar.gz: 938c41bf4456fa552a8cedb899f2f817d1b756b707a2ef4769a7a6d63e6c57f4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 135a1f6b7ba09b03e2419cd703b025371ab0d43b45d97268ac8cea31ff146f2298226f879e8155b5a0caac1d00c7b6de8862e5ee7df9082952f461371707efd6
|
|
7
|
+
data.tar.gz: d0cb342373291e9f00ee6ae98821535d9a983632a7dbe3f80403a24cf4d561117213dce5a166abac117eb328394128605af41e331236aeabf0a3785e87f2170d
|
data/.github/workflows/tests.yml
CHANGED
|
@@ -33,3 +33,35 @@ jobs:
|
|
|
33
33
|
ruby-version: ${{ matrix.ruby }}
|
|
34
34
|
bundler-cache: true
|
|
35
35
|
- run: bundle exec rake
|
|
36
|
+
|
|
37
|
+
rails:
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
strategy:
|
|
40
|
+
matrix:
|
|
41
|
+
load_order: [before, after]
|
|
42
|
+
framework:
|
|
43
|
+
- rails: '~> 7.1.0'
|
|
44
|
+
rspec: '~> 6.1'
|
|
45
|
+
rack: '~> 2.2'
|
|
46
|
+
sinatra: '~> 2.2'
|
|
47
|
+
- rails: '~> 7.2.0'
|
|
48
|
+
rspec: '~> 7.1'
|
|
49
|
+
rack: '~> 3.0'
|
|
50
|
+
sinatra: '~> 4.0'
|
|
51
|
+
- rails: '~> 8.1.0'
|
|
52
|
+
rspec: '~> 8.0'
|
|
53
|
+
rack: '~> 3.0'
|
|
54
|
+
sinatra: '~> 4.0'
|
|
55
|
+
env:
|
|
56
|
+
RAILS_VERSION: ${{ matrix.framework.rails }}
|
|
57
|
+
RSPEC_RAILS_VERSION: ${{ matrix.framework.rspec }}
|
|
58
|
+
RACK_VERSION: ${{ matrix.framework.rack }}
|
|
59
|
+
SINATRA_VERSION: ${{ matrix.framework.sinatra }}
|
|
60
|
+
AIRBORNE_LOAD_ORDER: ${{ matrix.load_order }}
|
|
61
|
+
steps:
|
|
62
|
+
- uses: actions/checkout@v4
|
|
63
|
+
- uses: ruby/setup-ruby@v1
|
|
64
|
+
with:
|
|
65
|
+
ruby-version: '3.3'
|
|
66
|
+
bundler-cache: true
|
|
67
|
+
- run: bundle exec rspec integration/rails --format progress
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## [v0.4.2](https://github.com/brooklynDev/airborne/tree/v0.4.2) (2026-11-24)
|
|
4
|
+
|
|
5
|
+
- Normalize equivalent request header names before merging configuration and request overrides. HTTP-style `Content-Type` headers now select the correct JSON, form, or multipart encoding (#64, #73).
|
|
6
|
+
- Accept string and symbol keys consistently in JSON value, type, key, and size assertions, including nested hashes and matching modes (#86).
|
|
7
|
+
- Reject empty arrays for `?` wildcard assertions. Add the `allow_empty_arrays: false` example metadata and `allow_empty_arrays_default = false` configuration to require nonempty `*` wildcard results (#100). Existing `*` behavior is preserved by default.
|
|
8
|
+
|
|
9
|
+
## [v0.4.1](https://github.com/brooklynDev/airborne/tree/v0.4.1) (2026-11-24)
|
|
10
|
+
|
|
11
|
+
- Preserve Rails request helpers and response accessors when Airborne is loaded after `rspec-rails`.
|
|
12
|
+
- Add Rails request and controller integration tests in both load orders, including the JSON assertion reported in #167 and consecutive requests.
|
|
13
|
+
- Document Rails request specs and the upgrade needed for the older 0.0.18 JSON-response bug.
|
|
14
|
+
|
|
3
15
|
## [v0.4.0](https://github.com/brooklynDev/airborne/tree/v0.4.0) (2026-11-24)
|
|
4
16
|
|
|
5
17
|
**Rack 3 support:**
|
data/Gemfile
CHANGED
|
@@ -7,6 +7,11 @@ gem 'rack', ENV['RACK_VERSION'] if ENV['RACK_VERSION']
|
|
|
7
7
|
gem 'coveralls', require: false
|
|
8
8
|
gem 'faraday-retry', require: false
|
|
9
9
|
|
|
10
|
+
if ENV['RAILS_VERSION']
|
|
11
|
+
gem 'railties', ENV['RAILS_VERSION']
|
|
12
|
+
gem 'rspec-rails', ENV.fetch('RSPEC_RAILS_VERSION', '>= 0')
|
|
13
|
+
end
|
|
14
|
+
|
|
10
15
|
group :test do
|
|
11
16
|
gem 'webmock'
|
|
12
17
|
gem 'sinatra', ENV.fetch('SINATRA_VERSION', '>= 0')
|
data/README.md
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
# Airborne
|
|
2
2
|
|
|
3
|
-
[](http://rubygems.org/gems/airborne)
|
|
3
|
+
[](https://github.com/brooklynDev/airborne/actions/workflows/tests.yml)
|
|
4
|
+
[](https://coveralls.io/github/brooklynDev/airborne?branch=master)
|
|
5
|
+
[](https://rubygems.org/gems/airborne)
|
|
6
|
+
[](https://rubygems.org/gems/airborne)
|
|
7
|
+
[](https://rubygems.org/gems/airborne)
|
|
9
8
|
|
|
10
9
|
RSpec driven API testing framework
|
|
11
10
|
|
|
12
|
-
## Looking for Project Maintainers
|
|
13
|
-
I am looking for project maintainers to help keep airborne up to date and bug-free while avoiding feature creep and maintaining backwards compatibility.
|
|
14
|
-
|
|
15
|
-
Comment [here](https://github.com/brooklynDev/airborne/issues/134) if you would like to help out.
|
|
16
|
-
|
|
17
11
|
## Installation
|
|
18
12
|
|
|
19
13
|
Install Airborne:
|
|
@@ -150,6 +144,16 @@ You may set a different content type and post a string body this way:
|
|
|
150
144
|
post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }
|
|
151
145
|
```
|
|
152
146
|
|
|
147
|
+
With the HTTP client, header names such as `'Content-Type'`, `'content-type'`, and
|
|
148
|
+
`:content_type` are equivalent. Request headers override equivalent global headers,
|
|
149
|
+
and the effective content type determines whether the body is JSON-encoded.
|
|
150
|
+
For form submissions, pass a hash with the form content type:
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
post '/login', { email: 'user', password: 'pass' },
|
|
154
|
+
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
155
|
+
```
|
|
156
|
+
|
|
153
157
|
For requests that require Query params you can pass a params hash into headers.
|
|
154
158
|
|
|
155
159
|
```ruby
|
|
@@ -246,6 +250,28 @@ RACK_VERSION='~> 3.0' SINATRA_VERSION='~> 4.0' bundle exec rake
|
|
|
246
250
|
|
|
247
251
|
If you're testing an API you've written in Rails, Airborne plays along with `rspec-rails`:
|
|
248
252
|
|
|
253
|
+
Require `airborne` in `rails_helper.rb` alongside `rspec/rails`. Rails request and
|
|
254
|
+
controller specs use Rails' own request helpers and response object; Airborne adds
|
|
255
|
+
JSON, status, and header assertions without requiring a separate server or `base_url`.
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
require 'rails_helper'
|
|
259
|
+
|
|
260
|
+
RSpec.describe 'Users API', type: :request do
|
|
261
|
+
it 'returns an error for a missing user' do
|
|
262
|
+
get '/api/users/1'
|
|
263
|
+
expect_status(:not_found)
|
|
264
|
+
expect_json('error', message: 'not found')
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
If an older installation reports `undefined method '[]' for nil:NilClass`, check
|
|
270
|
+
the Airborne version in `Gemfile.lock` and run `bundle update airborne`. Early
|
|
271
|
+
versions such as 0.0.18 only populated `json_body` after Airborne's own requests;
|
|
272
|
+
current versions parse the Rails response directly, including after consecutive requests.
|
|
273
|
+
|
|
274
|
+
Controller specs are also supported:
|
|
249
275
|
|
|
250
276
|
```ruby
|
|
251
277
|
require 'rails_helper'
|
|
@@ -262,6 +288,14 @@ end
|
|
|
262
288
|
|
|
263
289
|
## API
|
|
264
290
|
|
|
291
|
+
JSON assertions accept string and symbol keys, including nested expectations:
|
|
292
|
+
|
|
293
|
+
```ruby
|
|
294
|
+
expect_json('@id' => '1234', 'nested' => { 'dc:title' => 'Unicorns' })
|
|
295
|
+
expect_json_types('@id' => :string)
|
|
296
|
+
expect_json_keys(['@id', :nested])
|
|
297
|
+
```
|
|
298
|
+
|
|
265
299
|
* `expect_json_types` - Tests the types of the JSON property values returned
|
|
266
300
|
* `expect_json` - Tests the values of the JSON property values returned
|
|
267
301
|
* `expect_json_keys` - Tests the existence of the specified keys in the JSON object
|
|
@@ -359,6 +393,28 @@ it 'should test all elements of the array' do
|
|
|
359
393
|
end
|
|
360
394
|
```
|
|
361
395
|
|
|
396
|
+
`?` requires at least one matching element and fails on an empty array. By default,
|
|
397
|
+
`*` checks every existing element and allows an empty array. To require nonempty
|
|
398
|
+
arrays at every wildcard in a spec, use `allow_empty_arrays: false`:
|
|
399
|
+
|
|
400
|
+
```ruby
|
|
401
|
+
it 'returns users', allow_empty_arrays: false do
|
|
402
|
+
get '/users'
|
|
403
|
+
expect_json_types('*', name: :string) # fails if the response is []
|
|
404
|
+
end
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
To make that the default for all specs:
|
|
408
|
+
|
|
409
|
+
```ruby
|
|
410
|
+
Airborne.configure do |config|
|
|
411
|
+
config.allow_empty_arrays_default = false
|
|
412
|
+
end
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
Individual specs can override the default with `allow_empty_arrays: true`.
|
|
416
|
+
Explicit assertions such as `expect_json_sizes(items: 0)` still accept empty arrays.
|
|
417
|
+
|
|
362
418
|
`*` and `?` work for nested arrays as well. Given the following JSON:
|
|
363
419
|
|
|
364
420
|
```json
|
data/airborne.gemspec
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe 'Airborne with Rails request specs', type: :request do
|
|
4
|
+
it 'uses Rails requests and assertions against the Rails response (issue #167)' do
|
|
5
|
+
expect(method(:get).owner.name).to start_with('ActionDispatch::')
|
|
6
|
+
get '/api/users/1'
|
|
7
|
+
|
|
8
|
+
expect_json('error', message: 'not found')
|
|
9
|
+
expect_json_types('error', message: :string, id: :string)
|
|
10
|
+
expect_json_keys('error', [:message, :id])
|
|
11
|
+
expect_status(:not_found)
|
|
12
|
+
expect_header_contains(:content_type, 'application/json')
|
|
13
|
+
expect { expect_json('error', message: 'wrong') }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'reads the latest response on consecutive Rails requests' do
|
|
17
|
+
get '/api/users/1'
|
|
18
|
+
expect_json('error', id: '1')
|
|
19
|
+
|
|
20
|
+
get '/api/users/2'
|
|
21
|
+
expect_json('error', id: '2')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'accepts the Rails keyword arguments for JSON requests' do
|
|
25
|
+
post '/api/users', params: { name: 'Alex' }, as: :json
|
|
26
|
+
|
|
27
|
+
expect_status(:created)
|
|
28
|
+
expect_json(name: 'Alex')
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
RSpec.describe 'Airborne requests outside Rails request and controller specs' do
|
|
33
|
+
it 'keeps the Airborne requester available when rspec-rails is loaded' do
|
|
34
|
+
external_response = Struct.new(:code, :headers, :body).new(200, {}, '{"ok":true}')
|
|
35
|
+
allow(RestClient::Request).to receive(:execute).and_return(external_response)
|
|
36
|
+
|
|
37
|
+
expect(method(:get).owner).to eq(Airborne::RequestMethods)
|
|
38
|
+
get 'https://example.test/ping'
|
|
39
|
+
|
|
40
|
+
expect_status(200)
|
|
41
|
+
expect_json(ok: true)
|
|
42
|
+
expect(RestClient::Request).to have_received(:execute).with(hash_including(url: 'https://example.test/ping'))
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
RSpec.describe AirborneRailsIntegration::UsersController, type: :controller do
|
|
47
|
+
it 'uses Rails controller helpers and response accessors' do
|
|
48
|
+
expect(method(:get).owner.name).to start_with('ActionController::')
|
|
49
|
+
get :show, params: { id: '1' }
|
|
50
|
+
|
|
51
|
+
expect_status(404)
|
|
52
|
+
expect_json('error', message: 'not found', id: '1')
|
|
53
|
+
expect_header_contains('Content-Type', 'application/json')
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
|
2
|
+
|
|
3
|
+
require 'rspec/core'
|
|
4
|
+
require 'airborne' if ENV.fetch('AIRBORNE_LOAD_ORDER', 'before') == 'before'
|
|
5
|
+
require 'rails'
|
|
6
|
+
require 'action_controller/railtie'
|
|
7
|
+
|
|
8
|
+
module AirborneRailsIntegration
|
|
9
|
+
class Application < Rails::Application
|
|
10
|
+
config.root = File.expand_path('../../tmp/rails-app', __dir__)
|
|
11
|
+
config.eager_load = false
|
|
12
|
+
config.secret_key_base = 'airborne-rails-integration-test-secret'
|
|
13
|
+
config.hosts.clear
|
|
14
|
+
config.logger = Logger.new(File::NULL)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class UsersController < ActionController::API
|
|
18
|
+
def show
|
|
19
|
+
render json: { error: { message: 'not found', id: params[:id] } }, status: :not_found
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def create
|
|
23
|
+
render json: { name: params[:name] }, status: :created
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
AirborneRailsIntegration::Application.initialize!
|
|
29
|
+
AirborneRailsIntegration::Application.routes.draw do
|
|
30
|
+
get '/api/users/:id', to: 'airborne_rails_integration/users#show'
|
|
31
|
+
post '/api/users', to: 'airborne_rails_integration/users#create'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
require 'rspec/rails'
|
|
35
|
+
require 'airborne' unless ENV.fetch('AIRBORNE_LOAD_ORDER', 'before') == 'before'
|
data/lib/airborne/base.rb
CHANGED
|
@@ -2,14 +2,13 @@ require 'json'
|
|
|
2
2
|
require 'active_support'
|
|
3
3
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
4
4
|
require 'airborne/response_headers'
|
|
5
|
+
require 'airborne/request_methods'
|
|
5
6
|
|
|
6
7
|
module Airborne
|
|
7
8
|
class InvalidJsonError < StandardError; end
|
|
8
9
|
|
|
9
10
|
include RequestExpectations
|
|
10
11
|
|
|
11
|
-
attr_reader :response, :headers, :body
|
|
12
|
-
|
|
13
12
|
def self.configure
|
|
14
13
|
RSpec.configure do |config|
|
|
15
14
|
yield config
|
|
@@ -17,6 +16,12 @@ module Airborne
|
|
|
17
16
|
end
|
|
18
17
|
|
|
19
18
|
def self.included(base)
|
|
19
|
+
# Rails request and controller specs own their request helpers and response.
|
|
20
|
+
if defined?(RSpec::Rails::RequestExampleGroup)
|
|
21
|
+
return if base < RSpec::Rails::RequestExampleGroup || base < RSpec::Rails::ControllerExampleGroup
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
base.send(:include, RequestMethods)
|
|
20
25
|
if !Airborne.configuration.requester_module.nil?
|
|
21
26
|
base.send(:include, Airborne.configuration.requester_module)
|
|
22
27
|
elsif !Airborne.configuration.rack_app.nil?
|
|
@@ -30,38 +35,6 @@ module Airborne
|
|
|
30
35
|
RSpec.configuration
|
|
31
36
|
end
|
|
32
37
|
|
|
33
|
-
def get(url, headers = nil, verify_ssl = base_verify_ssl)
|
|
34
|
-
@response = make_request(:get, url, headers: headers, verify_ssl: verify_ssl)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def post(url, post_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
38
|
-
@response = make_request(:post, url, body: post_body, headers: headers, verify_ssl: verify_ssl)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def patch(url, patch_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
42
|
-
@response = make_request(:patch, url, body: patch_body, headers: headers, verify_ssl: verify_ssl)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def put(url, put_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
46
|
-
@response = make_request(:put, url, body: put_body, headers: headers, verify_ssl: verify_ssl)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def delete(url, delete_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
50
|
-
@response = make_request(:delete, url, body: delete_body, headers: headers, verify_ssl: verify_ssl)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def head(url, headers = nil)
|
|
54
|
-
@response = make_request(:head, url, headers: headers)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def options(url, headers = nil)
|
|
58
|
-
@response = make_request(:options, url, headers: headers)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def response
|
|
62
|
-
@response
|
|
63
|
-
end
|
|
64
|
-
|
|
65
38
|
def headers
|
|
66
39
|
ResponseHeaders.new(response.headers.to_h)
|
|
67
40
|
end
|
|
@@ -8,7 +8,7 @@ module Airborne
|
|
|
8
8
|
parts = path.split('.')
|
|
9
9
|
parts.each_with_index do |part, index|
|
|
10
10
|
if part == '*' || part == '?'
|
|
11
|
-
ensure_array(path, json)
|
|
11
|
+
ensure_array(path, json, part)
|
|
12
12
|
type = part
|
|
13
13
|
if index < parts.length.pred
|
|
14
14
|
walk_with_path(type, index, path, parts, json, &block) && return
|
|
@@ -94,8 +94,11 @@ module Airborne
|
|
|
94
94
|
fail error unless error.nil?
|
|
95
95
|
end
|
|
96
96
|
|
|
97
|
-
def ensure_array(path, json)
|
|
97
|
+
def ensure_array(path, json, wildcard)
|
|
98
98
|
fail RSpec::Expectations::ExpectationNotMetError, "Expected #{path} to be array got #{json.class} from JSON response" unless json.class == Array
|
|
99
|
+
if json.empty? && (wildcard == '?' || !Airborne.configuration.allow_empty_arrays?)
|
|
100
|
+
fail RSpec::Expectations::ExpectationNotMetError, "Expected #{path} to match a non-empty array"
|
|
101
|
+
end
|
|
99
102
|
end
|
|
100
103
|
end
|
|
101
104
|
end
|
|
@@ -22,7 +22,7 @@ module Airborne
|
|
|
22
22
|
|
|
23
23
|
def expect_json_keys(*args)
|
|
24
24
|
call_with_path(args) do |param, body|
|
|
25
|
-
expect(body.keys).to include(*param)
|
|
25
|
+
expect(body.keys).to include(*param.map { |key| key.is_a?(String) ? key.to_sym : key })
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
|
|
@@ -134,12 +134,28 @@ module Airborne
|
|
|
134
134
|
end
|
|
135
135
|
|
|
136
136
|
def call_with_path(args)
|
|
137
|
+
expected = normalize_json_keys(args.last)
|
|
137
138
|
if args.length == 2
|
|
138
139
|
get_by_path(args[0], json_body) do |json_chunk|
|
|
139
|
-
yield(
|
|
140
|
+
yield(expected, json_chunk)
|
|
140
141
|
end
|
|
141
142
|
else
|
|
142
|
-
yield(
|
|
143
|
+
yield(expected, json_body)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def normalize_json_keys(value)
|
|
148
|
+
case value
|
|
149
|
+
when Hash
|
|
150
|
+
value.each_with_object({}) do |(key, item), normalized|
|
|
151
|
+
normalized[key.is_a?(String) ? key.to_sym : key] = normalize_json_keys(item)
|
|
152
|
+
end
|
|
153
|
+
when Array
|
|
154
|
+
value.map { |item| normalize_json_keys(item) }
|
|
155
|
+
when OptionalHashTypeExpectations
|
|
156
|
+
OptionalHashTypeExpectations.new(normalize_json_keys(value.hash))
|
|
157
|
+
else
|
|
158
|
+
value
|
|
143
159
|
end
|
|
144
160
|
end
|
|
145
161
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Airborne
|
|
2
|
+
module RequestMethods
|
|
3
|
+
attr_reader :response
|
|
4
|
+
|
|
5
|
+
def get(url, headers = nil, verify_ssl = base_verify_ssl)
|
|
6
|
+
@response = make_request(:get, url, headers: headers, verify_ssl: verify_ssl)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def post(url, post_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
10
|
+
@response = make_request(:post, url, body: post_body, headers: headers, verify_ssl: verify_ssl)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def patch(url, patch_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
14
|
+
@response = make_request(:patch, url, body: patch_body, headers: headers, verify_ssl: verify_ssl)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def put(url, put_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
18
|
+
@response = make_request(:put, url, body: put_body, headers: headers, verify_ssl: verify_ssl)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def delete(url, delete_body = nil, headers = nil, verify_ssl = base_verify_ssl)
|
|
22
|
+
@response = make_request(:delete, url, body: delete_body, headers: headers, verify_ssl: verify_ssl)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def head(url, headers = nil)
|
|
26
|
+
@response = make_request(:head, url, headers: headers)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def options(url, headers = nil)
|
|
30
|
+
@response = make_request(:options, url, headers: headers)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -3,7 +3,7 @@ require 'rest_client'
|
|
|
3
3
|
module Airborne
|
|
4
4
|
module RestClientRequester
|
|
5
5
|
def make_request(method, url, options = {})
|
|
6
|
-
headers = base_headers.merge(options[:headers] || {})
|
|
6
|
+
headers = base_headers.merge(normalize_request_headers(options[:headers] || {}))
|
|
7
7
|
verify_ssl = options.fetch(:verify_ssl, true)
|
|
8
8
|
res = if method == :post || method == :patch || method == :put || method == :delete
|
|
9
9
|
begin
|
|
@@ -38,7 +38,7 @@ module Airborne
|
|
|
38
38
|
|
|
39
39
|
def is_json_request(headers)
|
|
40
40
|
header = headers.fetch(:content_type)
|
|
41
|
-
header == :json ||
|
|
41
|
+
header == :json || /\Aapplication\/(?:[a-z0-9._-]+\+)?json\z/i.match?(header.to_s.split(';', 2).first.to_s.strip)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def is_empty(body)
|
|
@@ -48,7 +48,13 @@ module Airborne
|
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
def base_headers
|
|
51
|
-
{ content_type: :json }.merge(Airborne.configuration.headers || {})
|
|
51
|
+
{ content_type: :json }.merge(normalize_request_headers(Airborne.configuration.headers || {}))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def normalize_request_headers(headers)
|
|
55
|
+
headers.each_with_object({}) do |(name, value), normalized|
|
|
56
|
+
normalized[name.to_s.downcase.tr('-', '_').to_sym] = value
|
|
57
|
+
end
|
|
52
58
|
end
|
|
53
59
|
end
|
|
54
60
|
end
|
data/lib/airborne.rb
CHANGED
|
@@ -11,12 +11,17 @@ RSpec.configure do |config|
|
|
|
11
11
|
config.add_setting :match_actual
|
|
12
12
|
config.add_setting :match_expected_default, default: true
|
|
13
13
|
config.add_setting :match_actual_default, default: false
|
|
14
|
+
config.add_setting :allow_empty_arrays_default, default: true
|
|
15
|
+
config.add_setting :allow_empty_arrays, default: true
|
|
14
16
|
config.add_setting :headers
|
|
15
17
|
config.add_setting :rack_app
|
|
16
18
|
config.add_setting :requester_type
|
|
17
19
|
config.add_setting :requester_module
|
|
18
20
|
config.add_setting :verify_ssl, default: true
|
|
19
21
|
config.before do |example|
|
|
22
|
+
config.allow_empty_arrays = example.metadata.fetch(:allow_empty_arrays) do
|
|
23
|
+
Airborne.configuration.allow_empty_arrays_default?
|
|
24
|
+
end
|
|
20
25
|
config.match_expected = example.metadata[:match_expected].nil? ?
|
|
21
26
|
Airborne.configuration.match_expected_default? : example.metadata[:match_expected]
|
|
22
27
|
config.match_actual = example.metadata[:match_actual].nil? ?
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'empty arrays in wildcard assertions' do
|
|
4
|
+
it 'preserves universal wildcard matching for empty arrays by default' do
|
|
5
|
+
@response = Struct.new(:body).new('[]')
|
|
6
|
+
expect_json_types('*', name: :string)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it 'requires an element for an any-element wildcard' do
|
|
10
|
+
@response = Struct.new(:body).new('[]')
|
|
11
|
+
expect { expect_json_types('?', name: :string) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
12
|
+
expect { expect_json('?.name', 'Alex') }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
context 'when nonempty wildcard results are required', allow_empty_arrays: false do
|
|
16
|
+
it 'rejects empty root arrays for value, type, key, and size assertions' do
|
|
17
|
+
@response = Struct.new(:body).new('[]')
|
|
18
|
+
expect { expect_json('*', name: 'Alex') }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
19
|
+
expect { expect_json_types('*', name: :string) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
20
|
+
expect { expect_json_keys('*', [:name]) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
21
|
+
expect { expect_json_sizes('*', name: 4) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'rejects empty arrays at intermediate and nested wildcards' do
|
|
25
|
+
@response = Struct.new(:body).new('{"cars":[]}')
|
|
26
|
+
expect { expect_json_types('cars.*.owners.*', name: :string) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
27
|
+
@response = Struct.new(:body).new('{"cars":[{"owners":[]}]}')
|
|
28
|
+
expect { expect_json_types('cars.*.owners.*', name: :string) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'accepts matching nonempty arrays and explicit zero-size assertions' do
|
|
32
|
+
@response = Struct.new(:body).new('{"items":[{"name":"Alex"}],"empty":[]}')
|
|
33
|
+
expect_json_types('items.*', name: :string)
|
|
34
|
+
expect_json_sizes(empty: 0)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context 'with nonempty wildcard results required globally' do
|
|
39
|
+
around do |example|
|
|
40
|
+
previous_default = Airborne.configuration.allow_empty_arrays_default
|
|
41
|
+
Airborne.configuration.allow_empty_arrays_default = false
|
|
42
|
+
example.run
|
|
43
|
+
ensure
|
|
44
|
+
Airborne.configuration.allow_empty_arrays_default = previous_default
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'rejects empty wildcard results by default' do
|
|
48
|
+
@response = Struct.new(:body).new('[]')
|
|
49
|
+
expect { expect_json_types('*', name: :string) }.to raise_error(ExpectationNotMetError, /non-empty array/)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'allows an example to opt back in to empty arrays', allow_empty_arrays: true do
|
|
53
|
+
@response = Struct.new(:body).new('[]')
|
|
54
|
+
expect_json_types('*', name: :string)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'string keys in JSON assertions' do
|
|
4
|
+
before do
|
|
5
|
+
@response = Struct.new(:body).new('{"@id":"1234","nested":{"dc:title":"Unicorns"},"items":[{"name":"Alex"}]}')
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it 'matches string and symbol keys in nested value assertions without changing the expectation' do
|
|
9
|
+
expected = { '@id' => '1234', nested: { 'dc:title' => 'Unicorns' } }
|
|
10
|
+
expect_json(expected)
|
|
11
|
+
expect(expected[:nested].keys).to eq(['dc:title'])
|
|
12
|
+
expect { expect_json('@id' => 'wrong') }.to raise_error(ExpectationNotMetError)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'matches string keys in type and optional assertions' do
|
|
16
|
+
expect_json_types('@id' => :string, 'nested' => optional('dc:title' => :string))
|
|
17
|
+
expect_json_types('items.*', 'name' => :string)
|
|
18
|
+
expect { expect_json_types('@id' => :integer) }.to raise_error(ExpectationNotMetError)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'matches string keys in array expectations, key assertions, and size assertions' do
|
|
22
|
+
expect_json('items', [{ 'name' => 'Alex' }])
|
|
23
|
+
expect_json_keys(['@id', :nested])
|
|
24
|
+
expect_json_keys('nested', ['dc:title'])
|
|
25
|
+
expect_json_sizes('items' => 1)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
context 'with exact matching', match_actual: true, match_expected: true do
|
|
29
|
+
it 'does not mistake string keys for extra response keys' do
|
|
30
|
+
expect_json('nested', 'dc:title' => 'Unicorns')
|
|
31
|
+
expect { expect_json('nested', 'dc:title' => 'Unicorns', 'extra' => 'value') }.to raise_error(ExpectationNotMetError)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
context 'with intersection matching', match_actual: false, match_expected: false do
|
|
36
|
+
it 'checks shared keys even when the expectation uses strings' do
|
|
37
|
+
expect { expect_json('@id' => 'wrong') }.to raise_error(ExpectationNotMetError)
|
|
38
|
+
expect { expect_json_types('@id' => :integer) }.to raise_error(ExpectationNotMetError)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'tempfile'
|
|
3
|
+
|
|
4
|
+
describe 'request encoding' do
|
|
5
|
+
around do |example|
|
|
6
|
+
previous_headers = Airborne.configuration.headers
|
|
7
|
+
Airborne.configuration.headers = {}
|
|
8
|
+
example.run
|
|
9
|
+
ensure
|
|
10
|
+
Airborne.configuration.headers = previous_headers
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
['Content-Type', 'content-type', 'CONTENT_TYPE', :content_type].each do |name|
|
|
14
|
+
it "encodes form data using the #{name.inspect} header spelling" do
|
|
15
|
+
stub_request(:post, 'http://www.example.com/login')
|
|
16
|
+
.with(body: { email: 'user', password: 'pass' },
|
|
17
|
+
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' })
|
|
18
|
+
.to_return(body: '{}')
|
|
19
|
+
|
|
20
|
+
post '/login', { email: 'user', password: 'pass' }, name => 'application/x-www-form-urlencoded'
|
|
21
|
+
|
|
22
|
+
expect_status(200)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'lets request headers override equivalent global header names' do
|
|
27
|
+
Airborne.configuration.headers = { 'Content-Type' => 'text/plain', 'X-Token' => 'default' }
|
|
28
|
+
stub_request(:post, 'http://www.example.com/items')
|
|
29
|
+
.with(body: '{"name":"test"}', headers: { 'Content-Type' => 'application/problem+json', 'X-Token' => 'override' })
|
|
30
|
+
.to_return(body: '{}')
|
|
31
|
+
|
|
32
|
+
post '/items', { name: 'test' }, content_type: 'application/problem+json', x_token: 'override'
|
|
33
|
+
|
|
34
|
+
expect_status(200)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'uses a globally configured HTTP Content-Type for form encoding' do
|
|
38
|
+
Airborne.configuration.headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
|
39
|
+
stub_request(:put, 'http://www.example.com/items')
|
|
40
|
+
.with(body: { name: 'test' }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' })
|
|
41
|
+
.to_return(body: '{}')
|
|
42
|
+
|
|
43
|
+
put '/items', { name: 'test' }
|
|
44
|
+
|
|
45
|
+
expect_status(200)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'recognizes case-insensitive JSON media types with parameters' do
|
|
49
|
+
stub_request(:patch, 'http://www.example.com/items')
|
|
50
|
+
.with(body: '{"name":"test"}', headers: { 'Content-Type' => 'Application/Problem+JSON; charset=utf-8' })
|
|
51
|
+
.to_return(body: '{}')
|
|
52
|
+
|
|
53
|
+
patch '/items', { name: 'test' }, 'Content-Type' => 'Application/Problem+JSON; charset=utf-8'
|
|
54
|
+
|
|
55
|
+
expect_status(200)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'does not JSON-encode a binary multipart upload' do
|
|
59
|
+
Tempfile.create(['airborne-upload', '.bin']) do |file|
|
|
60
|
+
file.binmode
|
|
61
|
+
file.write("\xFF\x00\xFE".b)
|
|
62
|
+
file.rewind
|
|
63
|
+
stub_request(:post, 'http://www.example.com/upload').with do |request|
|
|
64
|
+
request.headers['Content-Type'].start_with?('multipart/form-data; boundary=') &&
|
|
65
|
+
request.body.b.include?("\xFF\x00\xFE".b)
|
|
66
|
+
end.to_return(body: '{}')
|
|
67
|
+
|
|
68
|
+
post '/upload', { file: file }, 'Content-Type' => 'multipart/form-data'
|
|
69
|
+
|
|
70
|
+
expect_status(200)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: airborne
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alex Friedman
|
|
@@ -173,12 +173,15 @@ files:
|
|
|
173
173
|
- README.md
|
|
174
174
|
- Rakefile
|
|
175
175
|
- airborne.gemspec
|
|
176
|
+
- integration/rails/request_spec.rb
|
|
177
|
+
- integration/rails/spec_helper.rb
|
|
176
178
|
- lib/airborne.rb
|
|
177
179
|
- lib/airborne/base.rb
|
|
178
180
|
- lib/airborne/optional_hash_type_expectations.rb
|
|
179
181
|
- lib/airborne/path_matcher.rb
|
|
180
182
|
- lib/airborne/rack_test_requester.rb
|
|
181
183
|
- lib/airborne/request_expectations.rb
|
|
184
|
+
- lib/airborne/request_methods.rb
|
|
182
185
|
- lib/airborne/response_headers.rb
|
|
183
186
|
- lib/airborne/rest_client_requester.rb
|
|
184
187
|
- rakelib/changelog.rake
|
|
@@ -186,6 +189,7 @@ files:
|
|
|
186
189
|
- spec/airborne/base_spec.rb
|
|
187
190
|
- spec/airborne/client_requester_spec.rb
|
|
188
191
|
- spec/airborne/delete_spec.rb
|
|
192
|
+
- spec/airborne/expectations/empty_arrays_spec.rb
|
|
189
193
|
- spec/airborne/expectations/expect_header_contains_spec.rb
|
|
190
194
|
- spec/airborne/expectations/expect_header_spec.rb
|
|
191
195
|
- spec/airborne/expectations/expect_json_keys_path_spec.rb
|
|
@@ -203,6 +207,7 @@ files:
|
|
|
203
207
|
- spec/airborne/expectations/expect_json_types_path_spec.rb
|
|
204
208
|
- spec/airborne/expectations/expect_json_types_spec.rb
|
|
205
209
|
- spec/airborne/expectations/expect_status_spec.rb
|
|
210
|
+
- spec/airborne/expectations/string_keys_spec.rb
|
|
206
211
|
- spec/airborne/head_spec.rb
|
|
207
212
|
- spec/airborne/options_spec.rb
|
|
208
213
|
- spec/airborne/patch_spec.rb
|
|
@@ -211,6 +216,7 @@ files:
|
|
|
211
216
|
- spec/airborne/put_spec.rb
|
|
212
217
|
- spec/airborne/rack/rack_requester_spec.rb
|
|
213
218
|
- spec/airborne/rack/rack_sinatra_spec.rb
|
|
219
|
+
- spec/airborne/request_encoding_spec.rb
|
|
214
220
|
- spec/spec_helper.rb
|
|
215
221
|
- spec/stub_helper.rb
|
|
216
222
|
- spec/test_responses/array_of_types.json
|