api-validator 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 +17 -0
- data/Gemfile +6 -0
- data/LICENSE +27 -0
- data/README.md +80 -0
- data/Rakefile +8 -0
- data/api-validator.gemspec +27 -0
- data/lib/api-validator.rb +19 -0
- data/lib/api-validator/assertion.rb +32 -0
- data/lib/api-validator/base.rb +47 -0
- data/lib/api-validator/header.rb +77 -0
- data/lib/api-validator/json.rb +56 -0
- data/lib/api-validator/json_schema.rb +228 -0
- data/lib/api-validator/json_schemas.rb +20 -0
- data/lib/api-validator/mixins.rb +7 -0
- data/lib/api-validator/mixins/deep_merge.rb +28 -0
- data/lib/api-validator/response_expectation.rb +90 -0
- data/lib/api-validator/response_expectation/results.rb +62 -0
- data/lib/api-validator/spec.rb +153 -0
- data/lib/api-validator/spec/results.rb +28 -0
- data/lib/api-validator/status.rb +37 -0
- data/lib/api-validator/version.rb +3 -0
- data/spec/header_validator_spec.rb +108 -0
- data/spec/json_schema_validator_spec.rb +463 -0
- data/spec/json_validator_spec.rb +174 -0
- data/spec/response_expectation_results_spec.rb +105 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/spec_results_spec.rb +52 -0
- data/spec/spec_spec.rb +196 -0
- data/spec/status_validator_spec.rb +46 -0
- data/spec/support/hash_wrapper.rb +50 -0
- data/spec/support/shared_examples/shared_example_declaration.rb +8 -0
- data/spec/support/shared_examples/shared_example_lookup.rb +5 -0
- data/spec/support/shared_examples/validation_declaration.rb +65 -0
- data/spec/support/validator_shared_examples.rb +21 -0
- metadata +188 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
require 'support/validator_shared_examples'
|
5
|
+
|
6
|
+
describe ApiValidator::Status do
|
7
|
+
let(:env) { HashWrapper.new(:status => 200, :response_headers => {}, :body => '') }
|
8
|
+
let(:response) { Faraday::Response.new(env) }
|
9
|
+
let(:validator) { stub(:everything) }
|
10
|
+
let(:instance) { described_class.new(expected_status) }
|
11
|
+
let(:expectation_key) { :response_status }
|
12
|
+
|
13
|
+
let(:res) { instance.validate(response) }
|
14
|
+
|
15
|
+
describe "#validate" do
|
16
|
+
let(:expected_status) { 304 }
|
17
|
+
|
18
|
+
let(:expected_assertions) do
|
19
|
+
[
|
20
|
+
{ :op => "test", :path => "", :value => 304 }
|
21
|
+
]
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when expectation fails" do
|
25
|
+
it_behaves_like "a validator #validate method"
|
26
|
+
|
27
|
+
before do
|
28
|
+
env.status = 400
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:expected_diff) { [{ :op => "replace", :path => "", :value => 304, :current_value => 400 }] }
|
32
|
+
let(:expected_failed_assertions) { [expected_assertions.first] }
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when expectation passes" do
|
36
|
+
it_behaves_like "a validator #validate method"
|
37
|
+
|
38
|
+
before do
|
39
|
+
env.status = 304
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:expected_diff) { [] }
|
43
|
+
let(:expected_failed_assertions) { [] }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class HashWrapper
|
2
|
+
attr_reader :hash
|
3
|
+
def initialize(hash)
|
4
|
+
hash.default_proc = lambda { |hash, key|
|
5
|
+
key = (Symbol === key) ? key.to_s : key.to_sym
|
6
|
+
hash.has_key?(key) ? hash[key] : nil
|
7
|
+
}
|
8
|
+
@hash = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_key?(key)
|
12
|
+
hash.has_key?(key.to_s) || hash.has_key?(key.to_sym)
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](key)
|
16
|
+
hash[key]
|
17
|
+
end
|
18
|
+
|
19
|
+
def []=(key, val)
|
20
|
+
hash[key.to_sym] = val
|
21
|
+
end
|
22
|
+
|
23
|
+
def ==(other)
|
24
|
+
hash == other
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_hash(options = {})
|
28
|
+
hash
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to_missing?(method)
|
32
|
+
has_key?(method) || method =~ /=\Z/ || hash.respond_to?(method)
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(method, *args, &block)
|
36
|
+
if respond_to_missing?(method)
|
37
|
+
if has_key?(method)
|
38
|
+
val = self.send(:[], method, *args, &block)
|
39
|
+
val = self.class.new(val) if Hash === val
|
40
|
+
val
|
41
|
+
elsif method =~ /=\Z/
|
42
|
+
self.send(:[]=, method.to_s.sub(/=\Z/, ''), *args, &block)
|
43
|
+
else
|
44
|
+
hash.send(method, *args, &block)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
shared_examples "a validation declaration" do
|
2
|
+
it "sets name" do
|
3
|
+
name = "some validation"
|
4
|
+
options = Hash.new
|
5
|
+
validation = instance.send(method_name, name, options)
|
6
|
+
expect(validation.name).to eql(name)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "references parent" do
|
10
|
+
options = Hash.new
|
11
|
+
validation = instance.send(method_name, "some validation", options)
|
12
|
+
expect(validation.parent).to eql(parent)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "gets appended to list of validations" do
|
16
|
+
options = Hash.new
|
17
|
+
validation = instance.send(method_name, "some validation", options)
|
18
|
+
expect(instance.validations.last).to eql(validation)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "calls block in scope of validation" do
|
22
|
+
ref = nil
|
23
|
+
options, block = Hash.new, proc { ref = self }
|
24
|
+
validation = instance.send(method_name, "some validation with block", options, &block)
|
25
|
+
expect(ref).to eql(validation)
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when no block given" do
|
29
|
+
it "sets pending flag" do
|
30
|
+
options = {}
|
31
|
+
validation = instance.send(method_name, "some validation without block", options)
|
32
|
+
expect(validation.pending).to be_true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when before hook specified" do
|
37
|
+
context "when before hook is a method name" do
|
38
|
+
it "appends method reference to before hooks list" do
|
39
|
+
before_hook_method_name = :some_random_method
|
40
|
+
|
41
|
+
described_class.class_eval do
|
42
|
+
define_method before_hook_method_name do
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
options = { :before => before_hook_method_name }
|
47
|
+
validation = instance.send(method_name, "some validation with before hook", options)
|
48
|
+
|
49
|
+
expect(validation.before_hooks.last.name).to eql(before_hook_method_name)
|
50
|
+
expect(validation.before_hooks.last).to respond_to(:call)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when before hook is a block" do
|
55
|
+
it "appends block to before hooks list" do
|
56
|
+
before_hook = lambda {}
|
57
|
+
|
58
|
+
options = { :before => before_hook }
|
59
|
+
validation = instance.send(method_name, "some validation with before hook", options)
|
60
|
+
|
61
|
+
expect(validation.before_hooks.last).to eql(before_hook)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
shared_examples "a validator #validate method" do
|
2
|
+
it "sets expectation key" do
|
3
|
+
expect(res[:key]).to eql(expectation_key)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "sets assertions" do
|
7
|
+
expect(res[:assertions].to_a.sort_by { |h| h[:path] }).to eql(expected_assertions.sort_by { |h| h[:path] })
|
8
|
+
end
|
9
|
+
|
10
|
+
it "sets diff" do
|
11
|
+
expect(res[:diff]).to eql(expected_diff)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets failed assertions" do
|
15
|
+
expect(res[:failed_assertions]).to eql(expected_failed_assertions)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets valid flag" do
|
19
|
+
expect(res[:valid]).to eql(expected_diff.empty?)
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api-validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jesse Stuart
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json-pointer
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.11'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.11'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mocha
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.13'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.13'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.3'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: faraday
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: ! ' '
|
111
|
+
email:
|
112
|
+
- jesse@jessestuart.ca
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- Gemfile
|
119
|
+
- LICENSE
|
120
|
+
- README.md
|
121
|
+
- Rakefile
|
122
|
+
- api-validator.gemspec
|
123
|
+
- lib/api-validator.rb
|
124
|
+
- lib/api-validator/assertion.rb
|
125
|
+
- lib/api-validator/base.rb
|
126
|
+
- lib/api-validator/header.rb
|
127
|
+
- lib/api-validator/json.rb
|
128
|
+
- lib/api-validator/json_schema.rb
|
129
|
+
- lib/api-validator/json_schemas.rb
|
130
|
+
- lib/api-validator/mixins.rb
|
131
|
+
- lib/api-validator/mixins/deep_merge.rb
|
132
|
+
- lib/api-validator/response_expectation.rb
|
133
|
+
- lib/api-validator/response_expectation/results.rb
|
134
|
+
- lib/api-validator/spec.rb
|
135
|
+
- lib/api-validator/spec/results.rb
|
136
|
+
- lib/api-validator/status.rb
|
137
|
+
- lib/api-validator/version.rb
|
138
|
+
- spec/header_validator_spec.rb
|
139
|
+
- spec/json_schema_validator_spec.rb
|
140
|
+
- spec/json_validator_spec.rb
|
141
|
+
- spec/response_expectation_results_spec.rb
|
142
|
+
- spec/spec_helper.rb
|
143
|
+
- spec/spec_results_spec.rb
|
144
|
+
- spec/spec_spec.rb
|
145
|
+
- spec/status_validator_spec.rb
|
146
|
+
- spec/support/hash_wrapper.rb
|
147
|
+
- spec/support/shared_examples/shared_example_declaration.rb
|
148
|
+
- spec/support/shared_examples/shared_example_lookup.rb
|
149
|
+
- spec/support/shared_examples/validation_declaration.rb
|
150
|
+
- spec/support/validator_shared_examples.rb
|
151
|
+
homepage: ''
|
152
|
+
licenses: []
|
153
|
+
post_install_message:
|
154
|
+
rdoc_options: []
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ! '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
none: false
|
165
|
+
requirements:
|
166
|
+
- - ! '>='
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 1.8.23
|
172
|
+
signing_key:
|
173
|
+
specification_version: 3
|
174
|
+
summary: ''
|
175
|
+
test_files:
|
176
|
+
- spec/header_validator_spec.rb
|
177
|
+
- spec/json_schema_validator_spec.rb
|
178
|
+
- spec/json_validator_spec.rb
|
179
|
+
- spec/response_expectation_results_spec.rb
|
180
|
+
- spec/spec_helper.rb
|
181
|
+
- spec/spec_results_spec.rb
|
182
|
+
- spec/spec_spec.rb
|
183
|
+
- spec/status_validator_spec.rb
|
184
|
+
- spec/support/hash_wrapper.rb
|
185
|
+
- spec/support/shared_examples/shared_example_declaration.rb
|
186
|
+
- spec/support/shared_examples/shared_example_lookup.rb
|
187
|
+
- spec/support/shared_examples/validation_declaration.rb
|
188
|
+
- spec/support/validator_shared_examples.rb
|