rspec_api_blueprint_matchers 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Dockerfile +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/codeship-services.yml +7 -0
- data/codeship-steps.yml +4 -0
- data/lib/rspec_api_blueprint_matchers.rb +1 -0
- data/lib/rspec_apib.rb +41 -0
- data/lib/rspec_apib/config.rb +17 -0
- data/lib/rspec_apib/elements.rb +20 -0
- data/lib/rspec_apib/elements/annotation.rb +7 -0
- data/lib/rspec_apib/elements/array.rb +9 -0
- data/lib/rspec_apib/elements/asset.rb +7 -0
- data/lib/rspec_apib/elements/base.rb +97 -0
- data/lib/rspec_apib/elements/category.rb +7 -0
- data/lib/rspec_apib/elements/copy.rb +7 -0
- data/lib/rspec_apib/elements/data_structure.rb +7 -0
- data/lib/rspec_apib/elements/href_variables.rb +13 -0
- data/lib/rspec_apib/elements/http_headers.rb +27 -0
- data/lib/rspec_apib/elements/http_message_payload.rb +45 -0
- data/lib/rspec_apib/elements/http_request.rb +59 -0
- data/lib/rspec_apib/elements/http_response.rb +35 -0
- data/lib/rspec_apib/elements/http_transaction.rb +58 -0
- data/lib/rspec_apib/elements/member.rb +21 -0
- data/lib/rspec_apib/elements/object.rb +7 -0
- data/lib/rspec_apib/elements/parse_result.rb +7 -0
- data/lib/rspec_apib/elements/resource.rb +24 -0
- data/lib/rspec_apib/elements/source_map.rb +7 -0
- data/lib/rspec_apib/elements/string.rb +9 -0
- data/lib/rspec_apib/elements/templated_href.rb +43 -0
- data/lib/rspec_apib/elements/transition.rb +22 -0
- data/lib/rspec_apib/engine.rb +0 -0
- data/lib/rspec_apib/extractors.rb +2 -0
- data/lib/rspec_apib/extractors/http_transaction.rb +23 -0
- data/lib/rspec_apib/extractors/resource.rb +23 -0
- data/lib/rspec_apib/parser.rb +79 -0
- data/lib/rspec_apib/request.rb +44 -0
- data/lib/rspec_apib/response.rb +39 -0
- data/lib/rspec_apib/rspec.rb +40 -0
- data/lib/rspec_apib/transaction_coverage_report.rb +36 -0
- data/lib/rspec_apib/transaction_coverage_validator.rb +49 -0
- data/lib/rspec_apib/transaction_validator.rb +44 -0
- data/lib/rspec_apib/transcluder.rb +30 -0
- data/lib/rspec_apib/version.rb +3 -0
- data/lib/transcluder.rb +3 -0
- data/rspec_api_blueprint_matchers.gemspec +29 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3beafbdb89aaed312b890081521e50c6973475eb
|
4
|
+
data.tar.gz: 512c3f1a07442117d84aadd3cd6e25ba03f12e81
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd113aa0f4dcdf19a1760bd9ed0d5f48db11f993ae56c53b6e8e06e3427df2900ed9f03003eeb23979f4c2870a11ffc621bbd8e5ad59fc395ddd9f4ca8629c90
|
7
|
+
data.tar.gz: 8c496cef8d847dd9e7e3155ea53da91735556c6d99ff2a8d74d4327297894e042531236c03d0cc9b806cd2fe3b48869d73232117621953125b9b5342e7ef481f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Dockerfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
FROM ruby:2.4.1-slim
|
2
|
+
LABEL maintainer "team@shiftcommerce.com"
|
3
|
+
|
4
|
+
# Install essentials and cURL
|
5
|
+
RUN apt-get update -qq && apt-get install -y --no-install-recommends build-essential curl git libpq-dev python
|
6
|
+
|
7
|
+
# Install drafter for API Blueprint Parsing
|
8
|
+
RUN mkdir /tmp_build
|
9
|
+
RUN cd /tmp_build
|
10
|
+
RUN bash -c "git clone --recursive git://github.com/apiaryio/drafter.git; cd drafter; ./configure; make test; make drafter; make install"
|
11
|
+
RUN rm -rf /tmp_build
|
12
|
+
|
13
|
+
|
14
|
+
# Configure the main working directory
|
15
|
+
ENV app /app
|
16
|
+
RUN mkdir $app
|
17
|
+
WORKDIR $app
|
18
|
+
|
19
|
+
# Set the where to install gems
|
20
|
+
ENV GEM_HOME /rubygems
|
21
|
+
ENV BUNDLE_PATH /rubygems
|
22
|
+
|
23
|
+
# Link the whole application up
|
24
|
+
ADD . $app
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Shift Commerce Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# RspecApib
|
2
|
+
|
3
|
+
This gem allows your rspec test suite to validate requests and responses against
|
4
|
+
your api blueprint documentation.
|
5
|
+
|
6
|
+
It also allows for validating that all documented http transactions are
|
7
|
+
covered in your test suite. (Care should be taken with parallel specs if using this)
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
This gem uses the standard api blueprint parser called "drafter" which must be
|
13
|
+
installed before using this gem.
|
14
|
+
|
15
|
+
To install it, please refer to https://github.com/apiaryio/drafter#build
|
16
|
+
|
17
|
+
Once drafter is installed, please do the following :-
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'rspec_apib', group: :test
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install rspec_apib
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
For the rest of the documentation, we will refer to "request" and "response". These
|
36
|
+
can be a faraday request / response environment, or a rack request / response environment
|
37
|
+
or a generic request / response from a rails integration test suite.
|
38
|
+
|
39
|
+
### Validating a http transaction
|
40
|
+
|
41
|
+
A transaction is a struct containing "request" and "response" where request and
|
42
|
+
response are typical requests and responses from rack / faraday etc..
|
43
|
+
At present, we are using this gem for faraday though.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
expect(transaction).to match_api_docs_for(path: "/blogs", request_method: :post, content_type: "application/json")
|
47
|
+
```
|
48
|
+
|
49
|
+
#### How This Works
|
50
|
+
|
51
|
+
How this works is the request is repeatedly matched against all transactions
|
52
|
+
(request / response pairs) in the api blueprint document. This is done in
|
53
|
+
2 phases - first a 'short list', then a detailed match on this short list.
|
54
|
+
This is to prevent expensive schema matching when even the basics don't match.
|
55
|
+
The short list is then further filtered by what the developer asked for in the
|
56
|
+
'match_api_docs_for' matcher.
|
57
|
+
|
58
|
+
The matcher also allows the symbol :any to mean "Don't care what it is" but use
|
59
|
+
with caution.
|
60
|
+
|
61
|
+
The same is then done for the response and any transactions that match both
|
62
|
+
are considered a good match.
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/shiftcommerce/rspec_api_blueprint_matchers
|
73
|
+
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rspec_apib"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/codeship-steps.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rspec_apib"
|
data/lib/rspec_apib.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "rspec_apib/version"
|
2
|
+
require "rspec_apib/parser"
|
3
|
+
require "rspec_apib/extractors"
|
4
|
+
require "rspec_apib/transaction_validator"
|
5
|
+
require "rspec_apib/rspec"
|
6
|
+
require "rspec_apib/config"
|
7
|
+
require "rspec_apib/request"
|
8
|
+
require "rspec_apib/response"
|
9
|
+
module RSpecApib
|
10
|
+
ROOT_THREAD_VARS = :"rspec_apib"
|
11
|
+
# The global configuration object
|
12
|
+
# If a block is passed into this method, it is yielded with
|
13
|
+
# the config object and all actions are performed from within the
|
14
|
+
# block as a batch - any action(s) that then need performing after
|
15
|
+
# a reconfigure are done once only.
|
16
|
+
def self.config
|
17
|
+
return config_instance unless block_given?
|
18
|
+
config_instance.batch_configure do |config|
|
19
|
+
yield(config)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.normalize_request(request)
|
24
|
+
Request.new request
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.normalize_response(response)
|
28
|
+
Response.new response
|
29
|
+
end
|
30
|
+
|
31
|
+
# Global storage per thread for the gems to use where required.
|
32
|
+
# @return [Hash] A hash which the caller is free to modify at will
|
33
|
+
def self.root_thread_vars
|
34
|
+
Thread.current[ROOT_THREAD_VARS] ||= {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.config_instance
|
38
|
+
root_thread_vars[:config_instance] ||= RSpecApib::Config.new
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RSpecApib
|
2
|
+
class Config
|
3
|
+
attr_accessor :default_blueprint_file
|
4
|
+
def batch_configure
|
5
|
+
yield self
|
6
|
+
end
|
7
|
+
|
8
|
+
# The default Parser
|
9
|
+
# If a single blueprint file is used throughout specs then by not specifying
|
10
|
+
# your own parser in your examples, this default will be used which has pre
|
11
|
+
# parsed the default blueprint file
|
12
|
+
# @return [::RSpecApib::Parser] The parser
|
13
|
+
def default_parser
|
14
|
+
@default_parser ||= Parser.new.tap { |p| p.parse_file(default_blueprint_file) }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "rspec_apib/elements/base"
|
2
|
+
require "rspec_apib/elements/parse_result"
|
3
|
+
require "rspec_apib/elements/category"
|
4
|
+
require "rspec_apib/elements/copy"
|
5
|
+
require "rspec_apib/elements/resource"
|
6
|
+
require "rspec_apib/elements/transition"
|
7
|
+
require "rspec_apib/elements/http_transaction"
|
8
|
+
require "rspec_apib/elements/http_request"
|
9
|
+
require "rspec_apib/elements/http_response"
|
10
|
+
require "rspec_apib/elements/http_headers"
|
11
|
+
require "rspec_apib/elements/asset"
|
12
|
+
require "rspec_apib/elements/data_structure"
|
13
|
+
require "rspec_apib/elements/object"
|
14
|
+
require "rspec_apib/elements/member"
|
15
|
+
require "rspec_apib/elements/annotation"
|
16
|
+
require "rspec_apib/elements/templated_href"
|
17
|
+
require "rspec_apib/elements/href_variables"
|
18
|
+
require "rspec_apib/elements/source_map"
|
19
|
+
require "rspec_apib/elements/string"
|
20
|
+
require "rspec_apib/elements/array"
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module RSpecApib
|
2
|
+
module Element
|
3
|
+
class Base < Struct.new(:element, :meta, :attributes, :content, :parent)
|
4
|
+
|
5
|
+
def inspect(*args)
|
6
|
+
"##{self.class} (element: #{element}, meta: #{meta}, attributes: #{attributes.inspect}, content: #{content.inspect}, parent: ##{parent.class})"
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.parse_root(root)
|
10
|
+
index = {}
|
11
|
+
result = parse(root, index: index, parent: nil)
|
12
|
+
[result, index]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.parse(node_or_nodes, index:, parent:, klass: nil)
|
16
|
+
return node_or_nodes.map { |node| parse(node, index: index, parent: parent) } if node_or_nodes.is_a?(::Array)
|
17
|
+
return transformed_basic_hash(node_or_nodes, index: index, parent: parent) if is_basic_hash?(node_or_nodes)
|
18
|
+
return node_or_nodes unless !klass.nil? || is_base_element?(node_or_nodes)
|
19
|
+
hash = node_or_nodes
|
20
|
+
klass_name = klass
|
21
|
+
klass_name ||= hash["element"].slice(0,1).capitalize + hash["element"].slice(1..-1).gsub(" ", "")
|
22
|
+
return node_or_nodes unless RSpecApib::Element.const_defined?(klass_name)
|
23
|
+
klass = RSpecApib::Element.const_get(klass_name)
|
24
|
+
index[klass] ||= []
|
25
|
+
element = klass.from_hash(hash, index: index, parent: parent)
|
26
|
+
index[klass] << element
|
27
|
+
element
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.from_hash(hash, index:, parent:)
|
31
|
+
attrs = parse_attributes(hash["attributes"], index: index, parent: parent)
|
32
|
+
child = new(hash["element"], hash["meta"], attrs, nil, parent)
|
33
|
+
child.content = parse(hash["content"], index: index, parent: child)
|
34
|
+
inherit_attrs_from_ancestors(child)
|
35
|
+
child
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.inherit_attrs_from_ancestors(child)
|
39
|
+
child.attributes ||= {}
|
40
|
+
attrs = child.attributes
|
41
|
+
attrs_to_inherit.each do |attr|
|
42
|
+
attr_as_str = attr.to_s
|
43
|
+
unless attrs.key?(attr_as_str)
|
44
|
+
val = ancestor_attr(child, attr_as_str)
|
45
|
+
attrs[attr_as_str] = val unless val.nil?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.ancestor_attr(node, attr)
|
51
|
+
return nil if node.nil?
|
52
|
+
return node.attributes[attr] if node.attributes && node.attributes.key?(attr)
|
53
|
+
ancestor_attr(node.parent, attr)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.attrs_to_inherit
|
57
|
+
[]
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.attr_readers
|
61
|
+
{}
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.is_base_element?(node)
|
65
|
+
node.is_a?(::Hash) && node.keys.include?("element")
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def self.attributes_schema
|
71
|
+
{}
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.parse_attributes(node, index:, parent:)
|
75
|
+
return node if node.nil?
|
76
|
+
node = node.dup
|
77
|
+
node.keys.each do |key|
|
78
|
+
klass = attributes_schema[key.to_sym]
|
79
|
+
node[key] = parse(node[key], index: index, parent: parent, klass: klass)
|
80
|
+
end
|
81
|
+
node
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.is_basic_hash?(node)
|
85
|
+
node.is_a?(::Hash) && !is_base_element?(node)
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.transformed_basic_hash(node, index:, parent:)
|
89
|
+
node = node.dup
|
90
|
+
node.keys.each do |key|
|
91
|
+
node[key] = parse(node[key], index: index, parent: parent)
|
92
|
+
end
|
93
|
+
node
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|