restfully 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/README.rdoc +82 -92
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/bin/restfully +10 -46
- data/examples/grid5000.rb +5 -5
- data/lib/restfully.rb +3 -2
- data/lib/restfully/collection.rb +64 -139
- data/lib/restfully/extensions.rb +0 -17
- data/lib/restfully/http/adapters/abstract_adapter.rb +1 -1
- data/lib/restfully/http/adapters/rest_client_adapter.rb +25 -3
- data/lib/restfully/http/request.rb +20 -3
- data/lib/restfully/http/response.rb +21 -3
- data/lib/restfully/link.rb +4 -3
- data/lib/restfully/resource.rb +144 -74
- data/lib/restfully/session.rb +36 -22
- data/restfully.gemspec +9 -4
- data/spec/collection_spec.rb +24 -25
- data/spec/http/request_spec.rb +6 -2
- data/spec/http/response_spec.rb +4 -0
- data/spec/http/rest_client_adapter_spec.rb +3 -2
- data/spec/link_spec.rb +7 -4
- data/spec/resource_spec.rb +31 -56
- data/spec/session_spec.rb +77 -41
- metadata +13 -2
data/spec/session_spec.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__)+'/spec_helper')
|
2
|
-
|
3
2
|
restfully_version = File.read(File.dirname(__FILE__)+'/../VERSION').strip
|
4
3
|
|
5
4
|
include Restfully
|
6
5
|
describe Session do
|
6
|
+
before do
|
7
|
+
@session = Session.new(:base_uri => 'https://api.grid5000.fr/sid/', :root_path => '/grid5000', :user => 'crohr', :password => 'password', :default_headers => {})
|
8
|
+
@request = mock("restfully http request", :uri => mock("uri"), :headers => mock("headers"), :body => nil)
|
9
|
+
@response = mock("restfully http response", :status => 200, :headers => mock("headers"))
|
10
|
+
end
|
7
11
|
describe "initialization" do
|
8
12
|
it "should have a logger reader" do
|
9
13
|
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
@@ -13,25 +17,21 @@ describe Session do
|
|
13
17
|
it "should have a base_uri reader" do
|
14
18
|
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
15
19
|
session.should_not respond_to(:base_uri=)
|
16
|
-
session.base_uri.should == 'https://api.grid5000.fr'
|
17
|
-
end
|
18
|
-
it "should have a root_path reader that returns the path of the root resource, relative to the base URI" do
|
19
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr/sid', 'root_path' => '/grid5000')
|
20
|
-
session.should_not respond_to(:root_path=)
|
21
|
-
session.root_path.should == '/grid5000'
|
20
|
+
session.base_uri.should == URI.parse('https://api.grid5000.fr')
|
22
21
|
end
|
23
|
-
it "should
|
24
|
-
|
25
|
-
session.root_path.should == '/'
|
22
|
+
it "should raise an ArgumentError if the base_uri is not a valid URI" do
|
23
|
+
lambda{Session.new(:base_uri => '/home/crohr/whatever')}.should raise_error(ArgumentError, /\/home\/crohr\/whatever is not a valid URI/)
|
26
24
|
end
|
27
25
|
it "should log to NullLogger by default" do
|
28
|
-
NullLogger.should_receive(:new).and_return(logger = mock(NullLogger, :debug => Proc.new{}))
|
26
|
+
NullLogger.should_receive(:new).and_return(logger = mock(NullLogger, :debug => Proc.new{}, :level => Logger::WARN))
|
27
|
+
logger.should_receive(:level=).with(Logger::WARN)
|
29
28
|
session = Session.new(:base_uri => 'https://api.grid5000.fr')
|
30
29
|
session.logger.should == logger
|
31
30
|
end
|
32
31
|
it "should use the given logger" do
|
33
32
|
logger = mock("custom logger", :debug => Proc.new{})
|
34
|
-
|
33
|
+
logger.should_receive(:level=).with(Logger::DEBUG)
|
34
|
+
session = Session.new(:base_uri => 'https://api.grid5000.fr', 'logger' => logger, :verbose => true)
|
35
35
|
session.logger.should == logger
|
36
36
|
end
|
37
37
|
it "should set a default Accept HTTP header if no default headers are given" do
|
@@ -49,17 +49,18 @@ describe Session do
|
|
49
49
|
}
|
50
50
|
end
|
51
51
|
it "should correctly initialize the connection" do
|
52
|
-
mock_logger = mock("logger", :debug => Proc.new{})
|
52
|
+
mock_logger = mock("logger", :debug => Proc.new{}, :level => Logger::DEBUG)
|
53
|
+
mock_logger.stub!(:level=)
|
53
54
|
Restfully.adapter.should_receive(:new).with('https://api.grid5000.fr/sid', :user => 'crohr', :password => 'password', :logger => mock_logger).and_return(connection = mock("restfully connection"))
|
54
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr/sid',
|
55
|
+
session = Session.new(:base_uri => 'https://api.grid5000.fr/sid', :user => 'crohr', :password => 'password', :logger => mock_logger)
|
55
56
|
session.connection.should == connection
|
56
57
|
end
|
57
58
|
|
58
59
|
it "should initialize the root resource" do
|
59
|
-
session = Session.new(:base_uri => 'https://api.grid5000.fr
|
60
|
+
session = Session.new(:base_uri => 'https://api.grid5000.fr/grid5000', :user => 'crohr', :password => 'password')
|
60
61
|
session.root.should be_a Restfully::Resource
|
61
|
-
session.root.uri.to_s.should == '/grid5000'
|
62
|
-
session.root.
|
62
|
+
session.root.uri.to_s.should == 'https://api.grid5000.fr/grid5000'
|
63
|
+
session.root.uri.path.should == '/grid5000'
|
63
64
|
end
|
64
65
|
|
65
66
|
it "should yield the loaded root resource and the session object" do
|
@@ -72,37 +73,72 @@ describe Session do
|
|
72
73
|
end
|
73
74
|
end
|
74
75
|
|
75
|
-
describe "
|
76
|
-
before
|
77
|
-
@session = Session.new(:base_uri => 'https://api.grid5000.fr/sid', :root_path => '/grid5000', :user => 'crohr', :password => 'password', :default_headers => {})
|
78
|
-
@request = mock("restfully http request", :uri => mock("uri"), :headers => mock("headers"))
|
76
|
+
describe "Transmitting requests" do
|
77
|
+
before do
|
79
78
|
@request.should_receive(:add_headers).with("User-Agent"=>"Restfully/#{restfully_version}", "Accept"=>"application/json")
|
80
|
-
@response = mock("restfully http response", :status => 200, :headers => mock("headers"))
|
81
79
|
end
|
82
|
-
it "should
|
83
|
-
|
84
|
-
@session.
|
85
|
-
@session.
|
80
|
+
it "should send a head" do
|
81
|
+
@session.connection.should_receive(:head).with(@request).and_return(@response)
|
82
|
+
@session.should_receive(:deal_with_eventual_errors).with(@response, @request).and_return(@response)
|
83
|
+
@session.send(:transmit, :head, @request).should == @response
|
86
84
|
end
|
87
|
-
it "should
|
88
|
-
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('http://somehost.com/some/path'), :headers => {}, :query => {}).and_return(@request)
|
85
|
+
it "should send a get" do
|
89
86
|
@session.connection.should_receive(:get).with(@request).and_return(@response)
|
90
|
-
@session.
|
87
|
+
@session.should_receive(:deal_with_eventual_errors).with(@response, @request).and_return(@response)
|
88
|
+
@session.send(:transmit, :get, @request).should == @response
|
91
89
|
end
|
92
|
-
it "should
|
93
|
-
|
94
|
-
@session.
|
95
|
-
@session.
|
90
|
+
it "should send a post" do
|
91
|
+
@session.connection.should_receive(:post).with(@request).and_return(@response)
|
92
|
+
@session.should_receive(:deal_with_eventual_errors).with(@response, @request).and_return(@response)
|
93
|
+
@session.send(:transmit, :post, @request).should == @response
|
96
94
|
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "URI construction" do
|
98
|
+
it "should not use the base_uri if the given path is a complete URI" do
|
99
|
+
@session.uri_for('http://somehost.com/some/path').should == URI.parse('http://somehost.com/some/path')
|
100
|
+
end
|
101
|
+
it "should combine the base_uri with the given path (absolute) if it is not a complete URI" do
|
102
|
+
@session.uri_for('/some/path').should == URI.parse('https://api.grid5000.fr/some/path')
|
103
|
+
end
|
104
|
+
it "should combine the base_uri with the given path (relative) if it is not a complete URI" do
|
105
|
+
@session.uri_for('some/path').should == URI.parse('https://api.grid5000.fr/sid/some/path')
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "Dealing with errors" do
|
97
110
|
it "should raise a Restfully::HTTP::ClientError error on 4xx errors" do
|
98
|
-
|
99
|
-
@session.
|
100
|
-
|
101
|
-
|
102
|
-
it "should raise a Restfully::HTTP::ServerError error on
|
103
|
-
|
104
|
-
@session.
|
105
|
-
|
111
|
+
response = mock("404 response", :status => 404, :body => {'code' => 404, 'message' => 'The requested resource cannot be found.', 'title' => 'Not Found'}, :headers => mock("headers"))
|
112
|
+
lambda{ @session.send(:deal_with_eventual_errors, response, @request) }.should raise_error(Restfully::HTTP::ClientError, "404 Not Found. The requested resource cannot be found.")
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should raise a Restfully::HTTP::ServerError error on 5xx errors" do
|
116
|
+
response = mock("500 response", :status => 500, :body => {'code' => 500, 'message' => 'Something went wrong.', 'title' => 'Internal Server Error'}, :headers => mock("headers"))
|
117
|
+
lambda{ @session.send(:deal_with_eventual_errors, response, @request) }.should raise_error(Restfully::HTTP::ServerError, "500 Internal Server Error. Something went wrong.")
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "HEADing resources" do
|
122
|
+
it "should create a new Request object and transmit it" do
|
123
|
+
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :headers => nil, :query => nil).and_return(@request)
|
124
|
+
@session.should_receive(:transmit).with(:head, @request)
|
125
|
+
@session.head('some/path')
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "GETting resources" do
|
130
|
+
it "should create a new Request object and transmit it" do
|
131
|
+
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :headers => {:cache_control => 'max-age=0'}, :query => nil).and_return(@request)
|
132
|
+
@session.should_receive(:transmit).with(:get, @request)
|
133
|
+
@session.get('some/path', 'headers' => {:cache_control => 'max-age=0'})
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "POSTing payload" do
|
138
|
+
it "should create a new Request object with a body, and transmit it" do
|
139
|
+
Restfully::HTTP::Request.should_receive(:new).with(URI.parse('https://api.grid5000.fr/sid/some/path'), :body => '{"a":"b"}', :headers => {:content_type => 'application/json'}, :query => nil).and_return(@request)
|
140
|
+
@session.should_receive(:transmit).with(:post, @request)
|
141
|
+
@session.post('some/path', {"a" => "b"}.to_json, 'headers' => {:content_type => 'application/json'})
|
106
142
|
end
|
107
143
|
end
|
108
144
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restfully
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Rohr
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-16 00:00:00 +01:00
|
13
13
|
default_executable: restfully
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,6 +22,16 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "1.0"
|
24
24
|
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: backports
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
25
35
|
description: Experimental code for auto-generation of wrappers on top of RESTful APIs that follow HATEOAS principles and provide OPTIONS methods and/or Allow headers.
|
26
36
|
email: cyril.rohr@gmail.com
|
27
37
|
executables:
|
@@ -34,6 +44,7 @@ extra_rdoc_files:
|
|
34
44
|
files:
|
35
45
|
- .document
|
36
46
|
- .gitignore
|
47
|
+
- CHANGELOG
|
37
48
|
- LICENSE
|
38
49
|
- README.rdoc
|
39
50
|
- Rakefile
|