roar 0.0.1.alpha1 → 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.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.travis.yml +10 -0
  4. data/CHANGES.markdown +203 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE +20 -0
  7. data/README.markdown +734 -0
  8. data/Rakefile +10 -0
  9. data/TODO.markdown +13 -0
  10. data/examples/example.rb +370 -0
  11. data/examples/example_server.rb +34 -0
  12. data/gemfiles/Gemfile.representable-2.0 +5 -0
  13. data/gemfiles/Gemfile.representable-2.1 +5 -0
  14. data/lib/roar/client.rb +38 -0
  15. data/lib/roar/coercion.rb +20 -0
  16. data/lib/roar/decorator.rb +11 -0
  17. data/lib/roar/http_verbs.rb +88 -0
  18. data/lib/roar/hypermedia.rb +179 -0
  19. data/lib/roar/json/collection.rb +3 -0
  20. data/lib/roar/json/collection_json.rb +208 -0
  21. data/lib/roar/json/hal.rb +223 -0
  22. data/lib/roar/json/hash.rb +3 -0
  23. data/lib/roar/json/json_api.rb +228 -0
  24. data/lib/roar/json.rb +55 -0
  25. data/lib/roar/representer.rb +11 -0
  26. data/lib/roar/transport/faraday.rb +49 -0
  27. data/lib/roar/transport/net_http/request.rb +77 -0
  28. data/lib/roar/transport/net_http.rb +49 -0
  29. data/lib/roar/version.rb +1 -1
  30. data/lib/roar/xml.rb +54 -0
  31. data/lib/roar.rb +4 -1
  32. data/roar.gemspec +14 -5
  33. data/test/client_test.rb +36 -0
  34. data/test/coercion_feature_test.rb +23 -0
  35. data/test/collection_json_test.rb +130 -0
  36. data/test/decorator_test.rb +132 -0
  37. data/test/faraday_http_transport_test.rb +82 -0
  38. data/test/fixtures/sample.pem +31 -0
  39. data/test/hal_json_test.rb +160 -0
  40. data/test/hal_links_test.rb +31 -0
  41. data/test/http_verbs_test.rb +154 -0
  42. data/test/hypermedia_feature_test.rb +155 -0
  43. data/test/hypermedia_test.rb +190 -0
  44. data/test/integration/Gemfile +4 -0
  45. data/test/integration/band_representer.rb +8 -0
  46. data/test/integration/runner.rb +42 -0
  47. data/test/integration/server.rb +120 -0
  48. data/test/integration/ssl_server.rb +60 -0
  49. data/test/json_api_test.rb +395 -0
  50. data/test/json_representer_test.rb +149 -0
  51. data/test/lib/runner.rb +134 -0
  52. data/test/lonely_test.rb +9 -0
  53. data/test/net_http_transport_test.rb +78 -0
  54. data/test/representer_test.rb +39 -0
  55. data/test/ssl_client_certs_test.rb +102 -0
  56. data/test/test_helper.rb +60 -0
  57. data/test/xml_representer_test.rb +68 -0
  58. metadata +209 -49
@@ -0,0 +1,130 @@
1
+ require 'test_helper'
2
+ require 'roar/json/collection_json'
3
+
4
+ class CollectionJsonTest < MiniTest::Spec
5
+ let(:song) { OpenStruct.new(:title => "scarifice", :length => 43) }
6
+
7
+ representer_for([Roar::JSON::CollectionJSON]) do
8
+ version "1.0"
9
+ href { "//songs/" }
10
+
11
+ link(:feed) { "//songs/feed" }
12
+
13
+ items(:class => Song) do
14
+ href { "//songs/scarifice" }
15
+
16
+ property :title, :prompt => "Song title"
17
+ property :length, :prompt => "Song length"
18
+
19
+ link(:download) { "//songs/scarifice.mp3" }
20
+ link(:stats) { "//songs/scarifice/stats" }
21
+ end
22
+
23
+ template do
24
+ property :title, :prompt => "Song title"
25
+ property :length, :prompt => "Song length"
26
+ end
27
+
28
+ queries do
29
+ link :search do
30
+ {:href => "//search", :data => [{:name => "q", :value => ""}]}
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#to_json" do
36
+ it "renders document" do
37
+ [song].extend(rpr).to_hash.must_equal(
38
+ {
39
+ "collection"=>{
40
+ "version"=>"1.0",
41
+ "href"=>"//songs/",
42
+
43
+ "template"=>{
44
+ :data=>[
45
+ {:name=>"title", :value=>nil},
46
+ {:name=>"length", :value=>nil}
47
+ ]
48
+ },
49
+
50
+ "queries"=>[
51
+ {"rel"=>"search", "href"=>"//search",
52
+ "data"=>[
53
+ {:name=>"q", :value=>""}
54
+ ]
55
+ }
56
+ ],
57
+
58
+ "items"=>[
59
+ {
60
+ "links"=>[
61
+ {"rel"=>"download", "href"=>"//songs/scarifice.mp3"},
62
+ {"rel"=>"stats", "href"=>"//songs/scarifice/stats"}
63
+ ],
64
+ "href"=>"//songs/scarifice",
65
+ :data=>[
66
+ {:name=>"title", :value=>"scarifice"},
67
+ {:name=>"length", :value=>43}
68
+ ]
69
+ }
70
+ ],
71
+
72
+ "links"=>[
73
+ {"rel"=>"feed", "href"=>"//songs/feed"}
74
+ ]
75
+ }
76
+ })# %{{"collection":{"version":"1.0","href":"//songs/","items":[{"href":"//songs/scarifice","links":[{"rel":"download","href":"//songs/scarifice.mp3"},{"rel":"stats","href":"//songs/scarifice/stats"}],"data":[{"name":"title","value":"scarifice"},{"name":"length","value":43}]}],"template":{"data":[{"name":"title","value":null},{"name":"length","value":null}]},"queries":[{"rel":"search","href":"//search","data":[{"name":"q","value":""}]}],"links":[{"rel":"feed","href":"//songs/feed"}]}}}
77
+ end
78
+ end
79
+
80
+ describe "#from_json" do
81
+ subject { [].extend(rpr).from_json [song].extend(rpr).to_json }
82
+
83
+ it "provides #version" do
84
+ subject.version.must_equal "1.0"
85
+ end
86
+
87
+ it "provides #href" do
88
+ subject.href.must_equal link(:href => "//songs/")
89
+ end
90
+
91
+ it "provides #template" do
92
+ # DISCUSS: this might return a Template instance, soon.
93
+ subject.template.must_equal([
94
+ {"name"=>"title", "value"=>nil},
95
+ {"name"=>"length", "value"=>nil}])
96
+ end
97
+
98
+ it "provides #queries" do
99
+ # DISCUSS: this might return CollectionJSON::Hyperlink instances that support some kind of substitution operation for the :data attribute.
100
+ # FIXME: this is currently _not_ parsed!
101
+ subject.queries.must_equal([link(:rel => :search, :href=>"//search", :data=>[{:name=>"q", :value=>""}])])
102
+ end
103
+
104
+ it "provides #items" do
105
+ subject.items.must_equal([Song.new(:title => "scarifice", :length => "43")])
106
+ song = subject.items.first
107
+ song.title.must_equal "scarifice"
108
+ song.length.must_equal 43
109
+ song.links.must_equal("download" => link({:rel=>"download", :href=>"//songs/scarifice.mp3"}), "stats" => link({:rel=>"stats", :href=>"//songs/scarifice/stats"}))
110
+ song.href.must_equal link(:href => "//songs/scarifice")
111
+ end
112
+
113
+ it "provides #links" do
114
+ subject.links.must_equal({"feed" => link(:rel => "feed", :href => "//songs/feed")})
115
+ end
116
+ end
117
+
118
+ describe "template_representer#from_json" do
119
+ it "parses object" do
120
+ song = OpenStruct.new.extend(rpr.template_representer).from_hash(
121
+ "template"=>{
122
+ "data"=>[
123
+ {"name"=>"title", "value"=>"Black Star"},
124
+ {"name"=>"length", "value"=>"4.53"}
125
+ ]
126
+ })
127
+ song.title.must_equal "Black Star"
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,132 @@
1
+ require 'test_helper'
2
+ require 'roar/decorator'
3
+
4
+ class DecoratorTest < MiniTest::Spec
5
+ class SongRepresentation < Roar::Decorator
6
+ include Roar::JSON
7
+
8
+ property :name
9
+ end
10
+
11
+ describe "Decorator" do
12
+ let (:song) { OpenStruct.new(:name => "Not The Same") }
13
+
14
+ it "exposes ::prepare" do
15
+ SongRepresentation.prepare(song).to_hash.must_equal({"name"=>"Not The Same"})
16
+ end
17
+ end
18
+
19
+ describe "Hypermedia modules" do
20
+ representer_for do
21
+ link(:self) { "http://self" } # TODO: test with Collection::JSON, too.
22
+ end
23
+
24
+ let (:model) { Object.new }
25
+ let (:model_with_links) { model.singleton_class.instance_eval { attr_accessor :links }; model }
26
+
27
+ describe "JSON" do
28
+ let (:decorator_class) { rpr_mod = rpr
29
+ Class.new(Roar::Decorator) do
30
+ include rpr_mod
31
+ end }
32
+ let (:decorator) { decorator_class.new(model) }
33
+
34
+ it "rendering links works" do
35
+ decorator.to_hash.must_equal({"links"=>[{"rel"=>"self", "href"=>"http://self"}]})
36
+ end
37
+
38
+ it "sets links on decorator" do
39
+ decorator.from_hash("links"=>[{:rel=>:self, :href=>"http://next"}])
40
+ decorator.links.must_equal("self"=>link(:rel=>:self, :href=>"http://next"))
41
+ end
42
+
43
+ it "does not set links on represented" do
44
+ decorator_class.new(model_with_links).from_hash("links"=>[{:rel=>:self, :href=>"http://self"}])
45
+ model_with_links.links.must_equal nil
46
+ end
47
+
48
+ class ConsumingDecorator < Roar::Decorator
49
+ include Roar::JSON
50
+ include Roar::Hypermedia
51
+ link(:self) { "http://self" }
52
+
53
+ include HypermediaConsumer
54
+ end
55
+
56
+ # TODO: test include ModuleWithLinks
57
+
58
+ describe "Decorator::HypermediaClient" do
59
+ it "propagates links to represented" do
60
+ decorator = ConsumingDecorator.new(model_with_links)
61
+
62
+
63
+ decorator.from_hash("links"=>[{:rel=>:self, :href=>"http://percolator"}])
64
+
65
+ # links are always set on decorator instance.
66
+ decorator .links[:self].must_equal(link(:rel=>:self, :href=>"http://percolator"))
67
+
68
+ # and propagated to represented with HypermediaConsumer.
69
+ model_with_links.links[:self].must_equal(link(:rel=>:self, :href=>"http://percolator"))
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "XML" do
75
+ representer_for([Roar::XML, Roar::Hypermedia]) do
76
+ link(:self) { "http://self" } # TODO: test with HAL, too.
77
+ #self.representation_wrap = :song # FIXME: why isn't this working?
78
+ end
79
+ let (:decorator_class) { rpr_mod = rpr
80
+ Class.new(Roar::Decorator) do
81
+ include rpr_mod
82
+ self.representation_wrap = :song
83
+ end
84
+ }
85
+ let (:decorator) { decorator_class.new(model) }
86
+
87
+ it "rendering links works" do
88
+ decorator.to_xml.must_equal_xml "<song><link rel=\"self\" href=\"http://self\"/></song>"
89
+ end
90
+
91
+ it "sets links on decorator" do
92
+ decorator.from_xml(%{<song><link rel="self" href="http://next"/></song>})
93
+ decorator.links.must_equal("self"=>link(:rel=>"self", :href=>"http://next"))
94
+ end
95
+ end
96
+
97
+
98
+ describe "JSON::HAL" do
99
+ representer_for([Roar::JSON::HAL]) do
100
+ link(:self) { "http://self" }
101
+ end
102
+ let (:decorator_class) { rpr_mod = rpr
103
+ Class.new(Roar::Decorator) do
104
+ include rpr_mod
105
+ end
106
+ }
107
+ let (:decorator) { decorator_class.new(model) }
108
+
109
+ it "rendering links works" do
110
+ decorator.to_hash.must_equal({"_links"=>{"self"=>{"href"=>"http://self"}}})
111
+ end
112
+
113
+ it "sets links on decorator" do
114
+ decorator.from_hash({"_links"=>{"self"=>{"href"=>"http://next"}}})
115
+ decorator.links.must_equal("self"=>link("rel"=>"self", "href"=>"http://next"))
116
+ end
117
+
118
+ describe "Decorator::HypermediaClient" do
119
+ let (:decorator) { rpr_mod = rpr
120
+ Class.new(Roar::Decorator) do
121
+ include rpr_mod
122
+ include Roar::Decorator::HypermediaConsumer
123
+ end }
124
+
125
+ it "propagates links to represented" do
126
+ decorator.new(model_with_links).from_hash("_links"=>{"self"=>{:href=>"http://self"}})
127
+ model_with_links.links[:self].must_equal(link(:rel=>"self", :href=>"http://self"))
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+ require 'roar/transport/faraday'
3
+
4
+ class FaradayHttpTransportTest < MiniTest::Spec
5
+ describe 'FaradayHttpTransport' do
6
+ let(:url) { "http://localhost:4567/method" }
7
+ let(:body) { "booty" }
8
+ let(:as) { "application/xml" }
9
+ before do
10
+ @transport = Roar::Transport::Faraday.new
11
+ end
12
+
13
+ it "#get_uri returns response" do
14
+ @transport.get_uri(uri: url, as: as).must_match_faraday_response :get, url, as
15
+ end
16
+
17
+ it "#post_uri returns response" do
18
+ @transport.post_uri(uri: url, body: body, as: as).must_match_faraday_response :post, url, as, body
19
+ end
20
+
21
+ it "#put_uri returns response" do
22
+ @transport.put_uri(uri: url, body: body, as: as).must_match_faraday_response :put, url, as, body
23
+ end
24
+
25
+ it "#delete_uri returns response" do
26
+ @transport.delete_uri(uri: url, as: as).must_match_faraday_response :delete, url, as
27
+ end
28
+
29
+ it "#patch_uri returns response" do
30
+ @transport.patch_uri(uri: url, body: body, as: as).must_match_faraday_response :patch, url, as, body
31
+ end
32
+
33
+ describe 'non-existent resource' do
34
+ let(:not_found_url) { 'http://localhost:4567/missing-resource' }
35
+
36
+ it '#get_uri raises a ResourceNotFound error' do
37
+ assert_raises(Faraday::Error::ResourceNotFound) do
38
+ @transport.get_uri(uri: not_found_url, as: as).body
39
+ end
40
+ end
41
+
42
+ it '#post_uri raises a ResourceNotFound error' do
43
+ assert_raises(Faraday::Error::ResourceNotFound) do
44
+ @transport.post_uri(uri: not_found_url, body: body, as: as).body
45
+ end
46
+ end
47
+
48
+ it '#post_uri raises a ResourceNotFound error' do
49
+ assert_raises(Faraday::Error::ResourceNotFound) do
50
+ @transport.post_uri(uri: not_found_url, body: body, as: as).body
51
+ end
52
+ end
53
+
54
+ it '#delete_uri raises a ResourceNotFound error' do
55
+ assert_raises(Faraday::Error::ResourceNotFound) do
56
+ @transport.delete_uri(uri: not_found_url, body: body, as: as).body
57
+ end
58
+ end
59
+ end
60
+
61
+ describe 'server errors (500 Internal Server Error)' do
62
+ it '#get_uri raises a ClientError' do
63
+ assert_raises(Faraday::Error::ClientError) do
64
+ @transport.get_uri(uri: 'http://localhost:4567/deliberate-error', as: as).body
65
+ end
66
+ end
67
+ end
68
+
69
+ end
70
+ end
71
+
72
+ module MiniTest::Assertions
73
+
74
+ def assert_faraday_response(type, response, url, as, body = nil)
75
+ headers = response.env[:request_headers]
76
+ assert_equal [as, as], [headers["Accept"], headers["Content-Type"]]
77
+ assert_equal "<method>#{type}#{(' - ' + body) if body}</method>", response.body
78
+ end
79
+
80
+ end
81
+
82
+ Faraday::Response.infect_an_assertion :assert_faraday_response, :must_match_faraday_response
@@ -0,0 +1,31 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIICcTCCAdqgAwIBAgIJAMvA30EY1rKtMA0GCSqGSIb3DQEBBQUAMDAxCzAJBgNV
3
+ BAYTAlVTMQ0wCwYDVQQKEwRLZWFzMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMDgw
4
+ OTA0MTU1MTU5WhcNMTgwOTAyMTU1MTU5WjAwMQswCQYDVQQGEwJVUzENMAsGA1UE
5
+ ChMES2VhczESMBAGA1UEAxMJbG9jYWxob3N0MIGfMA0GCSqGSIb3DQEBAQUAA4GN
6
+ ADCBiQKBgQDK17rB/KVaK8MVjiEkvA4ZncOOIC3nStZ/erXM+qwkghPM4Tfr2FTU
7
+ iTgwwdLdu/ht74oWnppttfaTQ+sVz2rFXnPgfqKTGoJTwWFiuNuZhSRDVssGVnL/
8
+ RatZW6wns8UNf+W4hUe6/vGQP6obNTe2T4R+t2hXP51OkOy4BMcq0QIDAQABo4GS
9
+ MIGPMB0GA1UdDgQWBBQDIsX7HoSqbxKrCawi64MkXRmtmzBgBgNVHSMEWTBXgBQD
10
+ IsX7HoSqbxKrCawi64MkXRmtm6E0pDIwMDELMAkGA1UEBhMCVVMxDTALBgNVBAoT
11
+ BEtlYXMxEjAQBgNVBAMTCWxvY2FsaG9zdIIJAMvA30EY1rKtMAwGA1UdEwQFMAMB
12
+ Af8wDQYJKoZIhvcNAQEFBQADgYEAW5UBM7EIMpARzQwpQ8N1gyTR/VqJ9fSm4MIw
13
+ Y5m90HRgsDcXVbhn0rRfcC8o4EtGDvCjqsFYXy/ImF9tjEiuaysxbqepl+XMszPE
14
+ 1kO50quWsV1FLSdcJX6t/ofJYOxiQkqPvg9t/ovTnEZ+w4NfPo+0MJgudjJoD2+w
15
+ 5UTsKtU=
16
+ -----END CERTIFICATE-----
17
+ -----BEGIN RSA PRIVATE KEY-----
18
+ MIICXQIBAAKBgQDK17rB/KVaK8MVjiEkvA4ZncOOIC3nStZ/erXM+qwkghPM4Tfr
19
+ 2FTUiTgwwdLdu/ht74oWnppttfaTQ+sVz2rFXnPgfqKTGoJTwWFiuNuZhSRDVssG
20
+ VnL/RatZW6wns8UNf+W4hUe6/vGQP6obNTe2T4R+t2hXP51OkOy4BMcq0QIDAQAB
21
+ AoGAHcDJDx1M784NfoLrj6TZ+J3wik9kDFIo5mgMdLWsPGqsFthOSJTh1I8QI+66
22
+ THX++bkyKyE2i7MuKOnEeN2Ezo2jAThF7XoWhm6/+pSXhSqmL1jKr/1CZRaR9jv0
23
+ cCVJc3mTuAGH+yFVeGpWNvDaCmOUlD5M48xTROJXteDQ0TECQQDuDM9pmQdqkGIp
24
+ dvbIviS8donYn0kJ0TKS14pMtb/C63lcld513rHS43ru3FRY9baR/q5vV9vW5RhH
25
+ S7w4cYvVAkEA2iNLsFEAkY88oZJYbdyybeKxZdReyes1/zPe4RYzRdbDHRNAa+zk
26
+ mZIZDI820E0Y+DeoT+q3nXkXiiOS/iRNDQJBAKdAvOH2sO1AcJetjArS/cCkkIlw
27
+ sMKDB0OAyRzIfekXxPc2HU03oD0Jsy/sAh9W1GWTST/VvRIpeHtvTNljfdkCQF5T
28
+ UuBcNoW6zXoEYU6oV1Oi6hjhW1eu6PuAv4jPY754XoiNEZdZqYQqo8BFkWtDW1/C
29
+ GXrtQRbMDPzD40UYB2UCQQCmJpJp+u2lHj7zuZikHIHQBNyXyoGnzgNs6XUj1Bs6
30
+ Y4vjue8w6RkRLZ1YGP+xqsngVqb9IRygyLDpEgwEnOT4
31
+ -----END RSA PRIVATE KEY-----
@@ -0,0 +1,160 @@
1
+ require 'test_helper'
2
+ require 'roar/json/hal'
3
+
4
+ class HalJsonTest < MiniTest::Spec
5
+ let(:rpr) do
6
+ Module.new do
7
+ include Roar::JSON
8
+ include Roar::JSON::HAL
9
+
10
+ links :self do
11
+ [{:lang => "en", :href => "http://en.hit"},
12
+ {:lang => "de", :href => "http://de.hit"}]
13
+ end
14
+
15
+ link :next do
16
+ "http://next"
17
+ end
18
+ end
19
+ end
20
+
21
+ subject { Object.new.extend(rpr) }
22
+
23
+ describe "links" do
24
+ describe "parsing" do
25
+ it "parses link array" do # TODO: remove me.
26
+ obj = subject.from_json("{\"_links\":{\"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}]}}")
27
+ obj.links.must_equal "self" => [link("rel" => "self", "href" => "http://en.hit", "lang" => "en"), link("rel" => "self", "href" => "http://de.hit", "lang" => "de")]
28
+ end
29
+
30
+ it "parses single links" do # TODO: remove me.
31
+ obj = subject.from_json("{\"_links\":{\"next\":{\"href\":\"http://next\"}}}")
32
+ obj.links.must_equal "next" => link("rel" => "next", "href" => "http://next")
33
+ end
34
+
35
+ it "parses link and link array" do
36
+ obj = subject.from_json("{\"_links\":{\"next\":{\"href\":\"http://next\"}, \"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}]}}")
37
+ obj.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
+ end
39
+
40
+ it "parses empty link array" do
41
+ subject.from_json("{\"_links\":{\"self\":[]}}").links[:self].must_equal []
42
+ end
43
+
44
+ it "parses non-existent link array" do
45
+ subject.from_json("{\"_links\":{}}").links[:self].must_equal nil # DISCUSS: should this be []?
46
+ end
47
+
48
+ it "rejects single links declared as array" do
49
+ assert_raises TypeError do
50
+ subject.from_json("{\"_links\":{\"self\":{\"href\":\"http://next\"}}}")
51
+ end
52
+ end
53
+ end
54
+
55
+ describe "rendering" do
56
+ it "renders link and link array" do
57
+ subject.to_json.must_equal "{\"_links\":{\"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}],\"next\":{\"href\":\"http://next\"}}}"
58
+ end
59
+
60
+ it "renders empty link array" do
61
+ rpr = Module.new do
62
+ include Roar::JSON::HAL
63
+
64
+ links :self do [] end
65
+ end
66
+ subject = Object.new.extend(rpr)
67
+
68
+ subject.to_json.must_equal "{\"_links\":{\"self\":[]}}"
69
+ end
70
+ end
71
+ end
72
+
73
+ # describe "#prepare_links!" do
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
79
+
80
+ describe "#link_array_rels" do
81
+ it "returns list of rels for array links" do
82
+ subject.send(:link_array_rels).must_equal [:self]
83
+ end
84
+ end
85
+
86
+
87
+ describe "HAL/JSON" do
88
+ Bla = Module.new do
89
+ include Roar::JSON::HAL
90
+ property :title
91
+ link :self do
92
+ "http://songs/#{title}"
93
+ end
94
+ end
95
+
96
+ representer_for([Roar::JSON::HAL]) do
97
+ property :id
98
+ collection :songs, :class => Song, :extend => Bla, :embedded => true
99
+ link :self do
100
+ "http://albums/#{id}"
101
+ end
102
+ end
103
+
104
+ before do
105
+ @album = Album.new(:songs => [Song.new(:title => "Beer")], :id => 1).extend(rpr)
106
+ end
107
+
108
+ it "render links and embedded resources according to HAL" do
109
+ assert_equal "{\"id\":1,\"_embedded\":{\"songs\":[{\"title\":\"Beer\",\"_links\":{\"self\":{\"href\":\"http://songs/Beer\"}}}]},\"_links\":{\"self\":{\"href\":\"http://albums/1\"}}}", @album.to_json
110
+ end
111
+
112
+ it "parses links and resources following the mighty HAL" do
113
+ @album.from_json("{\"id\":2,\"_embedded\":{\"songs\":[{\"title\":\"Coffee\",\"_links\":{\"self\":{\"href\":\"http://songs/Coffee\"}}}]},\"_links\":{\"self\":{\"href\":\"http://albums/2\"}}}")
114
+ assert_equal 2, @album.id
115
+ assert_equal "Coffee", @album.songs.first.title
116
+ assert_equal "http://songs/Coffee", @album.songs.first.links[:self].href
117
+ assert_equal "http://albums/2", @album.links[:self].href
118
+ end
119
+
120
+ it "doesn't require _links and _embedded to be present" do
121
+ @album.from_json("{\"id\":2}")
122
+ assert_equal 2, @album.id
123
+
124
+ # in newer representables, this is not overwritten to an empty [] anymore.
125
+ assert_equal ["Beer"], @album.songs.map(&:title)
126
+ @album.links.must_equal nil
127
+ end
128
+ end
129
+ end
130
+
131
+ class LinkCollectionTest < MiniTest::Spec
132
+ subject { Roar::JSON::HAL::LinkCollection.new([:self, "next"]) }
133
+ describe "#is_array?" do
134
+ it "returns true for array link" do
135
+ subject.is_array?(:self).must_equal true
136
+ subject.is_array?("self").must_equal true
137
+ end
138
+
139
+ it "returns false otherwise" do
140
+ subject.is_array?("prev").must_equal false
141
+ end
142
+ end
143
+ end
144
+
145
+
146
+ class HalCurieTest < MiniTest::Spec
147
+ representer!([Roar::JSON::HAL]) do
148
+ link "doc:self" do
149
+ "/"
150
+ end
151
+
152
+ curies do
153
+ [{:name => :doc,
154
+ :href => "//docs/{rel}",
155
+ :templated => true}]
156
+ end
157
+ end
158
+
159
+ it { Object.new.extend(rpr).to_hash.must_equal({"_links"=>{"doc:self"=>{"href"=>"/"}, :curies=>[{"name"=>:doc, "href"=>"//docs/{rel}", "templated"=>true}]}}) }
160
+ end
@@ -0,0 +1,31 @@
1
+ require 'ostruct'
2
+ require 'test_helper'
3
+ require 'roar/json/hal'
4
+
5
+
6
+ class HalLinkTest < MiniTest::Spec
7
+ let(:rpr) do
8
+ Module.new do
9
+ include Roar::JSON
10
+ include Roar::JSON::HAL::Links
11
+ link :self do
12
+ "//songs"
13
+ end
14
+ end
15
+ end
16
+
17
+ subject { Object.new.extend(rpr) }
18
+
19
+ describe "#to_json" do
20
+ it "uses 'links' key" do
21
+ subject.to_json.must_equal "{\"links\":{\"self\":{\"href\":\"//songs\"}}}"
22
+ end
23
+ end
24
+
25
+ describe "#from_json" do
26
+ it "uses 'links' key" do
27
+ subject.from_json("{\"links\":{\"self\":{\"href\":\"//lifer\"}}}").links.values.must_equal [link("href" => "//lifer", "rel" => "self")]
28
+ end
29
+ end
30
+ end
31
+