hal_spec 0.0.1 → 1.0.0
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/features/equivalence.feature +22 -3
- data/features/step_definitions/steps.rb +1 -1
- data/hal_spec.gemspec +1 -0
- data/lib/hal_spec/cucumber.rb +6 -8
- data/lib/hal_spec/matchers/be_valid_hal.rb +3 -4
- data/lib/hal_spec/matchers/have_a_valid_hal_content_type.rb +4 -4
- data/lib/hal_spec/matchers.rb +4 -4
- data/lib/hal_spec/version.rb +1 -1
- data/lib/hal_spec.rb +1 -0
- data/spec/hal_spec/matchers/be_valid_hal_spec.rb +22 -29
- data/spec/hal_spec/matchers/have_a_valid_hal_content_type_spec.rb +5 -5
- metadata +17 -1
@@ -1,11 +1,30 @@
|
|
1
1
|
Feature: Equivalence
|
2
|
-
|
2
|
+
Background:
|
3
3
|
Given the HAL is:
|
4
4
|
"""
|
5
|
-
|
5
|
+
{
|
6
|
+
"_links": {"self": {"name":"namen", "href": "/lol"}}
|
7
|
+
,"_embedded": {"users": [], "pages": []}, "id": 666
|
8
|
+
}
|
6
9
|
"""
|
10
|
+
|
11
|
+
Scenario: Complete HAL
|
7
12
|
When I get the HAL
|
8
13
|
Then the HAL response should be:
|
9
14
|
"""
|
10
|
-
|
15
|
+
{
|
16
|
+
"id": 666
|
17
|
+
/*it has embedded things!*/
|
18
|
+
,"_embedded": {
|
19
|
+
"pages": []
|
20
|
+
,"users": []
|
21
|
+
}
|
22
|
+
,"_links": {
|
23
|
+
"self": {
|
24
|
+
/*lol is funny*/
|
25
|
+
"href": "/lol"
|
26
|
+
,"name": "namen"
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
11
30
|
"""
|
data/hal_spec.gemspec
CHANGED
data/lib/hal_spec/cucumber.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
require File.expand_path("../../hal_spec", __FILE__)
|
2
|
-
World(HalSpec::Matchers)
|
2
|
+
World(HalSpec::Matchers, JsonSpec::Matchers)
|
3
3
|
|
4
|
-
Then /the HAL response should be:/ do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
last_response.should have_a_valid_hal_content_type
|
10
|
-
last_response.should be_valid_hal
|
4
|
+
Then /the HAL response should be:/ do |expected_hal|
|
5
|
+
expected_hal.gsub(/\s+/, " ").should be_valid_hal
|
6
|
+
last_response.body.should be_json_eql(expected_hal)
|
7
|
+
last_response.headers.should have_a_valid_hal_content_type
|
8
|
+
last_response.body.should be_valid_hal
|
11
9
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
module HalSpec
|
2
2
|
module Matchers
|
3
3
|
module BeValidHal
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
return Halidator.new(response.body).valid?
|
4
|
+
def is_valid_hal?(str)
|
5
|
+
JSON.parse(str) rescue return false
|
6
|
+
return Halidator.new(str).valid?
|
8
7
|
end
|
9
8
|
end
|
10
9
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module HalSpec
|
2
2
|
module Matchers
|
3
3
|
module HaveAValidHalContentType
|
4
|
-
def has_valid_content_type?(
|
5
|
-
|
6
|
-
|
4
|
+
def has_valid_content_type?(headers)
|
5
|
+
return false if headers.nil?
|
6
|
+
return false unless headers.include?("Content-Type")
|
7
7
|
%w(
|
8
8
|
application/vnd.hal+json
|
9
9
|
application/hal+json
|
10
|
-
).include?(
|
10
|
+
).include?(headers["Content-Type"])
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
data/lib/hal_spec/matchers.rb
CHANGED
@@ -8,14 +8,14 @@ module HalSpec
|
|
8
8
|
include HalSpec::Matchers::BeValidHal
|
9
9
|
|
10
10
|
matcher :have_a_valid_hal_content_type do
|
11
|
-
match do |
|
12
|
-
has_valid_content_type?(
|
11
|
+
match do |headers|
|
12
|
+
has_valid_content_type?(headers)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
matcher :be_valid_hal do
|
17
|
-
match do |
|
18
|
-
|
17
|
+
match do |body|
|
18
|
+
is_valid_hal?(body)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/hal_spec/version.rb
CHANGED
data/lib/hal_spec.rb
CHANGED
@@ -1,82 +1,75 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'ostruct'
|
3
2
|
require 'json'
|
4
3
|
|
5
|
-
def
|
6
|
-
|
4
|
+
def dump(body)
|
5
|
+
JSON.dump(body)
|
7
6
|
end
|
8
7
|
|
9
8
|
describe HalSpec::Matchers::BeValidHal do
|
10
9
|
let(:minimal_hal){ {_links: {self: {href: "/some/url"}}} }
|
11
10
|
|
12
|
-
context "
|
13
|
-
it "should match
|
14
|
-
|
15
|
-
body: '/*this is hal*/{"_links": /*documents are fun!*/{"self": {"href": "/url"}}}'
|
16
|
-
).should be_valid_hal
|
11
|
+
context "string prerequisites" do
|
12
|
+
it "should match json with comments" do
|
13
|
+
'/*this is hal*/{"_links": /*documents are fun!*/{"self": {"href": "/url"}}}'.should be_valid_hal
|
17
14
|
end
|
18
15
|
|
19
16
|
it "should not match responses without bodies" do
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should not match responses without json bodies" do
|
25
|
-
OpenStruct.new(body: "Yo dawg").should_not be_valid_hal
|
17
|
+
"li".should_not be_valid_hal
|
18
|
+
nil.should_not be_valid_hal
|
26
19
|
end
|
27
20
|
end
|
28
21
|
|
29
22
|
context "link validation" do
|
30
23
|
it "should match a document with only links" do
|
31
|
-
|
24
|
+
dump(minimal_hal).should be_valid_hal
|
32
25
|
end
|
33
26
|
|
34
27
|
it "should match a document with valid links other than self" do
|
35
28
|
json = minimal_hal.dup
|
36
29
|
json[:_links].merge!({search: {href: "/search/{q}", templated: true}})
|
37
|
-
|
30
|
+
dump(json).should be_valid_hal
|
38
31
|
end
|
39
32
|
|
40
33
|
it "should match a document with valid links in arrays" do
|
41
34
|
json = minimal_hal.dup
|
42
35
|
json[:_links].merge!({users: [{href: "/users/42"}]})
|
43
|
-
|
36
|
+
dump(json).should be_valid_hal
|
44
37
|
end
|
45
38
|
|
46
39
|
it "should match a document with templated links in arrays" do
|
47
40
|
json = minimal_hal.dup
|
48
41
|
json[:_links].merge!({sortings: [{href: "/sort/{dir}", templated: true}]})
|
49
|
-
|
42
|
+
dump(json).should be_valid_hal
|
50
43
|
end
|
51
44
|
|
52
45
|
it "should not match a document without links" do
|
53
|
-
|
46
|
+
dump({no_links: {self: {href: "/nope"}}}).should_not be_valid_hal
|
54
47
|
end
|
55
48
|
|
56
49
|
it "should not match a document without the self link" do
|
57
|
-
|
50
|
+
dump({_links: {lel: {href: "/pah"}}}).should_not be_valid_hal
|
58
51
|
end
|
59
52
|
|
60
53
|
it "should not match a document with invalid link values for self" do
|
61
|
-
|
54
|
+
dump({_links: {self: {breh: "/brah"}}}).should_not be_valid_hal
|
62
55
|
end
|
63
56
|
|
64
57
|
it "should not match a document with invalid links for other rels" do
|
65
58
|
json = minimal_hal.dup
|
66
59
|
json[:_links].merge!({search: "/search/{q}", templated: true})
|
67
|
-
|
60
|
+
dump(json).should_not be_valid_hal
|
68
61
|
end
|
69
62
|
|
70
63
|
it "should not match a document with too many self rels" do
|
71
64
|
json = minimal_hal.dup
|
72
65
|
json[:_links].merge!({users: [{self: {href: "/users/4"}},{self: {href: "/users/2"}}]})
|
73
|
-
|
66
|
+
dump(json).should_not be_valid_hal
|
74
67
|
end
|
75
68
|
|
76
69
|
it "should not match a document with no href key in arrays" do
|
77
70
|
json = minimal_hal.dup
|
78
71
|
json[:_links].merge!({users: [{bref: "/lel"}]})
|
79
|
-
|
72
|
+
dump(json).should_not be_valid_hal
|
80
73
|
end
|
81
74
|
|
82
75
|
it "should not match a document with invalid templates in arrays" do
|
@@ -91,15 +84,15 @@ describe HalSpec::Matchers::BeValidHal do
|
|
91
84
|
|
92
85
|
context "resource validation" do
|
93
86
|
it "should match a document with embedded resources as an object" do
|
94
|
-
|
87
|
+
dump({_embedded: {resources: []}}.merge(minimal_hal)).should be_valid_hal
|
95
88
|
end
|
96
89
|
|
97
90
|
it "should match a document with valid embedded resources" do
|
98
|
-
|
91
|
+
dump({_embedded: {resources: 2.times.collect{minimal_hal}}}.merge(minimal_hal)).should be_valid_hal
|
99
92
|
end
|
100
93
|
|
101
94
|
it "should match a document with non-array valid embedded resources" do
|
102
|
-
|
95
|
+
dump({
|
103
96
|
_embedded: {user: minimal_hal}
|
104
97
|
}.merge(minimal_hal)).should be_valid_hal
|
105
98
|
end
|
@@ -111,11 +104,11 @@ describe HalSpec::Matchers::BeValidHal do
|
|
111
104
|
user: [{_embedded: {stuff: [valid_hal]}}.merge(valid_hal)]
|
112
105
|
}
|
113
106
|
}.merge(minimal_hal)
|
114
|
-
|
107
|
+
dump(json).should be_valid_hal
|
115
108
|
end
|
116
109
|
|
117
110
|
it "should not match a document with deeply nested invalid resources" do
|
118
|
-
|
111
|
+
dump({
|
119
112
|
_embedded: {
|
120
113
|
user: [{
|
121
114
|
_embedded: {stuff: [{_links: {self: {lel: "/lol"}}}]}
|
@@ -4,23 +4,23 @@ require 'ostruct'
|
|
4
4
|
describe HalSpec::Matchers::HaveAValidHalContentType do
|
5
5
|
it "matches standard hal content types" do
|
6
6
|
response = OpenStruct.new(headers: {"Content-Type" => "application/vnd.hal+json"})
|
7
|
-
response.should have_a_valid_hal_content_type
|
7
|
+
response.headers.should have_a_valid_hal_content_type
|
8
8
|
|
9
9
|
response = OpenStruct.new(headers: {"Content-Type" => "application/hal+json"})
|
10
|
-
response.should have_a_valid_hal_content_type
|
10
|
+
response.headers.should have_a_valid_hal_content_type
|
11
11
|
end
|
12
12
|
|
13
13
|
it "doesn't match invalid hal content types" do
|
14
14
|
response = OpenStruct.new(headers: {"Content-Type" => "application/json"})
|
15
|
-
response.should_not have_a_valid_hal_content_type
|
15
|
+
response.headers.should_not have_a_valid_hal_content_type
|
16
16
|
end
|
17
17
|
|
18
18
|
it "doesn't match responses without content types" do
|
19
19
|
response = OpenStruct.new(headers: {})
|
20
|
-
response.should_not have_a_valid_hal_content_type
|
20
|
+
response.headers.should_not have_a_valid_hal_content_type
|
21
21
|
|
22
22
|
response = OpenStruct.new
|
23
|
-
response.should_not have_a_valid_hal_content_type
|
23
|
+
response.headers.should_not have_a_valid_hal_content_type
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hal_spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: json_spec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.1'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.1'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|