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,134 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+ require 'timeout'
4
+
5
+ # Helps you spinning up and shutting down your own sinatra app. This is especially helpful for running
6
+ # real network tests against a sinatra backend.
7
+ #
8
+ # The backend server could look like the following (in test/server.rb).
9
+ #
10
+ # require "sinatra"
11
+ #
12
+ # get "/" do
13
+ # "Cheers from test server"
14
+ # end
15
+ #
16
+ # get "/ping" do
17
+ # "1"
18
+ # end
19
+ #
20
+ # Note that you need to implement a ping action for internal use.
21
+ #
22
+ # Next, you need to write your runner.
23
+ #
24
+ # require 'sinatra/runner'
25
+ #
26
+ # class Runner < Sinatra::Runner
27
+ # def app_file
28
+ # File.expand_path("../server.rb", __FILE__)
29
+ # end
30
+ # end
31
+ #
32
+ # Override Runner#app_file, #command, #port, #protocol and #ping_path for customization.
33
+ #
34
+ # Whereever you need this test backend, here's how you manage it. The following example assumes you
35
+ # have a test in your app that needs to be run against your test backend.
36
+ #
37
+ # runner = ServerRunner.new
38
+ # runner.run
39
+ #
40
+ # # ..tests against localhost:4567 here..
41
+ #
42
+ # runner.kill
43
+ #
44
+ # For an example, check https://github.com/apotonick/roar/blob/master/test/test_helper.rb
45
+ module Sinatra
46
+ class Runner
47
+ def app_file
48
+ File.expand_path("../server.rb", __FILE__)
49
+ end
50
+
51
+ def run
52
+ #puts command
53
+ @pipe = start
54
+ @started = Time.now
55
+ warn "#{server} up and running on port #{port}" if ping
56
+ end
57
+
58
+ def kill
59
+ return unless pipe
60
+ Process.kill("KILL", pipe.pid)
61
+ rescue NotImplementedError
62
+ system "kill -9 #{pipe.pid}"
63
+ rescue Errno::ESRCH
64
+ end
65
+
66
+ def get(url)
67
+ Timeout.timeout(1) { get_url("#{protocol}://127.0.0.1:#{port}#{url}") }
68
+ end
69
+
70
+ def log
71
+ @log ||= ""
72
+ loop { @log << pipe.read_nonblock(1) }
73
+ rescue Exception
74
+ @log
75
+ end
76
+
77
+ private
78
+ attr_accessor :pipe
79
+
80
+ def start
81
+ IO.popen(command)
82
+ end
83
+
84
+ def command # to be overwritten
85
+ "bundle exec ruby #{app_file} -p #{port} -e production"
86
+ end
87
+
88
+ def ping(timeout=30)
89
+ loop do
90
+ return if alive?
91
+ if Time.now - @started > timeout
92
+ $stderr.puts command, log
93
+ fail "timeout"
94
+ else
95
+ sleep 0.1
96
+ end
97
+ end
98
+ end
99
+
100
+ def alive?
101
+ 3.times { get(ping_path) }
102
+ true
103
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, SystemCallError, OpenURI::HTTPError, Timeout::Error
104
+ false
105
+ end
106
+
107
+ def ping_path # to be overwritten
108
+ '/ping'
109
+ end
110
+
111
+ def port # to be overwritten
112
+ 4567
113
+ end
114
+
115
+ def protocol
116
+ "http"
117
+ end
118
+
119
+ def get_url(url)
120
+ uri = URI.parse(url)
121
+
122
+ return uri.read unless protocol == "https"
123
+ get_https_url(uri)
124
+ end
125
+
126
+ def get_https_url(uri)
127
+ http = Net::HTTP.new(uri.host, uri.port)
128
+ http.use_ssl = true
129
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
130
+ request = Net::HTTP::Get.new(uri.request_uri)
131
+ http.request(request).body
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ require "roar/json/collection"
4
+ require "roar/json/hash"
5
+
6
+ class LonelyCollectionTest < MiniTest::Spec
7
+ it { Roar::JSON::Collection.must_equal Representable::JSON::Collection }
8
+ it { Roar::JSON::Hash.must_equal Representable::JSON::Hash }
9
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+ require 'roar/transport/net_http'
3
+
4
+ class NetHTTPTransportTest < MiniTest::Spec
5
+ let(:url) { "http://localhost:4567/method" }
6
+ let(:body) { "booty" }
7
+ let(:as) { "application/xml" }
8
+ let (:transport) { Roar::Transport::NetHTTP.new }
9
+
10
+ it "#get_uri returns response" do
11
+ transport.get_uri(:uri => url, :as => as).must_match_net_response :get, url, as
12
+ end
13
+
14
+ it "#post_uri returns response" do
15
+ transport.post_uri(:uri => url, :as => as, :body => body).must_match_net_response :post, url, as, body
16
+ end
17
+
18
+ it "#put_uri returns response" do
19
+ transport.put_uri(:uri => url, :as => as, :body => body).must_match_net_response :put, url, as, body
20
+ end
21
+
22
+ it "#delete_uri returns response" do
23
+ transport.delete_uri(:uri => url, :as => as).must_match_net_response :delete, url, as
24
+ end
25
+
26
+ it "#patch_uri returns response" do
27
+ transport.patch_uri(:uri => url, :as => as, :body => body).must_match_net_response :patch, url, as, body
28
+ end
29
+
30
+ it "complains with invalid URL" do
31
+ assert_raises RuntimeError do
32
+ transport.get_uri(:uri => "example.com", :as => as)
33
+ end
34
+ end
35
+
36
+ # TODO: test all verbs.
37
+ describe "request customization" do
38
+ #verbs do |verb|
39
+ verb = "get"
40
+ it "#{verb} yields the request object" do
41
+ transport.send("#{verb}_uri", :uri => "http://localhost:4567/cookies", :as => "application/json") do |req|
42
+ req.add_field("Cookie", "Yumyum")
43
+ end.body.must_equal %{{"name": "Bodyjar"}}
44
+ end
45
+ #end
46
+ end
47
+
48
+ describe "basic auth" do
49
+ it "raises when no credentials provided" do
50
+ exception = assert_raises Roar::Transport::Error do
51
+ transport.get_uri(:uri => "http://localhost:4567/protected/bands/bodyjar", :as => "application/json")
52
+ end
53
+
54
+ exception.response.code.must_equal "401"
55
+ end
56
+
57
+ it "raises when wrong credentials provided" do
58
+ exception = assert_raises Roar::Transport::Error do
59
+ transport.get_uri(:uri => "http://localhost:4567/protected/bands/bodyjar", :as => "application/json", :basic_auth => ["admin", "wrong--!!!--password"])
60
+ end
61
+
62
+ exception.response.code.must_equal "401"
63
+ end
64
+
65
+ it "what" do
66
+ transport.get_uri(:uri => "http://localhost:4567/protected/bands/bodyjar", :as => "application/json", :basic_auth => ["admin", "password"])
67
+ end
68
+ end
69
+ end
70
+
71
+ module MiniTest::Assertions
72
+ def assert_net_response(type, response, url, as, body = nil)
73
+ # TODO: Assert headers
74
+ assert_equal "<method>#{type}#{(' - ' + body) if body}</method>", response.body
75
+ end
76
+ end
77
+
78
+ Net::HTTPOK.infect_an_assertion :assert_net_response, :must_match_net_response
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class RepresenterTest < MiniTest::Spec
4
+ describe "Representer" do
5
+ before do
6
+ @c = Class.new do
7
+ include Roar::Representer
8
+ end
9
+ end
10
+
11
+ it "aliases #representable_property to #property" do
12
+ @c.property :title
13
+ assert_equal "title", @c.representable_attrs.first.name
14
+ end
15
+
16
+ it "aliases #representable_collection to #collection" do
17
+ @c.collection :songs
18
+ assert_equal "songs", @c.representable_attrs.first.name
19
+ end
20
+ end
21
+
22
+ describe "Inheritance" do
23
+ it "properly inherits properties from modules" do
24
+ module PersonRepresentation
25
+ include Roar::JSON
26
+ property :name
27
+ end
28
+
29
+ class Person
30
+ include AttributesConstructor
31
+ include Roar::JSON
32
+ include PersonRepresentation
33
+ attr_accessor :name
34
+ end
35
+
36
+ assert_equal "{\"name\":\"Paulo\"}", Person.new(:name => "Paulo").to_json
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,102 @@
1
+ require 'test_helper'
2
+
3
+ require "roar"
4
+ require 'roar/transport/net_http/request'
5
+ require 'roar/transport/net_http'
6
+
7
+
8
+ class SslClientCertsTest < MiniTest::Spec
9
+
10
+ describe Roar::Transport::NetHTTP do
11
+
12
+ describe "instance methods" do
13
+
14
+ let(:url) { "http://www.bbc.co.uk" }
15
+ let(:as) { "application/xml" }
16
+
17
+ let(:transport) { Roar::Transport::NetHTTP.new }
18
+
19
+ describe "options passed to the request object (private #call method)" do
20
+
21
+ let(:options) { { uri: url, as: as, pem_file: "test/fixtures/sample.pem", ssl_verify_mode: "ssl_verify_mode" } }
22
+
23
+ describe "option handling" do
24
+
25
+ it "provides all options to the Request object" do
26
+
27
+ request_mock = MiniTest::Mock.new
28
+ request_mock.expect :call, nil, [Net::HTTP::Get]
29
+
30
+ options_assertions = lambda { |argument_options|
31
+ assert_equal argument_options, options
32
+ request_mock
33
+ }
34
+
35
+ Roar::Transport::NetHTTP::Request.stub :new, options_assertions do
36
+ transport.get_uri(options)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ describe Roar::Transport::NetHTTP::Request do
45
+
46
+ describe "instance methods" do
47
+ describe "#initialize" do
48
+
49
+ describe "client certificate configuration" do
50
+
51
+ let(:uri) { URI.parse("http://www.bbc.co.uk") }
52
+ let(:options) { { uri: uri, pem_file: pem_file, ssl_verify_mode: ssl_verify_mode } }
53
+
54
+ let(:request) { Roar::Transport::NetHTTP::Request.new(options) }
55
+ let(:net_http_instance) { request.instance_variable_get(:@http) }
56
+ let(:ssl_verify_mode) { nil }
57
+
58
+ describe "when a pem file has been provided with the request options" do
59
+
60
+ let(:pem_file) { File.expand_path("test/fixtures/sample.pem", Roar.root) }
61
+
62
+ let(:pem) { File.read(pem_file) }
63
+ let(:cert) { OpenSSL::X509::Certificate.new(pem) }
64
+ let(:key) { OpenSSL::PKey::RSA.new(pem) }
65
+
66
+ it "sets the client to use an ssl connection" do
67
+ assert(net_http_instance.use_ssl?, "Net::HTTP connection uses ssl")
68
+ end
69
+
70
+ it "sets the client cert" do
71
+ assert_equal(net_http_instance.cert.to_s, cert.to_s)
72
+ end
73
+ it "sets the client key" do
74
+ assert_equal(net_http_instance.key.to_s, key.to_s)
75
+ end
76
+ it "defaults the verify mode to OpenSSL::SSL::VERIFY_PEER when no option provided" do
77
+ assert_equal(net_http_instance.verify_mode, OpenSSL::SSL::VERIFY_PEER)
78
+ end
79
+
80
+ describe "verify mode is specified" do
81
+
82
+ let(:ssl_verify_mode) { OpenSSL::SSL::VERIFY_NONE }
83
+
84
+ it "sets the client verify mode to that option provided" do
85
+ assert_equal(net_http_instance.verify_mode, ssl_verify_mode)
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "when a pem file has not been provided in the request options" do
91
+
92
+ let(:pem_file) { nil }
93
+
94
+ it "does not set the client to use an ssl connection" do
95
+ refute(net_http_instance.use_ssl?, "Net::HTTP connection do not use SSL")
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,60 @@
1
+ require 'minitest/autorun'
2
+ require 'ostruct'
3
+
4
+ require 'roar/representer'
5
+ require 'roar/http_verbs'
6
+ require 'roar/json/hal'
7
+
8
+ module AttributesConstructor # TODO: remove me.
9
+ def initialize(attrs={})
10
+ attrs.each do |k,v|
11
+ instance_variable_set("@#{k}", v)
12
+ end
13
+ end
14
+ end
15
+
16
+ # FIXME: provide a real #== for OpenStruct.
17
+ class Song < OpenStruct
18
+ def ==(other)
19
+ name == other.name and track == other.track
20
+ end
21
+ end
22
+
23
+ class Album < OpenStruct
24
+ end
25
+
26
+ require "test_xml/mini_test"
27
+ require "roar/xml"
28
+
29
+ require "integration/runner"
30
+
31
+ MiniTest::Spec.class_eval do
32
+ def link(options)
33
+ Roar::Hypermedia::Hyperlink.new(options)
34
+ end
35
+
36
+ def self.representer_for(modules=[Roar::JSON, Roar::Hypermedia], &block)
37
+ let (:rpr) do
38
+ Module.new do
39
+ include *modules.reverse
40
+
41
+ module_exec(&block)
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.representer!(*args, &block)
47
+ representer_for(*args, &block)
48
+ end
49
+
50
+ def self.verbs(&block)
51
+ %w(get post put delete).each(&block)
52
+ end
53
+ end
54
+
55
+
56
+ Roar::Hypermedia::Hyperlink.class_eval do
57
+ def ==(b)
58
+ @attrs == b.instance_variable_get(:@attrs)
59
+ end
60
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class XMLRepresenterFunctionalTest < MiniTest::Spec
4
+ module OrderRepresenter
5
+ include Roar::XML
6
+
7
+ property :id
8
+ self.representation_wrap = :order
9
+ end
10
+
11
+ Order = Struct.new(:id, :items)
12
+
13
+ describe "#to_xml" do
14
+ let (:order) { Order.new(1).extend(OrderRepresenter) }
15
+
16
+ # empty model
17
+ it { Order.new.extend(OrderRepresenter).to_xml.must_equal_xml "<order/>" }
18
+
19
+ # populated model
20
+ it { order.to_xml.must_equal_xml "<order><id>1</id></order>" }
21
+
22
+ # with wrap
23
+ it { order.to_xml(wrap: :rap).must_equal_xml "<rap><id>1</id></rap>" }
24
+
25
+ # aliased to #serialize
26
+ it { order.to_xml.must_equal order.serialize }
27
+
28
+ # accepts options
29
+ it { order.to_xml(exclude: [:id]).must_equal_xml "<order/>" }
30
+ end
31
+
32
+ describe "#from_xml" do
33
+ let (:order) { Order.new.extend(OrderRepresenter) }
34
+
35
+ # parses
36
+ it { order.from_xml("<order><id>1</id></order>").id.must_equal "1" }
37
+
38
+ # aliased to #deserialize
39
+ it { order.deserialize("<order><id>1</id></order>").id.must_equal "1" }
40
+
41
+ # accepts options
42
+ it { order.from_xml("<order><id>1</id></order>", exclude: [:id]).id.must_equal nil }
43
+ end
44
+ end
45
+
46
+ class XmlHyperlinkRepresenterTest < MiniTest::Spec
47
+ describe "API" do
48
+ before do
49
+ @link = Roar::Hypermedia::Hyperlink.new.extend(Roar::XML::HyperlinkRepresenter).from_xml(%{<link rel="self" href="http://roar.apotomo.de" media="web"/>})
50
+ end
51
+
52
+ it "responds to #rel" do
53
+ assert_equal "self", @link.rel
54
+ end
55
+
56
+ it "responds to #href" do
57
+ assert_equal "http://roar.apotomo.de", @link.href
58
+ end
59
+
60
+ it "responds to #media" do
61
+ assert_equal "web", @link.media
62
+ end
63
+
64
+ it "responds to #to_xml" do
65
+ assert_xml_equal %{<link rel=\"self\" href=\"http://roar.apotomo.de\" media="web"/>}, @link.to_xml
66
+ end
67
+ end
68
+ end