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.
@@ -1,11 +1,30 @@
1
1
  Feature: Equivalence
2
- Scenario: Complete HAL
2
+ Background:
3
3
  Given the HAL is:
4
4
  """
5
- {"_links": {"self": {"href": "/lol"}}}
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
- {"_links": {"self": {"href": "/lol"}}}
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
  """
@@ -9,7 +9,7 @@ def app
9
9
  end
10
10
 
11
11
  Given "the HAL is:" do |provided_hal|
12
- @hal = provided_hal
12
+ @hal = provided_hal.gsub(/\s+/, "")
13
13
  end
14
14
 
15
15
  When "I get the HAL" do
data/hal_spec.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.version = HalSpec::VERSION
21
21
 
22
22
  gem.add_dependency "halidator", "~> 0.5"
23
+ gem.add_dependency "json_spec", "~> 1.1"
23
24
 
24
25
  gem.add_development_dependency "rspec", "~> 2.0"
25
26
  gem.add_development_dependency "cucumber"
@@ -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 |whole_or_partial_hal|
5
-
6
- json_body = JSON.parse(last_response.body)
7
- json_body.should_not be_nil
8
- json_body.should == (JSON.parse(whole_or_partial_hal))
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 has_valid_body?(response)
5
- return false unless (response.respond_to?(:body) and !response.body.nil?)
6
- JSON.parse(response.body) rescue return false
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?(response)
5
- response.respond_to?(:headers) or return false
6
- response.headers.include?("Content-Type") or return false
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?(response.headers["Content-Type"])
10
+ ).include?(headers["Content-Type"])
11
11
  end
12
12
  end
13
13
  end
@@ -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 |response|
12
- has_valid_content_type?(response)
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 |response|
18
- has_valid_body?(response)
17
+ match do |body|
18
+ is_valid_hal?(body)
19
19
  end
20
20
  end
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module HalSpec
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/hal_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rspec'
2
+ require 'json_spec'
2
3
  require 'halidator'
3
4
  require "hal_spec/version"
4
5
  require "hal_spec/matchers"
@@ -1,82 +1,75 @@
1
1
  require 'spec_helper'
2
- require 'ostruct'
3
2
  require 'json'
4
3
 
5
- def Hal(body)
6
- OpenStruct.new(body: JSON.dump(body))
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 "response prerequisites" do
13
- it "should match responses with documented json" do
14
- OpenStruct.new(
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
- OpenStruct.new(bodie: "lel").should_not be_valid_hal
21
- OpenStruct.new(body: nil).should_not be_valid_hal
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
- Hal(minimal_hal).should be_valid_hal
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
- Hal(json).should be_valid_hal
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
- Hal(json).should be_valid_hal
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
- Hal(json).should be_valid_hal
42
+ dump(json).should be_valid_hal
50
43
  end
51
44
 
52
45
  it "should not match a document without links" do
53
- Hal({no_links: {self: {href: "/nope"}}}).should_not be_valid_hal
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
- Hal({_links: {lel: {href: "/pah"}}}).should_not be_valid_hal
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
- Hal({_links: {self: {breh: "/brah"}}}).should_not be_valid_hal
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
- Hal(json).should_not be_valid_hal
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
- Hal(json).should_not be_valid_hal
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
- Hal(json).should_not be_valid_hal
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
- Hal({_embedded: {resources: []}}.merge(minimal_hal)).should be_valid_hal
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
- Hal({_embedded: {resources: 2.times.collect{minimal_hal}}}.merge(minimal_hal)).should be_valid_hal
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
- Hal({
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
- Hal(json).should be_valid_hal
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
- Hal({
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.1
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