heisenberg 0.0.1
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.
- data/.gitignore +18 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +75 -0
- data/Rakefile +2 -0
- data/heisenberg.gemspec +14 -0
- data/lib/heisenberg.rb +28 -0
- data/lib/heisenberg/api_integration_spec.rb +39 -0
- data/lib/heisenberg/hashext.rb +14 -0
- data/lib/heisenberg/version.rb +3 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Marcello Milhomem Albuquerque
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
Heisenberg
|
2
|
+
=======
|
3
|
+
|
4
|
+
Acceptance test for REST apis.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
---------
|
8
|
+
|
9
|
+
Simply add the gem to your project's Gemfile:
|
10
|
+
|
11
|
+
gem 'heisenberg', group: :test
|
12
|
+
|
13
|
+
and then run the bundle command:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
|
20
|
+
First of all, we must set our spec to use Heisenberg by setting the spec type to rest_api.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require "spec_helper"
|
24
|
+
|
25
|
+
describe "My awesome REST API", type: :rest_api
|
26
|
+
it "should do something awesome"
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
By default, all specs under the rest_apis directory will use Heisenberg, allowing you to ommit the type declaration.
|
31
|
+
|
32
|
+
To send messages to your api you can call any of these methods:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
get "/people"
|
36
|
+
post "/people", person: { name: "Chuck Norris" }
|
37
|
+
delete "/people/1"
|
38
|
+
put "/people/1", person: { name: "Jet Li" }
|
39
|
+
```
|
40
|
+
|
41
|
+
To get the response body from the last request can call the response_body method, which will return the response body parsed to a HashWithIndifferentAccess. For example:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
get "/people/1"
|
45
|
+
expect(response_body).to be_eql {person: {name: "Jet Li" }}
|
46
|
+
```
|
47
|
+
|
48
|
+
You can get the response status code from the last request and do expectations using Heisenberg's status code matchers, as follows:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
get "/people/1"
|
52
|
+
expect(response_status).to be_successful
|
53
|
+
```
|
54
|
+
|
55
|
+
Other status code matchers available are:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
expect(response_status).to be_successful
|
59
|
+
expect(response_status).to be_forbidden
|
60
|
+
expect(response_status).to be_bad_request
|
61
|
+
expect(response_status).to be_created
|
62
|
+
expect(response_status).to be_unauthorized
|
63
|
+
expect(response_status).to be_server_error
|
64
|
+
expect(response_status).to be_not_found
|
65
|
+
expect(response_status).to be_no_content
|
66
|
+
```
|
67
|
+
|
68
|
+
Contributing
|
69
|
+
------------
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
73
|
+
3. Commit your changes (git commit -am 'Added some feature')
|
74
|
+
4. Push to the branch (git push origin my-new-feature)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/heisenberg.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/heisenberg/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Marcello Milhomem Albuquerque"]
|
6
|
+
gem.email = ["marcello.m.albuquerque@gmail.com"]
|
7
|
+
gem.description = "Heisenberg is an REST API integration testing tool for rack based REST APIs"
|
8
|
+
gem.summary = "Heisenberg's goal is to make API integration tests more concise by allowing to spec APIs through a dead simple API"
|
9
|
+
gem.homepage = "https://github.com/marcelloma/heisenberg"
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.name = "heisenberg"
|
12
|
+
gem.require_paths = ["lib"]
|
13
|
+
gem.version = Heisenberg::VERSION
|
14
|
+
end
|
data/lib/heisenberg.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "rack/test"
|
2
|
+
|
3
|
+
require "heisenberg/hashext"
|
4
|
+
require "heisenberg/version"
|
5
|
+
require "heisenberg/api_integration_spec"
|
6
|
+
|
7
|
+
module Heisenberg
|
8
|
+
if defined? RSpec
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.include ApiIntegrationSpec,
|
11
|
+
example_group: { file_path: /spec\/rest_apis/ },
|
12
|
+
type: :rest_api
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec::Matchers.define :be_status_for do |expected|
|
16
|
+
match do |actual|
|
17
|
+
actual == case expected
|
18
|
+
when :success then 200
|
19
|
+
when :created then 201
|
20
|
+
when :no_content then 204
|
21
|
+
when :bad_request then 400
|
22
|
+
when :unauthorized then 401
|
23
|
+
when :forbidden then 403
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Heisenberg
|
2
|
+
module ApiIntegrationSpec
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
include Rack::Test::Methods
|
7
|
+
include Rails.application.routes.url_helpers
|
8
|
+
|
9
|
+
metadata[:type] = :rest_api
|
10
|
+
|
11
|
+
Rails.application.routes.default_url_options[:host] = Rack::Test::DEFAULT_HOST
|
12
|
+
|
13
|
+
def app
|
14
|
+
Rails.application
|
15
|
+
end
|
16
|
+
|
17
|
+
def last_response_json
|
18
|
+
parsed_json = JSON.parse(last_response.body)
|
19
|
+
parsed_json = parsed_json.recursive_symbolize_keys if parsed_json.is_a? Hash
|
20
|
+
parsed_json = parsed_json.map(&:recursive_symbolize_keys) if parsed_json.is_a? Array
|
21
|
+
parsed_json
|
22
|
+
end
|
23
|
+
|
24
|
+
def serialize resource, options = {}
|
25
|
+
if resource.is_a? Array
|
26
|
+
array_serializer = options.fetch(:serializer) { ActiveModel::ArraySerializer }
|
27
|
+
array_serializer.new(resource, options).as_json
|
28
|
+
else
|
29
|
+
serializer = options.fetch(:serializer) { resource.active_model_serializer }
|
30
|
+
serializer.new(resource).as_json
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
before :each do
|
35
|
+
header 'Accept', "application/json"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Hash
|
2
|
+
def recursive_symbolize_keys
|
3
|
+
new_hash = self.dup
|
4
|
+
new_hash.symbolize_keys!
|
5
|
+
new_hash.values.select{ |v| v.is_a?(Hash) }.each &:recursive_symbolize_keys!
|
6
|
+
new_hash.values.select{ |v| v.is_a?(Array) }.each { |h| h.each &:recursive_symbolize_keys! }
|
7
|
+
new_hash
|
8
|
+
end
|
9
|
+
|
10
|
+
def recursive_symbolize_keys!
|
11
|
+
symbolize_keys!
|
12
|
+
values.select{ |v| v.is_a?(Hash) }.each { |h| h.recursive_symbolize_keys! }
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heisenberg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcello Milhomem Albuquerque
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Heisenberg is an REST API integration testing tool for rack based REST
|
15
|
+
APIs
|
16
|
+
email:
|
17
|
+
- marcello.m.albuquerque@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- heisenberg.gemspec
|
28
|
+
- lib/heisenberg.rb
|
29
|
+
- lib/heisenberg/api_integration_spec.rb
|
30
|
+
- lib/heisenberg/hashext.rb
|
31
|
+
- lib/heisenberg/version.rb
|
32
|
+
homepage: https://github.com/marcelloma/heisenberg
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.23
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Heisenberg's goal is to make API integration tests more concise by allowing
|
56
|
+
to spec APIs through a dead simple API
|
57
|
+
test_files: []
|