README.md in airborne-0.0.16 vs README.md in airborne-0.0.17
- old
+ new
@@ -1,11 +1,11 @@
# Airborne
-[![airborne travis](http://img.shields.io/travis/brooklynDev/airborne.svg?branch=master&style=flat)](https://travis-ci.org/brooklynDev/airborne)
-[![airborne gem version](http://img.shields.io/gem/v/airborne.svg?style=flat)](http://rubygems.org/gems/airborne)
-[![airbore gem downloads](http://img.shields.io/gem/dt/airborne.svg?style=flat)](http://rubygems.org/gems/airborne)
-[![airborne gem stable downloads](http://img.shields.io/gem/dv/airborne/stable.svg?style=flat)](http://rubygems.org/gems/airborne)
+[![airborne travis](http://img.shields.io/travis/brooklynDev/airborne.svg?branch=master&style=flat-square)](https://travis-ci.org/brooklynDev/airborne)
+[![airborne gem version](http://img.shields.io/gem/v/airborne.svg?style=flat-square)](http://rubygems.org/gems/airborne)
+[![airbore gem downloads](http://img.shields.io/gem/dt/airborne.svg?style=flat-square)](http://rubygems.org/gems/airborne)
+[![airborne gem stable downloads](http://img.shields.io/gem/dv/airborne/stable.svg?style=flat-square)](http://rubygems.org/gems/airborne)
RSpec driven API testing framework inspired by [frisby.js](https://github.com/vlucas/frisby)
## Installation
Install Airborne:
@@ -15,19 +15,19 @@
##Creating Tests
require 'airborne'
describe 'sample spec' do
- it 'should validate types' do
- get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" }
- expect_json_types({name: :string})
- end
-
- it 'should validate values' do
- get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" }
- expect_json({:name => "John Doe"})
- end
+ it 'should validate types' do
+ get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" }
+ expect_json_types({name: :string})
+ end
+
+ it 'should validate values' do
+ get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" }
+ expect_json({:name => "John Doe"})
+ end
end
When calling expect_json_types, these are the valid types that can be tested against:
* `:int` or `:integer`
@@ -49,11 +49,18 @@
it 'should validate types' do
get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" } or { "name" : "John Doe", "age" : 45 }
expect_json_types({name: :string, age: :int_or_null})
end
end
-
+
+Additionally, if an entire object could be null, but you'd still want to test the types if it does exist, you can wrap the expectations in a call to `optional`:
+
+ it 'should allow optional nested hash' do
+ get '/simple_path_get' #may or may not return coordinates
+ expect_json_types("address.coordinates", optional({lattitude: :float, longitutde: :float}))
+ end
+
When calling `expect_json`, you can optionally provide a block and run your own `rspec` expectations:
describe 'sample spec' do
it 'should validate types' do
get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" }
@@ -68,10 +75,19 @@
* `response` - The HTTP response returned from the request
* `headers` - A symbolized hash of the response headers returned by the request
* `body` - The raw HTTP body returned from the request
* `json_body` - A symbolized hash representation of the JSON returned by the request
+Fo example:
+
+ it 'should validate types' do
+ get 'http://example.com/api/v1/simple_get' #json api that returns { "name" : "John Doe" }
+ name = json_body[:name] #name will equal "John Doe"
+ body_as_string = body
+ end
+
+
When calling any of the methods above, you can pass request headers to be used.
get 'http://example.com/api/v1/my_api', {'x-auth-token' => 'my_token'}
For requests that require a body (`post`, `put`, `patch`) you can pass the body as a hash as well:
@@ -90,90 +106,90 @@
When calling `expect_json_types`, `expect_json` or `expect_json_keys` you can optionaly specify a path as a first parameter.
For example, if our API returns the following JSON:
{
- "name": "Alex",
- "address": {
- "street": "Area 51",
- "city": "Roswell",
- "state": "NM",
- "coordinates":{
- "lattitude": 33.3872,
- "longitude": 104.5281
- }
- }
+ "name": "Alex",
+ "address": {
+ "street": "Area 51",
+ "city": "Roswell",
+ "state": "NM",
+ "coordinates":{
+ "lattitude": 33.3872,
+ "longitude": 104.5281
+ }
+ }
}
This test would only test the address object:
describe 'path spec' do
- it 'should allow simple path and verify only that path' do
- get 'http://example.com/api/v1/simple_path_get'
- expect_json_types('address', {street: :string, city: :string, state: :string, coordinates: :object })
- #or this
- expect_json_types('address', {street: :string, city: :string, state: :string, coordinates: { lattitude: :float, longitude: :float } })
- end
+ it 'should allow simple path and verify only that path' do
+ get 'http://example.com/api/v1/simple_path_get'
+ expect_json_types('address', {street: :string, city: :string, state: :string, coordinates: :object })
+ #or this
+ expect_json_types('address', {street: :string, city: :string, state: :string, coordinates: { lattitude: :float, longitude: :float } })
+ end
end
Alternativley, if we only want to test `coordinates` we can dot into just the `coordinates`:
- it 'should allow nested paths' do
- get 'http://example.com/api/v1/simple_path_get'
- expect_json('address.coordinates', {lattitude: 33.3872, longitutde: 104.5281} )
- end
+ it 'should allow nested paths' do
+ get 'http://example.com/api/v1/simple_path_get'
+ expect_json('address.coordinates', {lattitude: 33.3872, longitutde: 104.5281} )
+ end
When dealing with `arrays`, we can optionally test all (`*`) or a single (`?` - any, `0` - index) element of the array:
Given the following JSON:
{
- "cars":[
- {"make": "Tesla", "model": "Model S"},
- {"make": "Lamborghini", "model": "Aventador"}
- ]
+ "cars":[
+ {"make": "Tesla", "model": "Model S"},
+ {"make": "Lamborghini", "model": "Aventador"}
+ ]
}
We can test against just the first car like this:
- it 'should index into array and test against specific element' do
- get '/array_api'
- expect_json('cars.0', {make: "Tesla", model: "Model S"})
- end
+ it 'should index into array and test against specific element' do
+ get '/array_api'
+ expect_json('cars.0', {make: "Tesla", model: "Model S"})
+ end
To test the types of all elements in the array:
- it 'should test all elements of the array' do
- get 'http://example.com/api/v1/array_api
- expect_json('cars.?', {make: "Tesla", model: "Model S"}) # tests that one car in array matches the tesla
- expect_json_types('cars.*', {make: :string, model: :string}) # tests all cars in array for make and model of type string
- end
-
+ it 'should test all elements of the array' do
+ get 'http://example.com/api/v1/array_api
+ expect_json('cars.?', {make: "Tesla", model: "Model S"}) # tests that one car in array matches the tesla
+ expect_json_types('cars.*', {make: :string, model: :string}) # tests all cars in array for make and model of type string
+ end
+
`*` and `?` work for nested arrays as well. Given the following JSON:
{
- "cars": [{
- "make": "Tesla",
- "model": "Model S",
- "owners": [{
- "name": "Bart Simpson"
- }]
- }, {
- "make": "Lamborghini",
- "model": "Aventador",
- "owners": [{
- "name": "Peter Griffin"
- }]
- }]
+ "cars": [{
+ "make": "Tesla",
+ "model": "Model S",
+ "owners": [{
+ "name": "Bart Simpson"
+ }]
+ }, {
+ "make": "Lamborghini",
+ "model": "Aventador",
+ "owners": [{
+ "name": "Peter Griffin"
+ }]
+ }]
}
===
it 'should check all nested arrays for specified elements' do
- get 'http://example.com/api/v1/array_with_nested'
- expect_json_types('cars.*.owners.*', {name: :string})
- end
+ get 'http://example.com/api/v1/array_with_nested'
+ expect_json_types('cars.*.owners.*', {name: :string})
+ end
##Configuration
When setting up Airborne, you can call `configure` just like you would with `rspec`:
@@ -198,12 +214,12 @@
## License
The MIT License
-Copyright (c) 2011-2014 brooklyndev, sethpollack
+Copyright (c) 2014 brooklyndev, sethpollack
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.