Top Related Projects
:tada: Makes http fun again!
Simple, but flexible HTTP client library, with support for multiple backends.
Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
Typhoeus wraps libcurl in order to make fast and reliable requests.
The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
Quick Overview
Airborne is a Ruby-based testing library designed for API automation testing. It provides a simple DSL for making HTTP requests and validating responses, making it easier to write and maintain API tests.
Pros
- Easy to use and intuitive DSL for writing API tests
- Supports various HTTP methods and request/response validations
- Integrates well with RSpec for a familiar testing experience
- Lightweight and has minimal dependencies
Cons
- Limited to Ruby ecosystem, not suitable for non-Ruby projects
- May require additional setup for complex authentication scenarios
- Less actively maintained compared to some other API testing tools
- Limited built-in reporting capabilities
Code Examples
- Making a simple GET request and validating the response:
describe "GET /users" do
it "returns a list of users" do
get "/users"
expect_status(200)
expect_json_types(users: :array)
end
end
- Posting data and checking the response:
describe "POST /users" do
it "creates a new user" do
post "/users", { name: "John Doe", email: "john@example.com" }
expect_status(201)
expect_json(name: "John Doe", email: "john@example.com")
end
end
- Testing headers and query parameters:
describe "GET /protected-resource" do
it "requires authentication" do
get "/protected-resource", {}, { "Authorization" => "Bearer token123" }
expect_status(200)
get "/protected-resource?api_key=abc123"
expect_status(200)
end
end
Getting Started
To use Airborne in your project, add it to your Gemfile:
gem 'airborne'
Then, in your spec file, require Airborne and RSpec:
require 'airborne'
RSpec.configure do |config|
config.include Airborne
end
describe "My API" do
# Your tests here
end
Now you can start writing API tests using Airborne's DSL within your RSpec tests.
Competitor Comparisons
:tada: Makes http fun again!
Pros of HTTParty
- More mature and widely adopted library with a larger community
- Supports a broader range of HTTP methods and features
- Can be used for general-purpose HTTP requests, not limited to testing
Cons of HTTParty
- Not specifically designed for API testing, requiring more setup for test scenarios
- Lacks built-in assertion methods for API responses
- May require additional gems or custom code for advanced testing features
Code Comparison
HTTParty:
response = HTTParty.get('https://api.example.com/users')
expect(response.code).to eq(200)
expect(JSON.parse(response.body)).to include('name' => 'John Doe')
Airborne:
get 'https://api.example.com/users'
expect_status(200)
expect_json(name: 'John Doe')
Summary
HTTParty is a versatile HTTP client library suitable for various HTTP-related tasks, including API interactions. It offers more flexibility and features for general HTTP requests. However, for API testing specifically, Airborne provides a more streamlined and test-oriented approach with built-in assertion methods and a focus on JSON APIs. HTTParty may require more setup and additional code for complex API testing scenarios, while Airborne offers a more concise syntax for common API testing tasks.
Simple, but flexible HTTP client library, with support for multiple backends.
Pros of Faraday
- More comprehensive HTTP client library with support for multiple adapters
- Extensive middleware system for request/response processing
- Active development and larger community support
Cons of Faraday
- Steeper learning curve due to more complex architecture
- Potentially overkill for simple API testing scenarios
- Requires more setup and configuration compared to Airborne
Code Comparison
Airborne example:
get '/api/users' do
expect_json_types(users: :array_of_objects)
expect_json('users.0', name: 'John', age: 27)
end
Faraday example:
conn = Faraday.new(url: 'http://api.example.com') do |faraday|
faraday.request :url_encoded
faraday.response :json
faraday.adapter Faraday.default_adapter
end
response = conn.get('/api/users')
expect(response.body['users'].first).to eq({ 'name' => 'John', 'age' => 27 })
Airborne is more concise and focused on API testing, while Faraday provides a more flexible and powerful HTTP client solution. Airborne's syntax is more declarative and easier to read for simple API tests, whereas Faraday offers greater control and customization options for complex HTTP interactions.
Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
Pros of rest-client
- More comprehensive and feature-rich HTTP client library
- Supports a wider range of HTTP methods and request types
- Offers more flexibility in handling complex API interactions
Cons of rest-client
- Steeper learning curve due to its extensive feature set
- May be overkill for simple API testing scenarios
- Less focused on testing-specific functionality compared to Airborne
Code Comparison
rest-client:
require 'rest-client'
response = RestClient.get 'http://example.com/resource'
puts response.code
puts response.body
Airborne:
require 'airborne'
describe 'example' do
it 'should return successful response' do
get 'http://example.com/resource'
expect_status(200)
expect_json_types(key: :string)
end
end
Summary
rest-client is a more comprehensive HTTP client library suitable for various API interactions, while Airborne is specifically designed for API testing with a focus on simplicity and readability. rest-client offers more flexibility but may be more complex for simple testing scenarios, whereas Airborne provides a streamlined approach to API testing with built-in expectations and assertions.
Typhoeus wraps libcurl in order to make fast and reliable requests.
Pros of Typhoeus
- Highly concurrent HTTP requests using libcurl multi interface
- Supports advanced features like parallel requests and request queueing
- Better performance for multiple requests due to connection pooling
Cons of Typhoeus
- More complex API compared to Airborne's simpler interface
- Steeper learning curve for beginners
- Requires libcurl dependency, which may not be available in all environments
Code Comparison
Typhoeus example:
request = Typhoeus::Request.new(
"www.example.com",
method: :post,
body: "this=is&my=body",
headers: { Accept: "text/html" }
)
response = request.run
Airborne example:
post "http://example.com/api/v1/users",
{ name: "John Doe", email: "john@example.com" },
{ "Content-Type" => "application/json" }
Summary
Typhoeus is a powerful HTTP client library focused on performance and advanced features, while Airborne is a simpler, more lightweight library designed for API testing. Typhoeus excels in scenarios requiring concurrent requests and complex HTTP operations, whereas Airborne provides a more intuitive interface for basic API interactions and testing.
The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
Pros of Insomnia
- More comprehensive API development and testing tool with a user-friendly GUI
- Supports a wider range of protocols and authentication methods
- Active development with frequent updates and a large community
Cons of Insomnia
- Heavier resource usage due to its GUI-based nature
- Steeper learning curve for users new to API testing tools
- Less suitable for integration into automated testing pipelines
Code Comparison
Airborne (Ruby):
describe 'example' do
it 'should validate types' do
get '/example' do
expect_json_types(foo: :string, bar: :integer)
end
end
end
Insomnia (JavaScript):
const response = await insomnia.send();
expect(response.status).to.equal(200);
expect(response.data).to.have.property('foo').that.is.a('string');
expect(response.data).to.have.property('bar').that.is.a('number');
Key Differences
- Airborne is a lightweight, Ruby-based testing framework specifically for REST APIs
- Insomnia is a full-featured API client with testing capabilities, supporting multiple protocols
- Airborne is better suited for integration into existing Ruby test suites
- Insomnia offers a more visual approach to API testing and exploration
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Airborne
RSpec driven API testing framework
Looking for Project Maintainers
I am looking for project maintainers to help keep airborne up to date and bug-free while avoiding feature creep and maintaining backwards compatibility.
Comment here if you would like to help out.
Installation
Install Airborne:
$ gem install airborne
Or add it to your Gemfile:
gem 'airborne'
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
end
When calling expect_json_types, these are the valid types that can be tested against:
:int
or:integer
:float
:bool
or:boolean
:string
:date
:object
:null
:array
:array_of_integers
or:array_of_ints
:array_of_floats
:array_of_strings
:array_of_booleans
or:array_of_bools
:array_of_objects
:array_of_arrays
If the properties are optional and may not appear in the response, you can append _or_null
to the types above.
describe 'sample spec' do
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(latitude: :float, longitude: :float))
end
Additionally, when calling expect_json
, you can provide a regex pattern in a call to regex
:
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(name: regex("^John"))
end
end
When calling expect_json
or expect_json_types
, 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" }
expect_json(name: -> (name){ expect(name.length).to eq(8) })
end
end
Calling expect_json_sizes
actually make use of the above feature and call expect_json
under the hood:
describe 'sample spec' do
it 'should validate types' do
get 'http://example.com/api/v1/simple_get_collection' #json api that returns { "ids" : [1, 2, 3, 4] }
expect_json_sizes(ids: 4)
end
end
Making requests
Airborne uses rest_client
to make the HTTP request, and supports all HTTP verbs. When creating a test, you can call any of the following methods: get
, post
, put
, patch
, delete
, head
, options
. This will then give you access the following properties:
response
- The HTTP response returned from the requestheaders
- A symbolized hash of the response headers returned by the requestbody
- The raw HTTP body returned from the requestjson_body
- A symbolized hash representation of the JSON returned by the request
For 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 well:
post 'http://example.com/api/v1/my_api', { :name => 'John Doe' }, { 'x-auth-token' => 'my_token' }
The body may be any JSON-serializable type, as long as you want to post application/json
content type.
You may set a different content type and post a string body this way:
post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }
For requests that require Query params you can pass a params hash into headers.
post 'http://example.com/api/v1/my_api', { }, { 'params' => {'param_key' => 'param_value' } }
(Not) Verifying SSL Certificates
SSL certificate verification is enabled by default (specifically, OpenSSL::SSL::VERIFY_PEER
).
Carefully consider how you use this. It's not a solution for getting around a failed SSL cert verification; rather, it's intended for testing systems that don't have a legitimate SSL cert, such as a development or test environment.
You can override this behavior per request:
verify_ssl = false
post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }, verify_ssl
or with a global Airborne configuration:
Airborne.configure do |config|
config.verify_ssl = false # equivalent to OpenSSL::SSL::VERIFY_NONE
end
Note the per-request option always overrides the Airborne configuration:
before do
Airborne.configuration.verify_ssl = false
end
it 'will still verify the SSL certificate' do
verify_ssl = true
post 'http://example.com/api/v1/my_api', "Hello there!", { content_type: 'text/plain' }, verify_ssl
end
You can use the verify_ssl
setting to override your global defaults in test blocks like this:
describe 'test something', verify_ssl: false do
end
OR
describe 'test something' do
Airborne.configuration.verify_ssl = false
end
This feature currently isn't supported when testing loaded Rack applications (see "Testing Rack Applications" below). If you need to set verify_ssl: false
, then we recommend starting your Rack app server and sending the airborne
HTTP requests as you would when testing any other service.
Testing Rack Applications
If you have an existing Rack application like sinatra
or grape
you can run Airborne against your application and test without actually having a server running. To do that, just specify your rack application in your Airborne configuration:
Airborne.configure do |config|
config.rack_app = MySinatraApp
end
Under the covers, Airborne uses rack-test to make the requests.
Rails Applications
If you're testing an API you've written in Rails, Airborne plays along with rspec-rails
:
require 'rails_helper'
RSpec.describe HomeController, :type => :controller do
describe 'GET index' do
it 'returns correct types' do
get :index, :format => 'json' #if your route responds to both html and json
expect_json_types(foo: :string)
end
end
end
API
expect_json_types
- Tests the types of the JSON property values returnedexpect_json
- Tests the values of the JSON property values returnedexpect_json_keys
- Tests the existence of the specified keys in the JSON objectexpect_json_sizes
- Tests the sizes of the JSON property values returned, also test if the values are arraysexpect_status
- Tests the HTTP status code returnedexpect_header
- Tests for a specified header in the responseexpect_header_contains
- Partial match test on a specified header
Path Matching
When calling expect_json_types
, expect_json
, expect_json_keys
or expect_json_sizes
you can optionally 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": {
"latitude": 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: { latitude: :float, longitude: :float })
end
end
Or, to test the existence of specific keys:
it 'should allow nested paths' do
get 'http://example.com/api/v1/simple_path_get'
expect_json_keys('address', [:street, :city, :state, :coordinates])
end
Alternatively, 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', latitude: 33.3872, longitude: 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"
}
]
}
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
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
*
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"
}
]
}
]
}
===
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
Dates
JSON has no support for dates, however airborne gives you the ability to check for dates using the following. For expect_json_types
you would use it as you would for any of the other types:
it 'should verify date type' do
get '/get_date' #api that returns {createdAt: "Mon Oct 20 2014 16:10:42 GMT-0400 (EDT)"}
expect_json_types(createdAt: :date)
end
However if you want to check the actual date data with expect_json
, you need to call the date
function:
it 'should verify correct date value' do
get '/get_date' #api that returns {createdAt: "Mon Oct 20 2014 16:10:42 GMT-0400 (EDT)"}
prev_day = DateTime.new(2014,10,19)
next_day = DateTime.new(2014,10,21)
#within the date callback, you can use regular RSpec expectations that work with dates
expect_json(createdAt: date { |value| expect(value).to be_between(prev_day, next_day) })
end
Configuration
When setting up Airborne, you can call configure
just like you would with rspec
:
#config is the RSpec configuration and can be used just like it
Airborne.configure do |config|
config.include MyModule
end
Additionally, you can specify a base_url
and default headers
to be used on every request (unless overridden in the actual request):
Airborne.configure do |config|
config.base_url = 'http://example.com/api/v1'
config.headers = { 'x-auth-token' => 'my_token' }
end
describe 'spec' do
it 'now we no longer need the full url' do
get '/simple_get'
expect_json_types(name: :string)
end
end
You can also control the strictness of expect_json
and expect_json_types
with the global settings match_expected_default
and match_actual_default
like this.
Airborne.configure do |config|
config.match_expected_default = true
config.match_actual_default = false
end
match_expected_default
requires all the keys in the expected JSON are present in the response.
match_actual_default
requires that the keys in the response are tested in the expected Hash.
So you can do the following combinations:
match_expected_default=false
, match_actual_default=false
- check only intersection
match_expected_default=false
, match_actual_default=true
- raise on extra key in response
match_expected_default=true
, match_actual_default=false
- raise on missing key in response
match_expected_default=true
, match_actual_default=true
- expect exact match
Airborne sets match_expected_default
to true
and match_actual_default
to false
by default.
You can use the match_expected
and match_actual
settings to override your global defaults in test blocks like this.
describe 'test something', match_expected: true, match_actual: false do
end
OR
describe 'test something' do
Airborne.configuration.match_expected = true
Airborne.configuration.match_actual = false
end
Run it from the CLI
$ cd your/project
$ rspec spec
Authors
Contributors
https://github.com/brooklynDev/airborne/graphs/contributors
Inspired by frisby.js
License
The MIT License
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.
Top Related Projects
:tada: Makes http fun again!
Simple, but flexible HTTP client library, with support for multiple backends.
Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
Typhoeus wraps libcurl in order to make fast and reliable requests.
The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot