roar 1.0.2 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/ISSUE_TEMPLATE.md +20 -0
- data/.travis.yml +16 -11
- data/CHANGES.markdown +86 -57
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +7 -4
- data/LICENSE +1 -1
- data/README.markdown +133 -255
- data/Rakefile +3 -1
- data/examples/example.rb +0 -0
- data/examples/example_server.rb +0 -0
- data/lib/roar/client.rb +8 -3
- data/lib/roar/decorator.rb +2 -2
- data/lib/roar/http_verbs.rb +0 -16
- data/lib/roar/hypermedia.rb +30 -56
- data/lib/roar/json/collection.rb +10 -2
- data/lib/roar/json/hal.rb +74 -83
- data/lib/roar/json.rb +5 -5
- data/lib/roar/version.rb +1 -1
- data/lib/roar/xml.rb +1 -1
- data/lib/roar.rb +3 -3
- data/roar.gemspec +7 -5
- data/test/client_test.rb +1 -1
- data/test/coercion_feature_test.rb +7 -2
- data/test/decorator_test.rb +17 -7
- data/test/hal_json_test.rb +101 -94
- data/test/hypermedia_feature_test.rb +13 -31
- data/test/hypermedia_test.rb +26 -92
- data/test/{decorator_client_test.rb → integration/decorator_client_test.rb} +5 -4
- data/test/{faraday_http_transport_test.rb → integration/faraday_http_transport_test.rb} +1 -0
- data/test/{http_verbs_test.rb → integration/http_verbs_test.rb} +3 -2
- data/test/integration/json_collection_test.rb +35 -0
- data/test/{net_http_transport_test.rb → integration/net_http_transport_test.rb} +1 -0
- data/test/integration/runner.rb +2 -3
- data/test/integration/server.rb +6 -0
- data/test/json_representer_test.rb +2 -29
- data/test/lonely_test.rb +1 -2
- data/test/ssl_client_certs_test.rb +1 -1
- data/test/test_helper.rb +21 -3
- data/test/xml_representer_test.rb +6 -5
- metadata +21 -37
- data/gemfiles/Gemfile.representable-1.7 +0 -6
- data/gemfiles/Gemfile.representable-1.8 +0 -6
- data/gemfiles/Gemfile.representable-2.0 +0 -5
- data/gemfiles/Gemfile.representable-2.1 +0 -5
- data/gemfiles/Gemfile.representable-head +0 -6
- data/lib/roar/json/collection_json.rb +0 -208
- data/lib/roar/json/json_api.rb +0 -233
- data/test/collection_json_test.rb +0 -132
- data/test/hal_links_test.rb +0 -31
- data/test/json_api_test.rb +0 -451
- data/test/lib/runner.rb +0 -134
data/test/hal_json_test.rb
CHANGED
@@ -2,8 +2,8 @@ require 'test_helper'
|
|
2
2
|
require 'roar/json/hal'
|
3
3
|
|
4
4
|
class HalJsonTest < MiniTest::Spec
|
5
|
-
let(:
|
6
|
-
|
5
|
+
let(:decorator_class) do
|
6
|
+
Class.new(Roar::Decorator) do
|
7
7
|
include Roar::JSON
|
8
8
|
include Roar::JSON::HAL
|
9
9
|
|
@@ -18,112 +18,107 @@ class HalJsonTest < MiniTest::Spec
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
subject { Object.new
|
21
|
+
subject { decorator_class.new(Object.new) }
|
22
22
|
|
23
23
|
describe "links" do
|
24
24
|
describe "parsing" do
|
25
25
|
it "parses link array" do # TODO: remove me.
|
26
26
|
obj = subject.from_json("{\"_links\":{\"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}]}}")
|
27
|
-
|
27
|
+
subject.links.must_equal "self" => [link("rel" => "self", "href" => "http://en.hit", "lang" => "en"), link("rel" => "self", "href" => "http://de.hit", "lang" => "de")]
|
28
28
|
end
|
29
29
|
|
30
30
|
it "parses single links" do # TODO: remove me.
|
31
31
|
obj = subject.from_json("{\"_links\":{\"next\":{\"href\":\"http://next\"}}}")
|
32
|
-
|
32
|
+
subject.links.must_equal "next" => link("rel" => "next", "href" => "http://next")
|
33
33
|
end
|
34
34
|
|
35
35
|
it "parses link and link array" do
|
36
|
-
obj = subject.from_json(
|
37
|
-
|
36
|
+
obj = subject.from_json(%@{"_links":{"next":{"href":"http://next"}, "self":[{"lang":"en","href":"http://en.hit"},{"lang":"de","href":"http://de.hit"}]}}@)
|
37
|
+
subject._links.must_equal "next"=>link("rel" => "next", "href" => "http://next"), "self"=>[link("rel" => "self", "href" => "http://en.hit", "lang" => "en"), link("rel" => "self", "href" => "http://de.hit", "lang" => "de")]
|
38
38
|
end
|
39
39
|
|
40
40
|
it "parses empty link array" do
|
41
|
-
subject.from_json("{\"_links\":{\"self\":[]}}")
|
41
|
+
subject.from_json("{\"_links\":{\"self\":[]}}")
|
42
|
+
subject.links[:self].must_be_nil
|
42
43
|
end
|
43
44
|
|
44
45
|
it "parses non-existent link array" do
|
45
|
-
subject.from_json("{\"_links\":{}}")
|
46
|
+
subject.from_json("{\"_links\":{}}")
|
47
|
+
subject.links[:self].must_be_nil # DISCUSS: should this be []?
|
46
48
|
end
|
47
49
|
|
48
|
-
it "rejects single links declared as array" do
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
50
|
+
# it "rejects single links declared as array" do
|
51
|
+
# assert_raises TypeError do
|
52
|
+
# subject.from_json("{\"_links\":{\"self\":{\"href\":\"http://next\"}}}")
|
53
|
+
# end
|
54
|
+
# end
|
53
55
|
end
|
54
56
|
|
55
57
|
describe "rendering" do
|
56
58
|
it "renders link and link array" do
|
57
59
|
subject.to_json.must_equal "{\"_links\":{\"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}],\"next\":{\"href\":\"http://next\"}}}"
|
58
60
|
end
|
61
|
+
end
|
62
|
+
end
|
59
63
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
end
|
66
|
-
subject = Object.new.extend(rpr)
|
64
|
+
describe "empty link array" do
|
65
|
+
let(:decorator_class) do
|
66
|
+
Class.new(Roar::Decorator) do
|
67
|
+
include Roar::JSON
|
68
|
+
include Roar::JSON::HAL
|
67
69
|
|
68
|
-
|
70
|
+
links(:self) { [] }
|
69
71
|
end
|
70
72
|
end
|
71
|
-
end
|
72
73
|
|
73
|
-
|
74
|
-
# it "should map link arrays correctly" do
|
75
|
-
# subject.send :prepare_links!, {}
|
76
|
-
# subject.links.must_equal :self => [link("rel" => "self", "href" => "http://en.hit", "lang" => "en"),link("rel" => :self, "href" => "http://de.hit", "lang" => "de")], "next" => link("href" => "http://next", "rel" => "next")
|
77
|
-
# end
|
78
|
-
# end
|
74
|
+
subject { decorator_class.new(Object.new) }
|
79
75
|
|
80
|
-
|
81
|
-
|
82
|
-
subject.send(:link_array_rels).must_equal [:self]
|
76
|
+
it "gets render" do
|
77
|
+
subject.to_json.must_equal %@{"_links":{"self":[]}}@
|
83
78
|
end
|
84
79
|
end
|
85
80
|
|
86
81
|
|
87
|
-
describe "
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
82
|
+
describe "_links and _embedded" do
|
83
|
+
let(:decorator_class) do
|
84
|
+
Class.new(Roar::Decorator) do
|
85
|
+
include Roar::JSON
|
86
|
+
include Roar::JSON::HAL
|
87
|
+
|
88
|
+
property :id
|
89
|
+
collection :songs, class: Song, embedded: true do
|
90
|
+
include Roar::JSON::HAL
|
91
|
+
|
92
|
+
property :title
|
93
|
+
link(:self) { "http://songs/#{represented.title}" }
|
94
|
+
end
|
95
95
|
|
96
|
-
|
97
|
-
property :id
|
98
|
-
collection :songs, :class => Song, :extend => Bla, :embedded => true
|
99
|
-
link :self do
|
100
|
-
"http://albums/#{id}"
|
96
|
+
link(:self) { "http://albums/#{represented.id}" }
|
101
97
|
end
|
102
98
|
end
|
103
99
|
|
104
|
-
|
105
|
-
|
106
|
-
end
|
100
|
+
let(:album) { Album.new(:songs => [Song.new(:title => "Beer")], :id => 1) }
|
101
|
+
subject { decorator_class.new(album) }
|
107
102
|
|
108
103
|
it "render links and embedded resources according to HAL" do
|
109
|
-
|
104
|
+
subject.to_json.must_equal "{\"id\":1,\"_embedded\":{\"songs\":[{\"title\":\"Beer\",\"_links\":{\"self\":{\"href\":\"http://songs/Beer\"}}}]},\"_links\":{\"self\":{\"href\":\"http://albums/1\"}}}"
|
110
105
|
end
|
111
106
|
|
112
107
|
it "parses links and resources following the mighty HAL" do
|
113
|
-
|
114
|
-
assert_equal 2,
|
115
|
-
assert_equal "Coffee",
|
116
|
-
assert_equal "http://songs/Coffee",
|
117
|
-
assert_equal "http://albums/2",
|
108
|
+
subject.from_json("{\"id\":2,\"_embedded\":{\"songs\":[{\"title\":\"Coffee\",\"_links\":{\"self\":{\"href\":\"http://songs/Coffee\"}}}]},\"_links\":{\"self\":{\"href\":\"http://albums/2\"}}}")
|
109
|
+
assert_equal 2, album.id
|
110
|
+
assert_equal "Coffee", album.songs.first.title
|
111
|
+
# FIXME assert_equal "http://songs/Coffee", subject.songs.first.links["self"].href
|
112
|
+
assert_equal "http://albums/2", subject.links["self"].href
|
118
113
|
end
|
119
114
|
|
120
115
|
it "doesn't require _links and _embedded to be present" do
|
121
|
-
|
122
|
-
assert_equal 2,
|
116
|
+
subject.from_json("{\"id\":2}")
|
117
|
+
assert_equal 2, album.id
|
123
118
|
|
124
119
|
# in newer representables, this is not overwritten to an empty [] anymore.
|
125
|
-
assert_equal ["Beer"],
|
126
|
-
|
120
|
+
assert_equal ["Beer"], album.songs.map(&:title)
|
121
|
+
album.links.must_be_nil
|
127
122
|
end
|
128
123
|
end
|
129
124
|
|
@@ -134,58 +129,70 @@ class JsonHalTest < MiniTest::Spec
|
|
134
129
|
Artist = Struct.new(:name)
|
135
130
|
Song = Struct.new(:title)
|
136
131
|
|
137
|
-
def self.representer!
|
138
|
-
super([Roar::JSON::HAL])
|
139
|
-
end
|
140
|
-
|
141
|
-
def representer
|
142
|
-
rpr
|
143
|
-
end
|
144
|
-
|
145
132
|
describe "render_nil: false" do
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
133
|
+
let(:decorator_class) do
|
134
|
+
Class.new(Roar::Decorator) do
|
135
|
+
include Roar::JSON
|
136
|
+
include Roar::JSON::HAL
|
137
|
+
|
138
|
+
property :artist, embedded: true, render_nil: false do
|
139
|
+
property :name
|
140
|
+
end
|
150
141
|
|
151
|
-
|
152
|
-
|
142
|
+
collection :songs, embedded: true, render_empty: false do
|
143
|
+
property :title
|
144
|
+
end
|
153
145
|
end
|
154
146
|
end
|
155
147
|
|
156
|
-
it { Album.new(Artist.new("Bare, Jr."), [Song.new("Tobacco Spit")])
|
157
|
-
it { Album.new
|
148
|
+
it { decorator_class.new(Album.new(Artist.new("Bare, Jr."), [Song.new("Tobacco Spit")])).to_hash.must_equal({"_embedded"=>{"artist"=>{"name"=>"Bare, Jr."}, "songs"=>[{"title"=>"Tobacco Spit"}]}}) }
|
149
|
+
it { decorator_class.new(Album.new).to_hash.must_equal({}) }
|
158
150
|
end
|
159
|
-
end
|
160
151
|
|
152
|
+
describe "as: alias" do
|
153
|
+
let(:decorator_class) do
|
154
|
+
Class.new(Roar::Decorator) do
|
155
|
+
include Roar::JSON
|
156
|
+
include Roar::JSON::HAL
|
161
157
|
|
162
|
-
class
|
163
|
-
|
164
|
-
|
165
|
-
it "returns true for array link" do
|
166
|
-
subject.is_array?(:self).must_equal true
|
167
|
-
subject.is_array?("self").must_equal true
|
168
|
-
end
|
158
|
+
property :artist, as: :my_artist, class: Artist, embedded: true do
|
159
|
+
property :name
|
160
|
+
end
|
169
161
|
|
170
|
-
|
171
|
-
|
162
|
+
collection :songs, as: :my_songs, class: Song, embedded: true do
|
163
|
+
property :title
|
164
|
+
end
|
165
|
+
end
|
172
166
|
end
|
167
|
+
|
168
|
+
it { decorator_class.new(Album.new(Artist.new("Bare, Jr."), [Song.new("Tobacco Spit")])).to_hash.must_equal({"_embedded"=>{"my_artist"=>{"name"=>"Bare, Jr."}, "my_songs"=>[{"title"=>"Tobacco Spit"}]}}) }
|
169
|
+
it { decorator_class.new(Album.new).from_hash({"_embedded"=>{"my_artist"=>{"name"=>"Bare, Jr."}, "my_songs"=>[{"title"=>"Tobacco Spit"}]}}).inspect.must_equal "#<struct JsonHalTest::Album artist=#<struct JsonHalTest::Artist name=\"Bare, Jr.\">, songs=[#<struct JsonHalTest::Song title=\"Tobacco Spit\">]>" }
|
173
170
|
end
|
174
171
|
end
|
175
172
|
|
176
|
-
|
177
173
|
class HalCurieTest < MiniTest::Spec
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
174
|
+
let(:decorator_class) do
|
175
|
+
Class.new(Roar::Decorator) do
|
176
|
+
include Roar::JSON
|
177
|
+
include Roar::JSON::HAL
|
178
|
+
|
179
|
+
link "doc:self" do
|
180
|
+
"/"
|
181
|
+
end
|
182
|
+
|
183
|
+
links "doc:link_collection" do
|
184
|
+
[{:name => "link_collection", :href => "/"}]
|
185
|
+
end
|
182
186
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
+
curies do
|
188
|
+
[{:name => :doc,
|
189
|
+
:href => "//docs/{rel}",
|
190
|
+
:templated => true}]
|
191
|
+
end
|
187
192
|
end
|
188
193
|
end
|
189
194
|
|
190
|
-
|
195
|
+
subject { decorator_class.new(Object.new) }
|
196
|
+
|
197
|
+
it { subject.to_hash.must_equal({"_links"=>{"doc:self"=>{"href"=>"/"}, "doc:link_collection"=>[{"name"=>"link_collection", "href"=>"/"}], "curies"=>[{"name"=>:doc, "href"=>"//docs/{rel}", "templated"=>true}]}}) }
|
191
198
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'test_helper'
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class HypermediaTest < MiniTest::Spec
|
4
4
|
|
@@ -30,7 +30,7 @@ class HypermediaTest < MiniTest::Spec
|
|
30
30
|
|
31
31
|
describe "#to_xml" do
|
32
32
|
it "works when no links defined" do
|
33
|
-
|
33
|
+
decorator_class = Class.new(Roar::Decorator) do
|
34
34
|
include Roar::XML
|
35
35
|
include Roar::Hypermedia
|
36
36
|
|
@@ -38,22 +38,22 @@ class HypermediaTest < MiniTest::Spec
|
|
38
38
|
property :title
|
39
39
|
end
|
40
40
|
|
41
|
-
|
41
|
+
decorator_class.new(song).to_xml.must_equal_xml "<song><title>Brandy Wine</title></song>"
|
42
42
|
end
|
43
43
|
|
44
|
-
let
|
44
|
+
let(:decorator_class) { Class.new(Roar::Decorator) do
|
45
45
|
include Roar::XML
|
46
46
|
include Roar::Hypermedia
|
47
47
|
|
48
48
|
self.representation_wrap = "song"
|
49
49
|
property :title
|
50
50
|
|
51
|
-
link(:self) { "/songs/#{title}" }
|
51
|
+
link(:self) { "/songs/#{represented.title}" }
|
52
52
|
link(:all) { "/songs" }
|
53
53
|
end }
|
54
54
|
|
55
55
|
it "includes links in rendered document" do
|
56
|
-
|
56
|
+
decorator_class.new(song).to_xml.must_equal_xml %{
|
57
57
|
<song>
|
58
58
|
<title>Brandy Wine</title>
|
59
59
|
<link rel="self" href="/songs/Brandy Wine"/>
|
@@ -62,23 +62,22 @@ class HypermediaTest < MiniTest::Spec
|
|
62
62
|
end
|
63
63
|
|
64
64
|
it "suppresses links when links: false" do
|
65
|
-
|
65
|
+
decorator_class.new(song).to_xml(user_options: {links: false}).must_equal_xml "<song><title>Brandy Wine</title></song>"
|
66
66
|
end
|
67
67
|
|
68
68
|
it "renders nested links" do
|
69
|
-
|
70
|
-
|
71
|
-
album_rpr = Module.new do
|
69
|
+
song_decorator_class = decorator_class
|
70
|
+
album_decorator_class = Class.new(Roar::Decorator) do
|
72
71
|
include Roar::XML
|
73
72
|
include Roar::Hypermedia
|
74
73
|
|
75
74
|
self.representation_wrap = "album"
|
76
|
-
collection :songs, :extend =>
|
75
|
+
collection :songs, :extend => song_decorator_class
|
77
76
|
|
78
77
|
link(:self) { "/albums/mixed" }
|
79
78
|
end
|
80
79
|
|
81
|
-
Album.new(:songs => [song])
|
80
|
+
album_decorator_class.new(Album.new(:songs => [song])).to_xml.must_equal_xml %{
|
82
81
|
<album>
|
83
82
|
<song>
|
84
83
|
<title>Brandy Wine</title>
|
@@ -125,30 +124,13 @@ class HypermediaTest < MiniTest::Spec
|
|
125
124
|
</bookmarks>
|
126
125
|
})
|
127
126
|
|
128
|
-
assert_kind_of
|
127
|
+
assert_kind_of Hash, doc.links
|
129
128
|
assert_equal 1, doc.links.size
|
130
129
|
assert_equal(["self", "http://bookmarks"], [doc.links["self"].rel, doc.links["self"].href])
|
131
130
|
end
|
132
131
|
|
133
132
|
it "sets up an empty link list if no links found in the document" do
|
134
|
-
@bookmarks_with_links.new.from_xml(%{<bookmarks/>}).links.must_equal
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
|
141
|
-
class LinkCollectionTest < MiniTest::Spec
|
142
|
-
describe "LinkCollection" do
|
143
|
-
subject {
|
144
|
-
Roar::Hypermedia::LinkCollection[
|
145
|
-
@self_link = link(:rel => :self), @next_link = link(:rel => :next)
|
146
|
-
]
|
147
|
-
}
|
148
|
-
|
149
|
-
describe "::[]" do
|
150
|
-
it "keys by using rel string" do
|
151
|
-
subject.values.must_equal [@self_link, @next_link]
|
133
|
+
@bookmarks_with_links.new.from_xml(%{<bookmarks/>}).links.must_equal({})
|
152
134
|
end
|
153
135
|
end
|
154
136
|
end
|
data/test/hypermedia_test.rb
CHANGED
@@ -1,52 +1,39 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'roar/decorator'
|
2
3
|
|
3
4
|
class HypermediaTest < MiniTest::Spec
|
4
5
|
describe "inheritance" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
include Roar::Hypermedia
|
6
|
+
class BaseRepresenter < Roar::Decorator
|
7
|
+
include Roar::JSON
|
8
|
+
include Roar::Hypermedia
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
module Bar
|
14
|
-
include Roar::JSON
|
15
|
-
include Roar::Hypermedia
|
16
|
-
|
17
|
-
link(:bar) { "http://bar" }
|
18
|
-
end
|
10
|
+
link(:base) { "http://base" }
|
11
|
+
end
|
19
12
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
include BaseRepresenter
|
24
|
-
include Bar
|
13
|
+
class Bar < BaseRepresenter
|
14
|
+
link(:bar) { "http://bar" }
|
15
|
+
end
|
25
16
|
|
26
|
-
|
27
|
-
|
17
|
+
class Foo < Bar
|
18
|
+
link(:foo) { "http://foo" }
|
28
19
|
end
|
29
20
|
|
30
21
|
it "inherits parent links" do
|
31
|
-
|
32
|
-
|
33
|
-
assert_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"bar\",\"href\":\"http://bar\"},{\"rel\":\"foo\",\"href\":\"http://foo\"}]}", foo.to_json
|
22
|
+
Foo.new(Object.new).to_json.must_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"bar\",\"href\":\"http://bar\"},{\"rel\":\"foo\",\"href\":\"http://foo\"}]}"
|
34
23
|
end
|
35
24
|
|
36
25
|
it "inherits links from all mixed-in representers" do
|
37
|
-
|
38
|
-
Object.new.extend(BaseRepresenter).extend(Bar).to_json.must_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"bar\",\"href\":\"http://bar\"}]}"
|
26
|
+
Bar.new(Object.new).to_json.must_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"bar\",\"href\":\"http://bar\"}]}"
|
39
27
|
end
|
40
28
|
end
|
41
29
|
|
42
30
|
describe "#links_array" do
|
43
|
-
subject { Object.new
|
31
|
+
subject { decorator_class.new(Object.new) }
|
44
32
|
|
45
|
-
|
33
|
+
decorator_for do
|
46
34
|
link(:self) { "//self" }
|
47
35
|
end
|
48
36
|
|
49
|
-
|
50
37
|
describe "#to_json" do
|
51
38
|
it "renders" do
|
52
39
|
subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\"}]}"
|
@@ -56,7 +43,7 @@ class HypermediaTest < MiniTest::Spec
|
|
56
43
|
describe "#from_json" do
|
57
44
|
it "parses" do
|
58
45
|
subject.from_json "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\"}]}"
|
59
|
-
subject.links.must_equal(
|
46
|
+
subject.links.must_equal("self" => link("rel" => "self", "href" => "//self"))
|
60
47
|
end
|
61
48
|
end
|
62
49
|
|
@@ -64,7 +51,7 @@ class HypermediaTest < MiniTest::Spec
|
|
64
51
|
describe "#link" do
|
65
52
|
|
66
53
|
describe "with any options" do
|
67
|
-
|
54
|
+
decorator_for do
|
68
55
|
link(:rel => :self, :title => "Hey, @myabc") { "//self" }
|
69
56
|
end
|
70
57
|
|
@@ -74,38 +61,39 @@ class HypermediaTest < MiniTest::Spec
|
|
74
61
|
end
|
75
62
|
|
76
63
|
describe "with string rel" do
|
77
|
-
|
64
|
+
decorator_for do
|
78
65
|
link("ns:self") { "//self" }
|
79
66
|
end
|
80
67
|
|
81
68
|
it "renders rel" do
|
69
|
+
# raise subject.inspect
|
82
70
|
subject.to_json.must_equal "{\"links\":[{\"rel\":\"ns:self\",\"href\":\"//self\"}]}"
|
83
71
|
end
|
84
72
|
end
|
85
73
|
|
86
74
|
describe "passing options to serialize" do
|
87
|
-
|
75
|
+
decorator_for do
|
88
76
|
link(:self) { |opts| "//self/#{opts[:id]}" }
|
89
77
|
end
|
90
78
|
|
91
79
|
it "receives options when rendering" do
|
92
|
-
subject.to_json(:id
|
80
|
+
subject.to_json(user_options: { id: 1 }).must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]}"
|
93
81
|
end
|
94
82
|
|
95
83
|
describe "in a composition" do
|
96
|
-
|
84
|
+
decorator_for do
|
97
85
|
property :entity, :extend => self
|
98
86
|
link(:self) { |opts| "//self/#{opts[:id]}" }
|
99
87
|
end
|
100
88
|
|
101
89
|
it "propagates options" do
|
102
|
-
Song.new(:entity => Song.new)
|
90
|
+
decorator_class.new(Song.new(:entity => Song.new)).to_json(user_options: { id: 1 }).must_equal "{\"entity\":{\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]},\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]}"
|
103
91
|
end
|
104
92
|
end
|
105
93
|
end
|
106
94
|
|
107
95
|
describe "returning option hash from block" do
|
108
|
-
|
96
|
+
decorator_for do
|
109
97
|
link(:self) do {:href => "//self", :type => "image/jpg"} end
|
110
98
|
link(:other) do |params|
|
111
99
|
hash = { :href => "//other" }
|
@@ -119,13 +107,13 @@ class HypermediaTest < MiniTest::Spec
|
|
119
107
|
end
|
120
108
|
|
121
109
|
it "is rendered according to context" do
|
122
|
-
subject.to_json(type: true).must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"},{\"rel\":\"other\",\"href\":\"//other\",\"type\":\"image/jpg\"}]}"
|
110
|
+
subject.to_json(user_options: { type: true }).must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"},{\"rel\":\"other\",\"href\":\"//other\",\"type\":\"image/jpg\"}]}"
|
123
111
|
subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"},{\"rel\":\"other\",\"href\":\"//other\"}]}"
|
124
112
|
end
|
125
113
|
end
|
126
114
|
|
127
115
|
describe "not calling #link" do
|
128
|
-
|
116
|
+
decorator_for {}
|
129
117
|
|
130
118
|
it "still allows rendering" do
|
131
119
|
subject.to_json.must_equal "{}"
|
@@ -134,57 +122,3 @@ class HypermediaTest < MiniTest::Spec
|
|
134
122
|
end
|
135
123
|
end
|
136
124
|
end
|
137
|
-
|
138
|
-
class HyperlinkTest < MiniTest::Spec
|
139
|
-
describe "Hyperlink" do
|
140
|
-
subject { link(:rel => "self", "href" => "http://self", "data-whatever" => "Hey, @myabc") }
|
141
|
-
|
142
|
-
it "accepts string keys in constructor" do
|
143
|
-
assert_equal "Hey, @myabc", subject.send("data-whatever")
|
144
|
-
end
|
145
|
-
|
146
|
-
it "responds to #rel" do
|
147
|
-
assert_equal "self", subject.rel
|
148
|
-
end
|
149
|
-
|
150
|
-
it "responds to #href" do
|
151
|
-
assert_equal "http://self", subject.href
|
152
|
-
end
|
153
|
-
|
154
|
-
it "responds to #replace with string keys" do
|
155
|
-
subject.replace("rel" => "next")
|
156
|
-
assert_equal nil, subject.href
|
157
|
-
assert_equal "next", subject.rel
|
158
|
-
end
|
159
|
-
|
160
|
-
it "responds to #each and implements Enumerable" do
|
161
|
-
assert_equal ["rel:self", "href:http://self", "data-whatever:Hey, @myabc"], subject.collect { |k,v| "#{k}:#{v}" }
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
describe "Config inheritance" do
|
166
|
-
it "doesn't mess up with inheritable_array" do # FIXME: remove this test when uber is out.
|
167
|
-
OpenStruct.new.extend( Module.new do
|
168
|
-
include Roar::JSON
|
169
|
-
include(Module.new do
|
170
|
-
include Roar::JSON
|
171
|
-
include Roar::Hypermedia
|
172
|
-
|
173
|
-
property :bla
|
174
|
-
|
175
|
-
link( :self) {"bo"}
|
176
|
-
|
177
|
-
#puts "hey ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
178
|
-
#puts representable_attrs.inheritable_array(:links).inspect
|
179
|
-
end)
|
180
|
-
|
181
|
-
|
182
|
-
#puts representable_attrs.inheritable_array(:links).inspect
|
183
|
-
|
184
|
-
property :blow
|
185
|
-
include Roar::Hypermedia
|
186
|
-
link(:bla) { "boo" }
|
187
|
-
end).to_hash.must_equal({"links"=>[{"rel"=>"self", "href"=>"bo"}, {"rel"=>"bla", "href"=>"boo"}]})
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'integration/runner'
|
2
3
|
require 'roar/decorator'
|
3
4
|
require 'roar/client'
|
4
5
|
|
@@ -6,7 +7,7 @@ class DecoratorClientTest < MiniTest::Spec
|
|
6
7
|
class Crew
|
7
8
|
attr_accessor :moniker, :company
|
8
9
|
end
|
9
|
-
|
10
|
+
|
10
11
|
class CrewDecorator < Roar::Decorator
|
11
12
|
include Roar::JSON
|
12
13
|
include Roar::Hypermedia
|
@@ -26,7 +27,7 @@ class DecoratorClientTest < MiniTest::Spec
|
|
26
27
|
before do
|
27
28
|
@crew = Crew.new
|
28
29
|
@client = CrewClient.new(@crew)
|
29
|
-
end
|
30
|
+
end
|
30
31
|
|
31
32
|
describe 'HttpVerbs integration' do
|
32
33
|
describe '#get' do
|
@@ -54,11 +55,11 @@ class DecoratorClientTest < MiniTest::Spec
|
|
54
55
|
@crew.moniker = 'Silence'
|
55
56
|
@client.to_json.must_equal %{{\"name\":\"Silence\",\"links\":[]}}
|
56
57
|
end
|
57
|
-
|
58
|
+
|
58
59
|
# since this is considered dangerous, we test the mutuable options.
|
59
60
|
it "adds links: false to options" do
|
60
61
|
@client.to_hash(options = {})
|
61
|
-
options.must_equal({:
|
62
|
+
options.must_equal(user_options: {links: false})
|
62
63
|
end
|
63
64
|
end
|
64
65
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'integration/runner'
|
2
3
|
require 'roar/http_verbs'
|
3
4
|
require 'roar/json'
|
4
5
|
|
@@ -80,11 +81,11 @@ class HttpVerbsTest < MiniTest::Spec
|
|
80
81
|
describe "#post" do
|
81
82
|
it "updates instance with incoming representation" do
|
82
83
|
@band.name = "Strung Out"
|
83
|
-
|
84
|
+
assert_nil @band.label
|
84
85
|
|
85
86
|
@band.post(:uri => "http://localhost:4567/bands", :as => "application/xml")
|
86
87
|
assert_equal "STRUNG OUT", @band.name
|
87
|
-
|
88
|
+
assert_nil @band.label
|
88
89
|
end
|
89
90
|
end
|
90
91
|
|