rspec-raml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5ae1aeda8f77496f297580603fd96c4e5154bf5
4
+ data.tar.gz: 8bc9b4a703c8279424066f96b495bb6bdfb31726
5
+ SHA512:
6
+ metadata.gz: 68c8ee7ef5fece8bc49284a9f09b0f1d5979eb5db9321640e7ffa887c74d94a641ce4804fec3905b79db441d04e41a7e99d5ae964a28151122874517fc6cdff1
7
+ data.tar.gz: c7cea7d5077cab65be77bbcf28879fcab012d764e52591c4598632de90c3ae3351dba6bc33cdcc25fe9d9a343504f8af5edd035b01ed03563765b96e79a7f3ed
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.8
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-raml.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ray Zane
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,95 @@
1
+ # Rspec::Raml
2
+
3
+ RSpec matchers for working with [RAML](http://raml.org).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rspec-raml'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rspec-raml
20
+
21
+ ## Setup
22
+
23
+ First, you'll need to include RSpec::Raml's helpers in your RSpec configuration.
24
+
25
+ ```ruby
26
+ RSpec.configure do |config|
27
+ config.include RSpec::Raml::Matchers, type: :request
28
+ end
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Assume you've got a RAML specification:
34
+
35
+ ```yaml
36
+ #%RAML 0.8
37
+ ---
38
+ title: Dummy API
39
+ baseUri: https://dummy-api.com/api/{version}
40
+ version: v1
41
+
42
+ /users:
43
+ /{id}:
44
+ displayName: Find a user
45
+ get:
46
+ responses:
47
+ 200:
48
+ description: User was found.
49
+ body:
50
+ application/json:
51
+ example: |-
52
+ {
53
+ "id": 1,
54
+ "first_name": "John",
55
+ "last_name": "Doe"
56
+ }
57
+
58
+ ```
59
+
60
+ You can write a spec that compares an HTTP response with the RAML specifcation you've written.
61
+
62
+ ```ruby
63
+ # spec/requests/api/v1/users_spec.rb
64
+
65
+ describe 'Users API' do
66
+ describe 'GET /api/v1/users/:id' do
67
+ raml { Rails.root.join('docs/api/v1.raml') }
68
+
69
+ let(:user) {
70
+ User.create!(
71
+ id: 1,
72
+ first_name: 'John',
73
+ last_name: 'Doe'
74
+ )
75
+ }
76
+
77
+ it 'is documented' do
78
+ get "/api/v1/users/#{user.id}"
79
+ expect(response).to match_raml(:get, '/users/{id}', 200)
80
+ end
81
+ end
82
+ end
83
+ ```
84
+
85
+ The `match_raml` matcher will verify that the status code returns successfully, and that the response body matches what you've declared in your RAML specification.
86
+
87
+ If you haven't written your specification yet, RSpec::Raml will output an example RAML specification.
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/rspec-raml.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,38 @@
1
+ require 'raml'
2
+
3
+ module RSpec
4
+ module Raml
5
+ class Finder
6
+ attr_reader :raml
7
+
8
+ def initialize(raml)
9
+ @raml = raml
10
+ end
11
+
12
+ # Recursively traverses the raml structure and locates all Raml::Resources
13
+ # with a matching url.
14
+ def find_resources(path, node = raml)
15
+ node.children.flat_map do |child|
16
+ if child.kind_of?(::Raml::Resource) && child.resource_path == path
17
+ child
18
+ else
19
+ find_resources(path, child)
20
+ end
21
+ end
22
+ end
23
+
24
+ # Finds the response that matches the verb, path, and status.
25
+ def find_response(verb, path, status)
26
+ resources = find_resources(path)
27
+
28
+ resource = resources.find do |node|
29
+ method = node.methods[verb.to_s]
30
+ method && method.responses[status]
31
+ end
32
+
33
+ http_method = resource && resource.methods[verb.to_s]
34
+ http_method.responses[status] if http_method
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,34 @@
1
+ require 'json'
2
+ require 'yaml'
3
+
4
+ module RSpec
5
+ module Raml
6
+ module Matchers
7
+ class Abstract
8
+ attr_reader :raml, :verb, :url, :status, :response
9
+
10
+ def initialize(raml, verb, url, status)
11
+ @raml = raml
12
+ @verb = verb
13
+ @url = url
14
+ @status = status
15
+ end
16
+
17
+ def matches?(response)
18
+ @response = response
19
+ matches_raml?
20
+ end
21
+
22
+ private
23
+
24
+ def content_type
25
+ response.content_type.to_s
26
+ end
27
+
28
+ def pretty_format(body)
29
+ JSON.pretty_generate(JSON.parse(body))
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec/raml/matchers/abstract'
2
+
3
+ module RSpec
4
+ module Raml
5
+ module Matchers
6
+ class BeRamlContentType < Abstract
7
+ def failure_message
8
+ "expected RAML to declare a response for content type: #{content_type}"
9
+ end
10
+
11
+ def failure_message_when_negated
12
+ "expected RAML not to declare content type: #{content_type}"
13
+ end
14
+
15
+ private
16
+
17
+ def matches_raml?
18
+ raml.bodies.key? content_type
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'rspec/raml/matchers/abstract'
2
+
3
+ module RSpec
4
+ module Raml
5
+ module Matchers
6
+ class MatchRaml < Abstract
7
+ delegate :failure_message, :failure_message_when_negated, to: :failure
8
+
9
+ private
10
+
11
+ def matches_raml?
12
+ failure.nil?
13
+ end
14
+
15
+ def failure
16
+ @failure ||= matchers.find { |m| !m.matches?(response) }
17
+ end
18
+
19
+ def matchers
20
+ (RSpec::Raml::Matchers::MATCHERS - [self.class]).map do |matcher|
21
+ matcher.new(raml, verb, url, status)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ require 'rspec/raml/matchers/abstract'
2
+
3
+ module RSpec
4
+ module Raml
5
+ module Matchers
6
+ class MatchRamlBody < Abstract
7
+ def failure_message
8
+ diff = differ.diff_as_string(response_body, raml_body)
9
+ "expected response bodies to match:#{diff}"
10
+ end
11
+
12
+ def failure_message_when_negated
13
+ "expected response bodies not to match"
14
+ end
15
+
16
+ private
17
+
18
+ def matches_raml?
19
+ response_body == raml_body
20
+ end
21
+
22
+ def response_body
23
+ @response_body ||= indent_pretty_format(response.body)
24
+ end
25
+
26
+ def raml_body
27
+ @raml_body ||= indent_pretty_format(raml.bodies.fetch(content_type).example)
28
+ end
29
+
30
+ def differ
31
+ @differ ||= RSpec::Support::Differ.new(color: true)
32
+ end
33
+
34
+ def indent_pretty_format(body)
35
+ pretty_format(body).gsub("\n", "\n ").prepend(' ')
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec/raml/matchers/abstract'
2
+
3
+ module RSpec
4
+ module Raml
5
+ module Matchers
6
+ class MatchRamlStatus < Abstract
7
+ def failure_message
8
+ "expected the response to have a #{raml.name} status code, but got #{response.status}"
9
+ end
10
+
11
+ def failure_message_when_negated
12
+ "expected the response not to have status: #{raml.name}"
13
+ end
14
+
15
+ private
16
+
17
+ def matches_raml?
18
+ response.status == raml.name
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'rspec/raml/matchers/abstract'
2
+
3
+ module RSpec
4
+ module Raml
5
+ module Matchers
6
+ class NullMatcher < Abstract
7
+ def initialize(verb, url, status)
8
+ super(nil, verb, url, status)
9
+ end
10
+
11
+ def failure_message
12
+ "RAML not found. Here's something to get you started:\n\n#{template}"
13
+ end
14
+
15
+ private
16
+
17
+ def matches_raml?
18
+ false
19
+ end
20
+
21
+ def template
22
+ {
23
+ url => {
24
+ displayName: 'PENDING NAME',
25
+ verb => {
26
+ responses: {
27
+ status => {
28
+ description: 'PENDING DESCRIPTION',
29
+ body: {
30
+ response.content_type => {
31
+ example: pretty_format(response.body)
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ }
38
+ }.deep_stringify_keys.to_yaml
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,57 @@
1
+ require 'raml'
2
+ require 'rspec/raml/finder'
3
+ require 'rspec/raml/matchers/null_matcher'
4
+ require 'rspec/raml/matchers/match_raml'
5
+ require 'rspec/raml/matchers/match_raml_body'
6
+ require 'rspec/raml/matchers/match_raml_status'
7
+ require 'rspec/raml/matchers/be_raml_content_type'
8
+
9
+ module RSpec
10
+ module Raml
11
+ module Matchers
12
+ MATCHERS = [
13
+ MatchRaml,
14
+ MatchRamlStatus,
15
+ BeRamlContentType,
16
+ MatchRamlBody
17
+ ]
18
+
19
+ def self.included(base)
20
+ base.extend ClassMethods
21
+ base.let(:_raml_finder) do
22
+ raise ArgumentError, <<-EOMSG.strip_heredoc
23
+ You need to specify a RAML specification file. Example:
24
+
25
+ raml { Rails.root.join('docs/api/v1.raml') }
26
+ EOMSG
27
+ end
28
+ end
29
+
30
+ module ClassMethods
31
+ def raml(&block)
32
+ let(:_raml_file, &block)
33
+ let(:_raml_finder) do
34
+ RSpec::Raml::Finder.new(::Raml.parse_file(_raml_file))
35
+ end
36
+ end
37
+ end
38
+
39
+ # Generate matcher methods:
40
+ #
41
+ # + def match_raml(verb, url, status)
42
+ # + def match_raml_status(verb, url, status)
43
+ # + def match_raml_body(verb, url, status)
44
+ # + def be_raml_content_type(verb, url, status)
45
+ #
46
+ MATCHERS.each do |matcher|
47
+ define_method matcher.name.split('::').last.underscore do |verb, url, status|
48
+ if raml = _raml_finder.find_response(verb, url, status)
49
+ matcher.new(raml, verb, url, status)
50
+ else
51
+ NullMatcher.new(verb, url, status)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module Raml
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/rspec/raml.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rspec/raml/version'
2
+ require 'rspec/raml/matchers'
3
+
4
+ module Rspec
5
+ module Raml
6
+ end
7
+ end
data/lib/rspec-raml.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rspec/raml'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec/raml/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rspec-raml'
8
+ spec.version = Rspec::Raml::VERSION
9
+ spec.authors = ['Ray Zane']
10
+ spec.email = ['raymondzane@gmail.com']
11
+
12
+ spec.summary = %q{RSpec matchers for working with RAML.}
13
+ spec.description = %q{RSpec matchers for working with RAML.}
14
+ spec.homepage = 'https://github.com/rzane/rspec-raml'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'raml_ruby'
23
+ spec.add_dependency 'rspec'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.12"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-raml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ray Zane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: raml_ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: RSpec matchers for working with RAML.
84
+ email:
85
+ - raymondzane@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/rspec-raml.rb
98
+ - lib/rspec/raml.rb
99
+ - lib/rspec/raml/finder.rb
100
+ - lib/rspec/raml/matchers.rb
101
+ - lib/rspec/raml/matchers/abstract.rb
102
+ - lib/rspec/raml/matchers/be_raml_content_type.rb
103
+ - lib/rspec/raml/matchers/match_raml.rb
104
+ - lib/rspec/raml/matchers/match_raml_body.rb
105
+ - lib/rspec/raml/matchers/match_raml_status.rb
106
+ - lib/rspec/raml/matchers/null_matcher.rb
107
+ - lib/rspec/raml/version.rb
108
+ - rspec-raml.gemspec
109
+ homepage: https://github.com/rzane/rspec-raml
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: RSpec matchers for working with RAML.
133
+ test_files: []