airborne 0.1.15 → 0.1.16

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -0
  3. data/airborne.gemspec +4 -4
  4. data/lib/airborne.rb +1 -2
  5. data/lib/airborne/base.rb +20 -26
  6. data/lib/airborne/optional_hash_type_expectations.rb +3 -3
  7. data/lib/airborne/path_matcher.rb +10 -12
  8. data/lib/airborne/rack_test_requester.rb +1 -0
  9. data/lib/airborne/request_expectations.rb +26 -31
  10. data/lib/airborne/rest_client_requester.rb +16 -17
  11. data/spec/airborne/base_spec.rb +10 -10
  12. data/spec/airborne/delete_spec.rb +2 -2
  13. data/spec/airborne/expectations/expect_header_contains_spec.rb +6 -8
  14. data/spec/airborne/expectations/expect_header_spec.rb +5 -8
  15. data/spec/airborne/expectations/expect_json_keys_path_spec.rb +4 -6
  16. data/spec/airborne/expectations/expect_json_keys_spec.rb +3 -5
  17. data/spec/airborne/expectations/expect_json_lambda_spec.rb +2 -4
  18. data/spec/airborne/expectations/expect_json_path_spec.rb +72 -73
  19. data/spec/airborne/expectations/expect_json_regex_spec.rb +25 -27
  20. data/spec/airborne/expectations/expect_json_sizes_spec.rb +3 -5
  21. data/spec/airborne/expectations/expect_json_spec.rb +10 -6
  22. data/spec/airborne/expectations/expect_json_types_date_spec.rb +18 -21
  23. data/spec/airborne/expectations/expect_json_types_lambda_spec.rb +3 -5
  24. data/spec/airborne/expectations/expect_json_types_optional_spec.rb +11 -13
  25. data/spec/airborne/expectations/expect_json_types_path_spec.rb +52 -54
  26. data/spec/airborne/expectations/expect_json_types_spec.rb +18 -8
  27. data/spec/airborne/expectations/expect_status_spec.rb +6 -6
  28. data/spec/airborne/head_spec.rb +2 -2
  29. data/spec/airborne/options_spec.rb +2 -2
  30. data/spec/airborne/patch_spec.rb +2 -2
  31. data/spec/airborne/path_spec.rb +28 -30
  32. data/spec/airborne/post_spec.rb +3 -3
  33. data/spec/airborne/put_spec.rb +2 -2
  34. data/spec/airborne/rack/rack_sinatra_spec.rb +14 -7
  35. data/spec/spec_helper.rb +0 -1
  36. data/spec/stub_helper.rb +3 -4
  37. data/spec/test_responses/array_with_partial_nested.json +12 -0
  38. data/spec/test_responses/hash_property.json +3 -0
  39. data/spec/test_responses/simple_get.json +2 -1
  40. metadata +4 -2
@@ -1,11 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'expect_json_types lambda' do
4
-
5
- it 'should invoke proc passed in' do
4
+ it 'should invoke proc passed in' do
6
5
  mock_get('simple_get')
7
6
  get '/simple_get'
8
- expect_json_types({name: ->(name){expect(name.length).to eq(4)}})
7
+ expect_json_types(name: ->(name) { expect(name.length).to eq(4) })
9
8
  end
10
-
11
- end
9
+ end
@@ -1,17 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
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
4
9
 
5
- it 'should test optional nested hash when exists' do
6
- mock_get('simple_nested_path')
7
- get '/simple_nested_path'
8
- expect_json_types("address.coordinates", optional({lattitude: :float, longitutde: :float}))
9
- end
10
-
11
- it 'should allow optional nested hash' do
12
- mock_get('simple_path_get')
13
- get '/simple_path_get'
14
- expect_json_types("address.coordinates", optional({lattitude: :float, longitutde: :float}))
15
- end
16
-
17
- end
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
@@ -1,67 +1,65 @@
1
1
  require 'spec_helper'
2
2
 
3
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
4
9
 
5
- it 'should allow simple path and verify only that path' do
6
- mock_get('simple_path_get')
7
- get '/simple_path_get'
8
- expect_json_types('address', {street: :string, city: :string, state: :string})
9
- end
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
10
15
 
11
- it 'should allow nested paths' do
12
- mock_get('simple_nested_path')
13
- get '/simple_nested_path'
14
- expect_json_types('address.coordinates', {lattitude: :float, longitutde: :float} )
15
- end
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
16
21
 
17
- it 'should index into array and test against specific element' do
18
- mock_get('array_with_index')
19
- get '/array_with_index'
20
- expect_json_types('cars.0', {make: :string, model: :string})
21
- end
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
22
27
 
23
- it 'should allow properties to be tested against a path' do
24
- mock_get('array_with_index')
25
- get '/array_with_index'
26
- expect_json_types('cars.0.make', :string)
27
- end
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
28
33
 
29
- it 'should test against all elements in the array' do
30
- mock_get('array_with_index')
31
- get '/array_with_index'
32
- expect_json_types('cars.*', {make: :string, model: :string})
33
- end
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
38
+ end
34
39
 
35
- it 'should ensure all elements of array are valid' do
36
- mock_get('array_with_index')
37
- get '/array_with_index'
38
- expect{expect_json_types('cars.*', {make: :string, model: :int})}.to raise_error
39
- end
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
40
45
 
41
- it 'should deep symbolize array responses' do
42
- mock_get('array_response')
43
- get '/array_response'
44
- expect_json_types("*", {name: :string})
45
- end
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
46
51
 
47
- it 'should check all nested arrays for specified elements' do
48
- mock_get('array_with_nested')
49
- get '/array_with_nested'
50
- expect_json_types('cars.*.owners.*', {name: :string})
51
- end
52
-
53
- it 'should ensure all nested arrays contain correct data' do
54
- mock_get('array_with_nested_bad_data')
55
- get '/array_with_nested_bad_data'
56
- expect{expect_json_types('cars.*.owners.*', {name: :string})}.to raise_error
57
- end
58
-
59
- it "should raise ExpectationError when expectation expects an object instead of type" do
60
- mock_get('array_with_index')
61
- get '/array_with_index'
62
- expect do
63
- expect_json_types('cars.0.make', {make: :string})
64
- end.to raise_error(Airborne::ExpectationError, "Expected String Tesla\nto be an object with property make")
65
- end
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
56
+ end
66
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(Airborne::ExpectationError, "Expected String Tesla\nto be an object with property make")
64
+ end
67
65
  end
@@ -1,47 +1,57 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'expect_json_types' do
4
-
5
4
  it 'should detect current type' do
6
5
  mock_get('simple_get')
7
6
  get '/simple_get'
8
- expect_json_types({name: :string, age: :int})
7
+ expect_json_types(name: :string, age: :int)
9
8
  end
10
9
 
11
10
  it 'should fail when incorrect json types tested' do
12
11
  mock_get('simple_get')
13
12
  get '/simple_get'
14
- expect{expect_json_types({bad: :bool})}.to raise_error
13
+ expect { expect_json_types(bad: :bool) }.to raise_error
15
14
  end
16
15
 
17
16
  it 'should not fail when optional property is not present' do
18
17
  mock_get('simple_get')
19
18
  get '/simple_get'
20
- expect_json_types({name: :string, age: :int, optional: :bool_or_null })
19
+ expect_json_types(name: :string, age: :int, optional: :bool_or_null)
21
20
  end
22
21
 
23
22
  it 'should allow full object graph' do
24
23
  mock_get('simple_path_get')
25
24
  get '/simple_path_get'
26
- expect_json_types({name: :string, address: {street: :string, city: :string, state: :string}})
25
+ expect_json_types(name: :string, address: { street: :string, city: :string, state: :string })
27
26
  end
28
27
 
29
28
  it 'should check all types in a simple array' do
30
29
  mock_get('array_of_values')
31
30
  get '/array_of_values'
32
- expect_json_types({grades: :array_of_ints})
31
+ expect_json_types(grades: :array_of_ints)
33
32
  end
34
33
 
35
34
  it 'should ensure all valid types in a simple array' do
36
35
  mock_get('array_of_values')
37
36
  get '/array_of_values'
38
- expect{expect_json_types({bad: :array_of_ints})}.to raise_error
37
+ expect { expect_json_types(bad: :array_of_ints) }.to raise_error
39
38
  end
40
39
 
41
40
  it 'should allow empty array' do
42
41
  mock_get('array_of_values')
43
42
  get '/array_of_values'
44
- expect_json_types({emptyArray: :array_of_ints})
43
+ expect_json_types(emptyArray: :array_of_ints)
44
+ end
45
+
46
+ it 'should be able to test for a nil type' do
47
+ mock_get('simple_get')
48
+ get '/simple_get'
49
+ expect_json_types(name: :string, age: :int, address: :null)
45
50
  end
46
51
 
52
+ it 'Should throw bad type error' do
53
+ mock_get('simple_get')
54
+ get '/simple_get'
55
+ expect { expect_json_types(name: :foo) }.to raise_error(Airborne::ExpectationError, "Expected type foo\nis an invalid type")
56
+ end
47
57
  end
@@ -4,20 +4,20 @@ describe 'expect_status' do
4
4
  it 'should verify correct status code' do
5
5
  mock_get('simple_get')
6
6
  get '/simple_get'
7
- expect_status(200)
7
+ expect_status 200
8
8
  end
9
9
 
10
10
  it 'should fail when incorrect status code is returned' do
11
11
  mock_get('simple_get')
12
12
  get '/simple_get'
13
- expect{expect_status(123)}.to raise_error
13
+ expect { expect_status 123 }.to raise_error
14
14
  end
15
15
 
16
16
  it 'should translate symbol codes to whatever is appropriate for the request' do
17
17
  mock_get('simple_get')
18
18
  get '/simple_get'
19
- expect_status(:ok)
20
- expect_status(200)
21
- expect_status('200')
19
+ expect_status :ok
20
+ expect_status 200
21
+ expect_status '200'
22
22
  end
23
- end
23
+ end
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe 'head' do
4
4
  it 'should allow testing on head requests' do
5
- mock_head('simple_head', {'foo' => 'foo'})
5
+ mock_head('simple_head', 'foo' => 'foo')
6
6
  head '/simple_head', {}
7
- expect_status(200)
7
+ expect_status 200
8
8
  expect_header('foo', 'foo')
9
9
  end
10
10
  end
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe 'head' do
4
4
  it 'should allow testing on options requests' do
5
- mock_options('simple_options', {'foo' => 'foo'})
5
+ mock_options('simple_options', 'foo' => 'foo')
6
6
  options '/simple_options', {}
7
- expect_status(200)
7
+ expect_status 200
8
8
  expect_header('foo', 'foo')
9
9
  end
10
10
  end
@@ -4,6 +4,6 @@ describe 'patch' do
4
4
  it 'should allow testing on patch requests' do
5
5
  mock_patch('simple_patch')
6
6
  patch '/simple_patch', {}
7
- expect_json_types({status: :string, someNumber: :int})
7
+ expect_json_types(status: :string, someNumber: :int)
8
8
  end
9
- end
9
+ end
@@ -1,36 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'expect path' do
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
4
9
 
5
- describe "errors" do
6
- before :each do
7
- mock_get('array_with_index')
8
- get '/array_with_index'
9
- end
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(Airborne::PathError, "Invalid Path, contains '..'")
14
+ end
10
15
 
11
- it "should raise PathError when incorrect path containing .. is used" do
12
- expect do
13
- expect_json('cars..make', "Tesla")
14
- end.to raise_error(Airborne::PathError, "Invalid Path, contains '..'")
15
- end
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(Airborne::PathError, "Expected Array\nto be an object with property make")
20
+ end
21
+ end
16
22
 
17
- it "should raise PathError when trying to call property on an array" do
18
- expect do
19
- expect_json('cars.make', "Tesla")
20
- end.to raise_error(Airborne::PathError, "Expected Array\nto to be an object with property make")
21
- end
22
- end
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
23
28
 
24
- it "should work with numberic properties" do
25
- mock_get('numeric_property')
26
- get '/numeric_property'
27
- expect_json('cars.0.make', "Tesla")
28
- end
29
-
30
- it "should work with numberic properties" do
31
- mock_get('numeric_property')
32
- get '/numeric_property'
33
- expect_json_keys('cars.0',[:make, :model])
34
- end
35
-
36
- end
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
@@ -5,13 +5,13 @@ describe 'post' do
5
5
  it 'should allow testing on post requests' do
6
6
  mock_post('simple_post')
7
7
  post '/simple_post', {}
8
- expect_json_types({status: :string, someNumber: :int})
8
+ expect_json_types(status: :string, someNumber: :int)
9
9
  end
10
10
 
11
11
  it 'should allow testing on post requests' do
12
12
  url = 'http://www.example.com/simple_post'
13
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'})
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
16
  end
17
17
  end
@@ -4,6 +4,6 @@ describe 'put' do
4
4
  it 'should allow testing on put requests' do
5
5
  mock_put('simple_put')
6
6
  put '/simple_put', {}
7
- expect_json_types({status: :string, someNumber: :int})
7
+ expect_json_types(status: :string, someNumber: :int)
8
8
  end
9
- end
9
+ end
@@ -3,10 +3,11 @@ require 'sinatra'
3
3
 
4
4
  class SampleApp < Sinatra::Application
5
5
  before do
6
- content_type 'application/json'
7
- end
6
+ content_type 'application/json'
7
+ end
8
+
8
9
  get '/' do
9
- {foo: "bar"}.to_json
10
+ { foo: 'bar' }.to_json
10
11
  end
11
12
  end
12
13
 
@@ -17,11 +18,17 @@ end
17
18
  describe 'rack app' do
18
19
  it 'should allow requests against a sinatra app' do
19
20
  get '/'
20
- expect_json_types({foo: :string})
21
+ expect_json_types(foo: :string)
21
22
  end
22
23
 
23
24
  it 'should ensure correct values from sinatra app' do
24
25
  get '/'
25
- expect{expect_json_types({foo: :int})}.to raise_error
26
- end
27
- end
26
+ expect { expect_json_types(foo: :int) }.to raise_error
27
+ end
28
+
29
+ it 'Should set json_body even when not using the airborne http requests' do
30
+ Response = Struct.new(:body, :headers)
31
+ @response = Response.new({ foo: 'bar' }.to_json)
32
+ expect(json_body).to eq(foo: 'bar')
33
+ end
34
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,4 +7,3 @@ Airborne.configure do |config|
7
7
  config.base_url = 'http://www.example.com'
8
8
  config.include StubHelper
9
9
  end
10
-
data/spec/stub_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'webmock/rspec'
2
2
 
3
3
  module StubHelper
4
-
5
4
  def initialize(*args)
6
5
  @base_url = 'http://www.example.com/'
7
6
  end
@@ -39,7 +38,7 @@ module StubHelper
39
38
 
40
39
  private
41
40
 
42
- def get_json_response_file(name)
43
- IO.read(File.join('spec/test_responses', name + ".json"))
44
- end
41
+ def get_json_response_file(name)
42
+ IO.read(File.join('spec/test_responses', name + '.json'))
43
+ end
45
44
  end