airborne 0.4.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e82ac09255660ba50383c09a309e13eeafbcbf02c0e6185fc4e9a034aa976043
4
- data.tar.gz: 25624c608eab8122cedb12b433c267eef09ca36538dad9f3dfd73afd38163f29
3
+ metadata.gz: 92b6138c2b98896b6957238ec85c22db1938be163f0ed298923c4d3841dcbfee
4
+ data.tar.gz: 938c41bf4456fa552a8cedb899f2f817d1b756b707a2ef4769a7a6d63e6c57f4
5
5
  SHA512:
6
- metadata.gz: b4ecf6d5a854d54863a2cd53e213baed22b00a9072b31e8769a66e8ca5ee7e832856b580cbc2eb736b3a9a35718b8a3cf437b70193c78fd04d4806c9b1eaaea4
7
- data.tar.gz: 4ba44d43e8ed37e70e05f591e06060a89ba8efa8cb8cefa0001ca382f06505cdc19b38595e77a04bf7de54719f22231dfde9aebbf5abe5364ed4a62b20246899
6
+ metadata.gz: 135a1f6b7ba09b03e2419cd703b025371ab0d43b45d97268ac8cea31ff146f2298226f879e8155b5a0caac1d00c7b6de8862e5ee7df9082952f461371707efd6
7
+ data.tar.gz: d0cb342373291e9f00ee6ae98821535d9a983632a7dbe3f80403a24cf4d561117213dce5a166abac117eb328394128605af41e331236aeabf0a3785e87f2170d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## [v0.4.1](https://github.com/brooklynDev/airborne/tree/v0.4.1) (2026-11-24)
4
10
 
5
11
  - Preserve Rails request helpers and response accessors when Airborne is loaded after `rspec-rails`.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Tests](https://img.shields.io/github/actions/workflow/status/brooklynDev/airborne/tests.yml?branch=master&style=flat-square&label=tests)](https://github.com/brooklynDev/airborne/actions/workflows/tests.yml)
4
4
  [![Coverage](https://img.shields.io/coverallsCoverage/github/brooklynDev/airborne?branch=master&style=flat-square)](https://coveralls.io/github/brooklynDev/airborne?branch=master)
5
- [![Gem version](https://img.shields.io/gem/v/airborne?style=flat-square)](https://rubygems.org/gems/airborne)
5
+ [![Gem version](https://img.shields.io/gem/v/airborne?style=flat-square&cacheSeconds=300)](https://rubygems.org/gems/airborne)
6
6
  [![Total downloads](https://img.shields.io/gem/dt/airborne?style=flat-square)](https://rubygems.org/gems/airborne)
7
7
  [![Stable version downloads](https://img.shields.io/gem/dv/airborne/stable?style=flat-square)](https://rubygems.org/gems/airborne)
8
8
 
@@ -144,6 +144,16 @@ You may set a different content type and post a string body this way:
144
144
  post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }
145
145
  ```
146
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
+
147
157
  For requests that require Query params you can pass a params hash into headers.
148
158
 
149
159
  ```ruby
@@ -278,6 +288,14 @@ end
278
288
 
279
289
  ## API
280
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
+
281
299
  * `expect_json_types` - Tests the types of the JSON property values returned
282
300
  * `expect_json` - Tests the values of the JSON property values returned
283
301
  * `expect_json_keys` - Tests the existence of the specified keys in the JSON object
@@ -375,6 +393,28 @@ it 'should test all elements of the array' do
375
393
  end
376
394
  ```
377
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
+
378
418
  `*` and `?` work for nested arrays as well. Given the following JSON:
379
419
 
380
420
  ```json
data/airborne.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'airborne'
5
- s.version = '0.4.1'
5
+ s.version = '0.4.2'
6
6
  s.date = Date.today.to_s
7
7
  s.summary = 'RSpec driven API testing framework'
8
8
  s.authors = ['Alex Friedman', 'Seth Pollack']
@@ -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(args[1], json_chunk)
140
+ yield(expected, json_chunk)
140
141
  end
141
142
  else
142
- yield(args[0], json_body)
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
 
@@ -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 || /application\/([a-zA-Z0-9\.\_\-]*\+?)json/ =~ header
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.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Friedman
@@ -189,6 +189,7 @@ files:
189
189
  - spec/airborne/base_spec.rb
190
190
  - spec/airborne/client_requester_spec.rb
191
191
  - spec/airborne/delete_spec.rb
192
+ - spec/airborne/expectations/empty_arrays_spec.rb
192
193
  - spec/airborne/expectations/expect_header_contains_spec.rb
193
194
  - spec/airborne/expectations/expect_header_spec.rb
194
195
  - spec/airborne/expectations/expect_json_keys_path_spec.rb
@@ -206,6 +207,7 @@ files:
206
207
  - spec/airborne/expectations/expect_json_types_path_spec.rb
207
208
  - spec/airborne/expectations/expect_json_types_spec.rb
208
209
  - spec/airborne/expectations/expect_status_spec.rb
210
+ - spec/airborne/expectations/string_keys_spec.rb
209
211
  - spec/airborne/head_spec.rb
210
212
  - spec/airborne/options_spec.rb
211
213
  - spec/airborne/patch_spec.rb
@@ -214,6 +216,7 @@ files:
214
216
  - spec/airborne/put_spec.rb
215
217
  - spec/airborne/rack/rack_requester_spec.rb
216
218
  - spec/airborne/rack/rack_sinatra_spec.rb
219
+ - spec/airborne/request_encoding_spec.rb
217
220
  - spec/spec_helper.rb
218
221
  - spec/stub_helper.rb
219
222
  - spec/test_responses/array_of_types.json