json_matchers 0.6.2 → 0.6.3
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.
- checksums.yaml +4 -4
- data/NEWS.md +7 -0
- data/README.md +1 -1
- data/lib/json_matchers/matcher.rb +2 -9
- data/lib/json_matchers/payload.rb +23 -0
- data/lib/json_matchers/rspec.rb +7 -4
- data/lib/json_matchers/version.rb +1 -1
- data/spec/json_matchers/match_response_schema_spec.rb +23 -13
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa337859deb80396749bed9a9c4d32d7079a81ab
|
4
|
+
data.tar.gz: 3a8dc313f1854e2c1e41cd0bafa66855319bb06e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c22878991190e76a633c836dbdc45eb95cc5ce26664c13acf19579940653b4939ea67f212f81c12f2760198716b4347c71803e9135de943e952066cc1f75392f
|
7
|
+
data.tar.gz: 706d1bfd640fd52d8743a90cddc3bd2727736167c6dc4deed8137b95b5651bdc880134c43a27a089e367daa76de342aa508ed3ed19c72b4f28949043ab330e57
|
data/NEWS.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "json-schema"
|
2
|
+
require "json_matchers/payload"
|
2
3
|
|
3
4
|
module JsonMatchers
|
4
5
|
class Matcher
|
@@ -10,7 +11,7 @@ module JsonMatchers
|
|
10
11
|
def matches?(response)
|
11
12
|
JSON::Validator.validate!(
|
12
13
|
schema_path.to_s,
|
13
|
-
|
14
|
+
Payload.new(response).to_s,
|
14
15
|
options,
|
15
16
|
)
|
16
17
|
rescue JSON::Schema::ValidationError => ex
|
@@ -28,14 +29,6 @@ module JsonMatchers
|
|
28
29
|
|
29
30
|
attr_reader :schema_path, :options
|
30
31
|
|
31
|
-
def json_from(response)
|
32
|
-
if response.respond_to?(:body)
|
33
|
-
response.body
|
34
|
-
else
|
35
|
-
response
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
32
|
def default_options
|
40
33
|
JsonMatchers.configuration.options || {}
|
41
34
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module JsonMatchers
|
2
|
+
class Payload
|
3
|
+
def initialize(payload)
|
4
|
+
@payload = extract_json_string(payload)
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
payload
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
attr_reader :payload
|
14
|
+
|
15
|
+
def extract_json_string(payload)
|
16
|
+
if payload.respond_to?(:body)
|
17
|
+
payload.body
|
18
|
+
else
|
19
|
+
payload
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/json_matchers/rspec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "json_matchers"
|
2
|
+
require "json_matchers/payload"
|
2
3
|
|
3
4
|
module JsonMatchers
|
4
5
|
class RSpec < SimpleDelegator
|
@@ -18,7 +19,7 @@ module JsonMatchers
|
|
18
19
|
|
19
20
|
expected
|
20
21
|
|
21
|
-
#{pretty_json(response
|
22
|
+
#{pretty_json(response)}
|
22
23
|
|
23
24
|
to match schema "#{schema_name}":
|
24
25
|
|
@@ -35,7 +36,7 @@ to match schema "#{schema_name}":
|
|
35
36
|
|
36
37
|
expected
|
37
38
|
|
38
|
-
#{pretty_json(response
|
39
|
+
#{pretty_json(response)}
|
39
40
|
|
40
41
|
not to match schema "#{schema_name}":
|
41
42
|
|
@@ -46,8 +47,10 @@ not to match schema "#{schema_name}":
|
|
46
47
|
|
47
48
|
private
|
48
49
|
|
49
|
-
def pretty_json(
|
50
|
-
|
50
|
+
def pretty_json(response)
|
51
|
+
payload = Payload.new(response).to_s
|
52
|
+
|
53
|
+
JSON.pretty_generate(JSON.parse(payload))
|
51
54
|
end
|
52
55
|
|
53
56
|
def schema_path
|
@@ -22,20 +22,30 @@ describe JsonMatchers, "#match_response_schema" do
|
|
22
22
|
expect(response_for({})).not_to match_response_schema("foo_schema")
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
"
|
28
|
-
|
29
|
-
"
|
30
|
-
|
31
|
-
|
32
|
-
"
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
context "when JSON is a string" do
|
26
|
+
before(:each) do
|
27
|
+
create_schema("foo_schema", {
|
28
|
+
"type" => "object",
|
29
|
+
"required" => [
|
30
|
+
"id",
|
31
|
+
],
|
32
|
+
"properties" => {
|
33
|
+
"id" => { "type" => "number" },
|
34
|
+
},
|
35
|
+
"additionalProperties" => false,
|
36
|
+
})
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
+
it "validates when the schema matches" do
|
40
|
+
expect({ "id" => 1 }.to_json).
|
41
|
+
to match_response_schema("foo_schema")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "fails with message when negated" do
|
45
|
+
expect {
|
46
|
+
expect({ "id" => "1" }.to_json).to match_response_schema("foo_schema")
|
47
|
+
}.to raise_formatted_error(%{{ "type": "number" }})
|
48
|
+
end
|
39
49
|
end
|
40
50
|
|
41
51
|
it "fails when the body contains a property with the wrong type" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Doyle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- lib/json_matchers/configuration.rb
|
103
103
|
- lib/json_matchers/errors.rb
|
104
104
|
- lib/json_matchers/matcher.rb
|
105
|
+
- lib/json_matchers/payload.rb
|
105
106
|
- lib/json_matchers/rspec.rb
|
106
107
|
- lib/json_matchers/version.rb
|
107
108
|
- spec/json_matchers/match_response_schema_spec.rb
|