hops-airborne 0.2.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +38 -0
- data/.rspec +1 -0
- data/.travis.yml +12 -0
- data/Gemfile +10 -0
- data/LICENSE +9 -0
- data/README.md +429 -0
- data/airborne.gemspec +19 -0
- data/lib/airborne/base.rb +82 -0
- data/lib/airborne/optional_hash_type_expectations.rb +23 -0
- data/lib/airborne/path_matcher.rb +101 -0
- data/lib/airborne/rack_test_requester.rb +18 -0
- data/lib/airborne/request_expectations.rb +285 -0
- data/lib/airborne/rest_client_requester.rb +33 -0
- data/lib/airborne.rb +29 -0
- data/spec/airborne/base_spec.rb +55 -0
- data/spec/airborne/client_requester_spec.rb +42 -0
- data/spec/airborne/delete_spec.rb +9 -0
- data/spec/airborne/expectations/expect_header_contains_spec.rb +21 -0
- data/spec/airborne/expectations/expect_header_spec.rb +21 -0
- data/spec/airborne/expectations/expect_json_keys_path_spec.rb +15 -0
- data/spec/airborne/expectations/expect_json_keys_spec.rb +21 -0
- data/spec/airborne/expectations/expect_json_lambda_spec.rb +9 -0
- data/spec/airborne/expectations/expect_json_options_spec.rb +65 -0
- data/spec/airborne/expectations/expect_json_path_spec.rb +108 -0
- data/spec/airborne/expectations/expect_json_regex_spec.rb +33 -0
- data/spec/airborne/expectations/expect_json_sizes_spec.rb +33 -0
- data/spec/airborne/expectations/expect_json_spec.rb +27 -0
- data/spec/airborne/expectations/expect_json_types_date_spec.rb +52 -0
- data/spec/airborne/expectations/expect_json_types_lambda_spec.rb +9 -0
- data/spec/airborne/expectations/expect_json_types_optional_spec.rb +15 -0
- data/spec/airborne/expectations/expect_json_types_options_spec.rb +59 -0
- data/spec/airborne/expectations/expect_json_types_path_spec.rb +65 -0
- data/spec/airborne/expectations/expect_json_types_spec.rb +85 -0
- data/spec/airborne/expectations/expect_status_spec.rb +23 -0
- data/spec/airborne/head_spec.rb +10 -0
- data/spec/airborne/options_spec.rb +10 -0
- data/spec/airborne/patch_spec.rb +9 -0
- data/spec/airborne/path_spec.rb +34 -0
- data/spec/airborne/post_spec.rb +17 -0
- data/spec/airborne/put_spec.rb +9 -0
- data/spec/airborne/rack/rack_sinatra_spec.rb +43 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/stub_helper.rb +45 -0
- data/spec/test_responses/array_of_types.json +9 -0
- data/spec/test_responses/array_of_values.json +5 -0
- data/spec/test_responses/array_response.json +5 -0
- data/spec/test_responses/array_with_index.json +6 -0
- data/spec/test_responses/array_with_nested.json +16 -0
- data/spec/test_responses/array_with_nested_bad_data.json +15 -0
- data/spec/test_responses/array_with_partial_nested.json +12 -0
- data/spec/test_responses/date_is_null_response.json +3 -0
- data/spec/test_responses/date_response.json +3 -0
- data/spec/test_responses/hash_property.json +3 -0
- data/spec/test_responses/invalid_get.json +1 -0
- data/spec/test_responses/invalid_json.json +1 -0
- data/spec/test_responses/numeric_property.json +6 -0
- data/spec/test_responses/simple_delete.json +4 -0
- data/spec/test_responses/simple_get.json +5 -0
- data/spec/test_responses/simple_json.json +5 -0
- data/spec/test_responses/simple_nested_path.json +12 -0
- data/spec/test_responses/simple_patch.json +4 -0
- data/spec/test_responses/simple_path_get.json +8 -0
- data/spec/test_responses/simple_post.json +4 -0
- data/spec/test_responses/simple_put.json +4 -0
- metadata +206 -0
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json options' do
|
4
|
+
describe 'match_expected', match_expected: true, match_actual: false do
|
5
|
+
it 'should require all expected properties' do
|
6
|
+
mock_get 'simple_get'
|
7
|
+
get '/simple_get'
|
8
|
+
expect{ expect_json(name: 'Alex', other: 'other') }.to raise_error(ExpectationNotMetError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should not require the actual properties' do
|
12
|
+
mock_get 'simple_get'
|
13
|
+
get '/simple_get'
|
14
|
+
expect_json(name: 'Alex')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'match_actual', match_expected: false, match_actual: true do
|
19
|
+
it 'should require all actual properties' do
|
20
|
+
mock_get 'simple_get'
|
21
|
+
get '/simple_get'
|
22
|
+
expect{ expect_json(name: 'Alex') }.to raise_error(ExpectationError)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should not require the expected properties' do
|
26
|
+
mock_get 'simple_get'
|
27
|
+
get '/simple_get'
|
28
|
+
expect_json(name: 'Alex', age: 32, address: nil, other: 'other')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'match_both', match_expected: true, match_actual: true do
|
33
|
+
it 'should require all actual properties' do
|
34
|
+
mock_get 'simple_get'
|
35
|
+
get '/simple_get'
|
36
|
+
expect{ expect_json(name: 'Alex') }.to raise_error(ExpectationError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should require all expected properties' do
|
40
|
+
mock_get 'simple_get'
|
41
|
+
get '/simple_get'
|
42
|
+
expect{ expect_json(name: 'Alex', other: 'other') }.to raise_error(ExpectationNotMetError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should require all expected properties' do
|
46
|
+
mock_get 'simple_get'
|
47
|
+
get '/simple_get'
|
48
|
+
expect{ expect_json(name: 'Alex', nested: {}) }.to raise_error(ExpectationNotMetError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'match_none', match_expected: false, match_actual: false do
|
53
|
+
it 'should not require the actual properties' do
|
54
|
+
mock_get 'simple_get'
|
55
|
+
get '/simple_get'
|
56
|
+
expect_json(name: 'Alex')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should not require the expected properties' do
|
60
|
+
mock_get 'simple_get'
|
61
|
+
get '/simple_get'
|
62
|
+
expect_json(name: 'Alex', age: 32, address: nil, other: 'other', nested: {})
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json with path' do
|
4
|
+
it 'should allow simple path and verify only that path' do
|
5
|
+
mock_get('simple_path_get')
|
6
|
+
get '/simple_path_get'
|
7
|
+
expect_json('address', street: 'Area 51', city: 'Roswell', state: 'NM')
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should allow nested paths' do
|
11
|
+
mock_get('simple_nested_path')
|
12
|
+
get '/simple_nested_path'
|
13
|
+
expect_json('address.coordinates', lattitude: 33.3872, longitutde: 104.5281)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should index into array and test against specific element' do
|
17
|
+
mock_get('array_with_index')
|
18
|
+
get '/array_with_index'
|
19
|
+
expect_json('cars.0', make: 'Tesla', model: 'Model S')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should test against all elements in the array' do
|
23
|
+
mock_get('array_with_index')
|
24
|
+
get '/array_with_index'
|
25
|
+
expect_json('cars.?', make: 'Tesla', model: 'Model S')
|
26
|
+
expect_json('cars.?', make: 'Lamborghini', model: 'Aventador')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should test against properties in the array' do
|
30
|
+
mock_get('array_with_index')
|
31
|
+
get '/array_with_index'
|
32
|
+
expect_json('cars.?.make', 'Tesla')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should ensure at least one match' do
|
36
|
+
mock_get('array_with_index')
|
37
|
+
get '/array_with_index'
|
38
|
+
expect { expect_json('cars.?.make', 'Teslas') }.to raise_error(ExpectationNotMetError)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should check for at least one match' do
|
42
|
+
mock_get('array_with_nested')
|
43
|
+
get '/array_with_nested'
|
44
|
+
expect_json('cars.?.owners.?', name: 'Bart Simpson')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should ensure at least one match' do
|
48
|
+
mock_get('array_with_nested')
|
49
|
+
get '/array_with_nested'
|
50
|
+
expect { expect_json('cars.?.owners.?', name: 'Bart Simpsons') }.to raise_error(ExpectationNotMetError)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should check for one match that matches all ' do
|
54
|
+
mock_get('array_with_nested')
|
55
|
+
get '/array_with_nested'
|
56
|
+
expect_json('cars.?.owners.*', name: 'Bart Simpson')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should check for one match that matches all with lambda' do
|
60
|
+
mock_get('array_with_nested')
|
61
|
+
get '/array_with_nested'
|
62
|
+
expect_json('cars.?.owners.*', name: ->(name) { expect(name).to eq('Bart Simpson') })
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should ensure one match that matches all with lambda' do
|
66
|
+
mock_get('array_with_nested')
|
67
|
+
get '/array_with_nested'
|
68
|
+
expect { expect_json('cars.?.owners.*', name: ->(name) { expect(name).to eq('Bart Simpsons') }) }.to raise_error(ExpectationNotMetError)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should ensure one match that matches all' do
|
72
|
+
mock_get('array_with_nested')
|
73
|
+
get '/array_with_nested'
|
74
|
+
expect { expect_json('cars.?.owners.*', name: 'Bart Simpsons') }.to raise_error(ExpectationNotMetError)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should allow indexing' do
|
78
|
+
mock_get('array_with_nested')
|
79
|
+
get '/array_with_nested'
|
80
|
+
expect_json('cars.0.owners.0', name: 'Bart Simpson')
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should allow strings (String) to be tested against a path' do
|
84
|
+
mock_get('simple_nested_path')
|
85
|
+
get '/simple_nested_path'
|
86
|
+
expect_json('address.city', 'Roswell')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should allow floats (Float) to be tested against a path' do
|
90
|
+
mock_get('simple_nested_path')
|
91
|
+
get '/simple_nested_path'
|
92
|
+
expect_json('address.coordinates.lattitude', 33.3872)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should allow integers (Fixnum, Bignum) to be tested against a path' do
|
96
|
+
mock_get('simple_get')
|
97
|
+
get '/simple_get'
|
98
|
+
expect_json('age', 32)
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should raise ExpectationError when expectation expects an object instead of value' do
|
102
|
+
mock_get('array_with_index')
|
103
|
+
get '/array_with_index'
|
104
|
+
expect do
|
105
|
+
expect_json('cars.0.make', make: 'Tesla')
|
106
|
+
end.to raise_error(ExpectationError, "Expected String Tesla\nto be an object with property make")
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json regex' do
|
4
|
+
it 'should test against regex' do
|
5
|
+
mock_get('simple_get')
|
6
|
+
get '/simple_get'
|
7
|
+
expect_json(name: regex('^A'))
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should raise an error if regex does not match' do
|
11
|
+
mock_get('simple_get')
|
12
|
+
get '/simple_get'
|
13
|
+
expect { expect_json(name: regex('^B')) }.to raise_error(ExpectationNotMetError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should allow regex(Regexp) to be tested against a path' do
|
17
|
+
mock_get('simple_nested_path')
|
18
|
+
get '/simple_nested_path'
|
19
|
+
expect_json('address.city', regex('^R'))
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should allow testing regex against numbers directly' do
|
23
|
+
mock_get('simple_nested_path')
|
24
|
+
get '/simple_nested_path'
|
25
|
+
expect_json('address.coordinates.lattitude', regex('^3'))
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should allow testing regex against numbers in the hash' do
|
29
|
+
mock_get('simple_nested_path')
|
30
|
+
get '/simple_nested_path'
|
31
|
+
expect_json('address.coordinates', lattitude: regex('^3'))
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json_sizes' do
|
4
|
+
it 'should detect sizes' do
|
5
|
+
mock_get('array_of_values')
|
6
|
+
get '/array_of_values'
|
7
|
+
expect_json_sizes(grades: 4, bad: 3, emptyArray: 0)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should allow full object graph' do
|
11
|
+
mock_get('array_with_nested')
|
12
|
+
get '/array_with_nested'
|
13
|
+
expect_json_sizes(cars: { 0 => { owners: 1 }, 1 => { owners: 1 } })
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should allow properties to be tested against a path' do
|
17
|
+
mock_get('array_with_nested')
|
18
|
+
get '/array_with_nested'
|
19
|
+
expect_json_sizes('cars.0.owners', 1)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should test against all elements in the array when path contains * AND expectation is an Integer' do
|
23
|
+
mock_get('array_with_nested')
|
24
|
+
get '/array_with_nested'
|
25
|
+
expect_json_sizes('cars.*.owners', 1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should test against all elements in the array when path contains * AND expectation is a Hash' do
|
29
|
+
mock_get('array_with_nested')
|
30
|
+
get '/array_with_nested'
|
31
|
+
expect_json_sizes('cars.*', owners: 1)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json' do
|
4
|
+
it 'should ensure correct json values' do
|
5
|
+
mock_get('simple_get')
|
6
|
+
get '/simple_get'
|
7
|
+
expect_json(name: 'Alex', age: 32)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should allow array response' do
|
11
|
+
mock_get('array_response')
|
12
|
+
get '/array_response'
|
13
|
+
expect_json([{ name: 'Seth' }])
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should fail when incorrect json is tested' do
|
17
|
+
mock_get('simple_get')
|
18
|
+
get '/simple_get'
|
19
|
+
expect { expect_json(bad: 'data') }.to raise_error(ExpectationNotMetError)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should allow full object graph' do
|
23
|
+
mock_get('simple_path_get')
|
24
|
+
get '/simple_path_get'
|
25
|
+
expect_json(name: 'Alex', address: { street: 'Area 51', city: 'Roswell', state: 'NM' })
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
describe 'expect_json_types with date' do
|
5
|
+
it 'should verify correct date types' do
|
6
|
+
mock_get('date_response')
|
7
|
+
get '/date_response'
|
8
|
+
expect_json_types(createdAt: :date)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should verify correct date types with path' do
|
12
|
+
mock_get('date_response')
|
13
|
+
get '/date_response'
|
14
|
+
expect_json_types('createdAt', :date)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'expect_json with date' do
|
19
|
+
it 'should verify correct date value' do
|
20
|
+
mock_get('date_response')
|
21
|
+
get '/date_response'
|
22
|
+
prev_day = DateTime.new(2014, 10, 19)
|
23
|
+
next_day = DateTime.new(2014, 10, 21)
|
24
|
+
expect_json(createdAt: date { |value| expect(value).to be_between(prev_day, next_day) })
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'expect_json_types with date_or_null' do
|
29
|
+
it 'should verify date_or_null when date is null' do
|
30
|
+
mock_get('date_is_null_response')
|
31
|
+
get '/date_is_null_response'
|
32
|
+
expect_json_types(dateDeleted: :date_or_null)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should verify date_or_null when date is null with path' do
|
36
|
+
mock_get('date_is_null_response')
|
37
|
+
get '/date_is_null_response'
|
38
|
+
expect_json_types('dateDeleted', :date_or_null)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should verify date_or_null with date' do
|
42
|
+
mock_get('date_response')
|
43
|
+
get '/date_response'
|
44
|
+
expect_json_types(createdAt: :date_or_null)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should verify date_or_null with date with path' do
|
48
|
+
mock_get('date_response')
|
49
|
+
get '/date_response'
|
50
|
+
expect_json_types('createdAt', :date_or_null)
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json_types optional' do
|
4
|
+
it 'should test optional nested hash when exists' do
|
5
|
+
mock_get('simple_nested_path')
|
6
|
+
get '/simple_nested_path'
|
7
|
+
expect_json_types('address.coordinates', optional(lattitude: :float, longitutde: :float))
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should allow optional nested hash' do
|
11
|
+
mock_get('simple_path_get')
|
12
|
+
get '/simple_path_get'
|
13
|
+
expect_json_types('address.coordinates', optional(lattitude: :float, longitutde: :float))
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json_types options' do
|
4
|
+
describe 'match_expected', match_expected: true, match_actual: false do
|
5
|
+
it 'should require all expected properties' do
|
6
|
+
mock_get 'simple_get'
|
7
|
+
get '/simple_get'
|
8
|
+
expect{ expect_json_types(name: :string, other: :string) }.to raise_error(ExpectationNotMetError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should not require the actual properties' do
|
12
|
+
mock_get 'simple_get'
|
13
|
+
get '/simple_get'
|
14
|
+
expect_json_types(name: :string)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'match_actual', match_expected: false, match_actual: true do
|
19
|
+
it 'should require all actual properties' do
|
20
|
+
mock_get 'simple_get'
|
21
|
+
get '/simple_get'
|
22
|
+
expect{ expect_json_types(name: :string) }.to raise_error(ExpectationError)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should not require the expected properties' do
|
26
|
+
mock_get 'simple_get'
|
27
|
+
get '/simple_get'
|
28
|
+
expect_json_types(name: :string, age: :int, address: :null, other: :string)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'match_both', match_expected: true, match_actual: true do
|
33
|
+
it 'should require all actual properties' do
|
34
|
+
mock_get 'simple_get'
|
35
|
+
get '/simple_get'
|
36
|
+
expect{ expect_json_types(name: :string) }.to raise_error(ExpectationError)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should require all expected properties' do
|
40
|
+
mock_get 'simple_get'
|
41
|
+
get '/simple_get'
|
42
|
+
expect{ expect_json_types(name: :string, other: :string) }.to raise_error(ExpectationNotMetError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'match_none', match_expected: false, match_actual: false do
|
47
|
+
it 'should not require the actual properties' do
|
48
|
+
mock_get 'simple_get'
|
49
|
+
get '/simple_get'
|
50
|
+
expect_json_types(name: :string)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should not require the expected properties' do
|
54
|
+
mock_get 'simple_get'
|
55
|
+
get '/simple_get'
|
56
|
+
expect_json_types(name: :string, age: :int, address: :null, other: :string)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json_types wih path' do
|
4
|
+
it 'should allow simple path and verify only that path' do
|
5
|
+
mock_get('simple_path_get')
|
6
|
+
get '/simple_path_get'
|
7
|
+
expect_json_types('address', street: :string, city: :string, state: :string)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should allow nested paths' do
|
11
|
+
mock_get('simple_nested_path')
|
12
|
+
get '/simple_nested_path'
|
13
|
+
expect_json_types('address.coordinates', lattitude: :float, longitutde: :float)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should index into array and test against specific element' do
|
17
|
+
mock_get('array_with_index')
|
18
|
+
get '/array_with_index'
|
19
|
+
expect_json_types('cars.0', make: :string, model: :string)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should allow properties to be tested against a path' do
|
23
|
+
mock_get('array_with_index')
|
24
|
+
get '/array_with_index'
|
25
|
+
expect_json_types('cars.0.make', :string)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should test against all elements in the array' do
|
29
|
+
mock_get('array_with_index')
|
30
|
+
get '/array_with_index'
|
31
|
+
expect_json_types('cars.*', make: :string, model: :string)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should ensure all elements of array are valid' do
|
35
|
+
mock_get('array_with_index')
|
36
|
+
get '/array_with_index'
|
37
|
+
expect { expect_json_types('cars.*', make: :string, model: :int) }.to raise_error(ExpectationNotMetError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should deep symbolize array responses' do
|
41
|
+
mock_get('array_response')
|
42
|
+
get '/array_response'
|
43
|
+
expect_json_types('*', name: :string)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should check all nested arrays for specified elements' do
|
47
|
+
mock_get('array_with_nested')
|
48
|
+
get '/array_with_nested'
|
49
|
+
expect_json_types('cars.*.owners.*', name: :string)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should ensure all nested arrays contain correct data' do
|
53
|
+
mock_get('array_with_nested_bad_data')
|
54
|
+
get '/array_with_nested_bad_data'
|
55
|
+
expect { expect_json_types('cars.*.owners.*', name: :string) }.to raise_error(ExpectationNotMetError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should raise ExpectationError when expectation expects an object instead of type' do
|
59
|
+
mock_get('array_with_index')
|
60
|
+
get '/array_with_index'
|
61
|
+
expect do
|
62
|
+
expect_json_types('cars.0.make', make: :string)
|
63
|
+
end.to raise_error(ExpectationError, "Expected String Tesla\nto be an object with property make")
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_json_types' do
|
4
|
+
it 'should detect current type' do
|
5
|
+
mock_get('simple_get')
|
6
|
+
get '/simple_get'
|
7
|
+
expect_json_types(name: :string, age: :int)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should fail when incorrect json types tested' do
|
11
|
+
mock_get('simple_get')
|
12
|
+
get '/simple_get'
|
13
|
+
expect { expect_json_types(bad: :bool) }.to raise_error(ExpectationNotMetError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should not fail when optional property is not present' do
|
17
|
+
mock_get('simple_get')
|
18
|
+
get '/simple_get'
|
19
|
+
expect_json_types(name: :string, age: :int, optional: :bool_or_null)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should allow full object graph' do
|
23
|
+
mock_get('simple_path_get')
|
24
|
+
get '/simple_path_get'
|
25
|
+
expect_json_types({name: :string, address: { street: :string, city: :string, state: :string }})
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should check all types in a simple array' do
|
29
|
+
mock_get('array_of_values')
|
30
|
+
get '/array_of_values'
|
31
|
+
expect_json_types(grades: :array_of_ints)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should ensure all valid types in a simple array' do
|
35
|
+
mock_get('array_of_values')
|
36
|
+
get '/array_of_values'
|
37
|
+
expect { expect_json_types(bad: :array_of_ints) }.to raise_error(ExpectationNotMetError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should allow array of types to be null" do
|
41
|
+
mock_get('array_of_types')
|
42
|
+
get '/array_of_types'
|
43
|
+
expect_json_types(nil_array: :array_or_null)
|
44
|
+
expect_json_types(nil_array: :array_of_integers_or_null)
|
45
|
+
expect_json_types(nil_array: :array_of_ints_or_null)
|
46
|
+
expect_json_types(nil_array: :array_of_floats_or_null)
|
47
|
+
expect_json_types(nil_array: :array_of_strings_or_null)
|
48
|
+
expect_json_types(nil_array: :array_of_booleans_or_null)
|
49
|
+
expect_json_types(nil_array: :array_of_bools_or_null)
|
50
|
+
expect_json_types(nil_array: :array_of_objects_or_null)
|
51
|
+
expect_json_types(nil_array: :array_of_arrays_or_null)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should check array types when not null" do
|
55
|
+
mock_get('array_of_types')
|
56
|
+
get '/array_of_types'
|
57
|
+
expect_json_types(array_of_ints: :array_or_null)
|
58
|
+
expect_json_types(array_of_ints: :array_of_integers_or_null)
|
59
|
+
expect_json_types(array_of_ints: :array_of_ints_or_null)
|
60
|
+
expect_json_types(array_of_floats: :array_of_floats_or_null)
|
61
|
+
expect_json_types(array_of_strings: :array_of_strings_or_null)
|
62
|
+
expect_json_types(array_of_bools: :array_of_booleans_or_null)
|
63
|
+
expect_json_types(array_of_bools: :array_of_bools_or_null)
|
64
|
+
expect_json_types(array_of_objects: :array_of_objects_or_null)
|
65
|
+
expect_json_types(array_of_arrays: :array_of_arrays_or_null)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should allow empty array' do
|
69
|
+
mock_get('array_of_values')
|
70
|
+
get '/array_of_values'
|
71
|
+
expect_json_types(emptyArray: :array_of_ints)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should be able to test for a nil type' do
|
75
|
+
mock_get('simple_get')
|
76
|
+
get '/simple_get'
|
77
|
+
expect_json_types(name: :string, age: :int, address: :null)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'Should throw bad type error' do
|
81
|
+
mock_get('simple_get')
|
82
|
+
get '/simple_get'
|
83
|
+
expect { expect_json_types(name: :foo) }.to raise_error(ExpectationError, "Expected type foo\nis an invalid type")
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect_status' do
|
4
|
+
it 'should verify correct status code' do
|
5
|
+
mock_get('simple_get')
|
6
|
+
get '/simple_get'
|
7
|
+
expect_status 200
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should fail when incorrect status code is returned' do
|
11
|
+
mock_get('simple_get')
|
12
|
+
get '/simple_get'
|
13
|
+
expect { expect_status 123 }.to raise_error(ExpectationNotMetError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should translate symbol codes to whatever is appropriate for the request' do
|
17
|
+
mock_get('simple_get')
|
18
|
+
get '/simple_get'
|
19
|
+
expect_status :ok
|
20
|
+
expect_status 200
|
21
|
+
expect_status '200'
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'expect path' do
|
4
|
+
describe 'errors' do
|
5
|
+
before :each do
|
6
|
+
mock_get('array_with_index')
|
7
|
+
get '/array_with_index'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should raise PathError when incorrect path containing .. is used' do
|
11
|
+
expect do
|
12
|
+
expect_json('cars..make', 'Tesla')
|
13
|
+
end.to raise_error(PathError, "Invalid Path, contains '..'")
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should raise PathError when trying to call property on an array' do
|
17
|
+
expect do
|
18
|
+
expect_json('cars.make', 'Tesla')
|
19
|
+
end.to raise_error(PathError, "Expected Array\nto be an object with property make")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should work with numberic properties' do
|
24
|
+
mock_get('numeric_property')
|
25
|
+
get '/numeric_property'
|
26
|
+
expect_json('cars.0.make', 'Tesla')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should work with numberic properties' do
|
30
|
+
mock_get('numeric_property')
|
31
|
+
get '/numeric_property'
|
32
|
+
expect_json_keys('cars.0', [:make, :model])
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'webmock/rspec'
|
3
|
+
|
4
|
+
describe 'post' do
|
5
|
+
it 'should allow testing on post requests' do
|
6
|
+
mock_post('simple_post')
|
7
|
+
post '/simple_post', {}
|
8
|
+
expect_json_types(status: :string, someNumber: :int)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should allow testing on post requests' do
|
12
|
+
url = 'http://www.example.com/simple_post'
|
13
|
+
stub_request(:post, url)
|
14
|
+
post '/simple_post', 'hello', content_type: 'text/plain'
|
15
|
+
expect(WebMock).to have_requested(:post, url).with(body: 'hello', headers: { 'Content-Type' => 'text/plain' })
|
16
|
+
end
|
17
|
+
end
|