rest-client-next 1.1.0 → 1.3.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,75 +1,99 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
3
6
  describe RestClient::Resource do
4
- before do
5
- @resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
6
- end
7
-
8
- context "Resource delegation" do
9
- it "GET" do
10
- RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
11
- @resource.get
12
- end
13
-
14
- it "POST" do
15
- RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
16
- @resource.post 'abc', :content_type => 'image/jpg'
17
- end
18
-
19
- it "PUT" do
20
- RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
21
- @resource.put 'abc', :content_type => 'image/jpg'
22
- end
23
-
24
- it "DELETE" do
25
- RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
26
- @resource.delete
27
- end
28
-
29
- it "overrides resource headers" do
30
- RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
31
- @resource.get 'X-Something' => '2'
32
- end
33
- end
34
-
35
- it "can instantiate with no user/password" do
36
- @resource = RestClient::Resource.new('http://some/resource')
37
- end
38
-
39
- it "is backwards compatible with previous constructor" do
40
- @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
41
- @resource.user.should == 'user'
42
- @resource.password.should == 'pass'
43
- end
44
-
45
- it "concatenates urls, inserting a slash when it needs one" do
46
- @resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
47
- end
48
-
49
- it "concatenates urls, using no slash if the first url ends with a slash" do
50
- @resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
51
- end
52
-
53
- it "concatenates urls, using no slash if the second url starts with a slash" do
54
- @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
55
- end
56
-
57
- it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
58
- @resource.concat_urls(:posts, 1).should == 'posts/1'
59
- end
60
-
61
- it "offers subresources via []" do
62
- parent = RestClient::Resource.new('http://example.com')
63
- parent['posts'].url.should == 'http://example.com/posts'
64
- end
65
-
66
- it "transports options to subresources" do
67
- parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
68
- parent['posts'].user.should == 'user'
69
- parent['posts'].password.should == 'password'
70
- end
71
-
72
- it "prints its url with to_s" do
73
- RestClient::Resource.new('x').to_s.should == 'x'
74
- end
7
+ before do
8
+ @resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
9
+ end
10
+
11
+ context "Resource delegation" do
12
+ it "GET" do
13
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
14
+ @resource.get
15
+ end
16
+
17
+ it "POST" do
18
+ RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
19
+ @resource.post 'abc', :content_type => 'image/jpg'
20
+ end
21
+
22
+ it "PUT" do
23
+ RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
24
+ @resource.put 'abc', :content_type => 'image/jpg'
25
+ end
26
+
27
+ it "DELETE" do
28
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
29
+ @resource.delete
30
+ end
31
+
32
+ it "overrides resource headers" do
33
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
34
+ @resource.get 'X-Something' => '2'
35
+ end
36
+ end
37
+
38
+ it "can instantiate with no user/password" do
39
+ @resource = RestClient::Resource.new('http://some/resource')
40
+ end
41
+
42
+ it "is backwards compatible with previous constructor" do
43
+ @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
44
+ @resource.user.should == 'user'
45
+ @resource.password.should == 'pass'
46
+ end
47
+
48
+ it "concatenates urls, inserting a slash when it needs one" do
49
+ @resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
50
+ end
51
+
52
+ it "concatenates urls, using no slash if the first url ends with a slash" do
53
+ @resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
54
+ end
55
+
56
+ it "concatenates urls, using no slash if the second url starts with a slash" do
57
+ @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
58
+ end
59
+
60
+ it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
61
+ @resource.concat_urls(:posts, 1).should == 'posts/1'
62
+ end
63
+
64
+ it "offers subresources via []" do
65
+ parent = RestClient::Resource.new('http://example.com')
66
+ parent['posts'].url.should == 'http://example.com/posts'
67
+ end
68
+
69
+ it "transports options to subresources" do
70
+ parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
71
+ parent['posts'].user.should == 'user'
72
+ parent['posts'].password.should == 'password'
73
+ end
74
+
75
+ it "prints its url with to_s" do
76
+ RestClient::Resource.new('x').to_s.should == 'x'
77
+ end
78
+
79
+ describe 'block' do
80
+ it 'can use block when creating the resource' do
81
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
82
+ resource = RestClient::Resource.new('www.example.com'){|response| 'foo'}
83
+ resource.get.should == 'foo'
84
+ end
85
+
86
+ it 'can use block when executing the resource' do
87
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
88
+ resource = RestClient::Resource.new('www.example.com')
89
+ resource.get{|response| 'foo'}.should == 'foo'
90
+ end
91
+
92
+ it 'execution block override resource block' do
93
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
94
+ resource = RestClient::Resource.new('www.example.com'){|response| 'foo'}
95
+ resource.get{|response| 'bar'}.should == 'bar'
96
+ end
97
+
98
+ end
75
99
  end
@@ -1,21 +1,72 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
3
  describe RestClient::Response do
4
- before do
5
- @net_http_res = mock('net http response', :to_hash => {"Status" => ["200 OK"]})
6
- @response = RestClient::Response.new('abc', @net_http_res)
7
- end
8
-
9
- it "behaves like string" do
10
- @response.should == 'abc'
11
- end
12
-
13
- it "accepts nil strings and sets it to empty for the case of HEAD" do
14
- RestClient::Response.new(nil, @net_http_res).should == ""
15
- end
16
-
17
- it "test headers and raw headers" do
18
- @response.raw_headers["Status"][0].should == "200 OK"
19
- @response.headers[:status].should == "200 OK"
20
- end
4
+ before do
5
+ @net_http_res = mock('net http response', :to_hash => {"Status" => ["200 OK"]})
6
+ @response = RestClient::Response.new('abc', @net_http_res)
7
+ end
8
+
9
+ it "behaves like string" do
10
+ @response.should == 'abc'
11
+ end
12
+
13
+ it "accepts nil strings and sets it to empty for the case of HEAD" do
14
+ RestClient::Response.new(nil, @net_http_res).should == ""
15
+ end
16
+
17
+ it "test headers and raw headers" do
18
+ @response.raw_headers["Status"][0].should == "200 OK"
19
+ @response.headers[:status].should == "200 OK"
20
+ end
21
+
22
+ describe "cookie processing" do
23
+ it "should correctly deal with one Set-Cookie header with one cookie inside" do
24
+ net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
25
+ response = RestClient::Response.new('abc', net_http_res)
26
+ response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
27
+ response.cookies.should == { "main_page" => "main_page_no_rewrite" }
28
+ end
29
+
30
+ it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
31
+ net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
32
+ response = RestClient::Response.new('abc', net_http_res)
33
+ response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
34
+ response.cookies.should == {
35
+ "main_page" => "main_page_no_rewrite",
36
+ "remember_me" => "",
37
+ "user" => "somebody"
38
+ }
39
+ end
40
+
41
+ it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
42
+ net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
43
+ response = RestClient::Response.new('abc', net_http_res)
44
+ response.cookies.should == {
45
+ "main_page" => "main_page_no_rewrite",
46
+ "remember_me" => "",
47
+ "user" => "somebody"
48
+ }
49
+ end
50
+ end
51
+
52
+ describe "exceptions processing" do
53
+ it "should return itself for normal codes" do
54
+ (200..206).each do |code|
55
+ net_http_res = mock('net http response', :code => '200')
56
+ response = RestClient::Response.new('abc', net_http_res)
57
+ response.return!
58
+ end
59
+ end
60
+
61
+ it "should throw an exception for other codes" do
62
+ RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
63
+ net_http_res = mock('net http response', :code => code.to_i)
64
+ response = RestClient::Response.new('abc', net_http_res)
65
+ lambda { response.return!}.should raise_error
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+
21
72
  end
@@ -1,53 +1,63 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
3
  describe RestClient do
4
- describe "API" do
5
- it "GET" do
6
- RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
7
- RestClient.get('http://some/resource')
8
- end
9
-
10
- it "POST" do
11
- RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
12
- RestClient.post('http://some/resource', 'payload')
13
- end
14
-
15
- it "PUT" do
16
- RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
17
- RestClient.put('http://some/resource', 'payload')
18
- end
19
-
20
- it "DELETE" do
21
- RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
22
- RestClient.delete('http://some/resource')
23
- end
24
-
25
- it "HEAD" do
26
- RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
27
- RestClient.head('http://some/resource')
28
- end
29
- end
30
-
31
- describe "logging" do
32
- after do
33
- RestClient.log = nil
34
- end
35
-
36
- it "gets the log source from the RESTCLIENT_LOG environment variable" do
37
- ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return('from env')
38
- RestClient.log = 'from class method'
39
- RestClient.log.should == 'from env'
40
- end
41
-
42
- it "sets a destination for log output, used if no environment variable is set" do
43
- ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
44
- RestClient.log = 'from class method'
45
- RestClient.log.should == 'from class method'
46
- end
47
-
48
- it "returns nil (no logging) if neither are set (default)" do
49
- ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
50
- RestClient.log.should == nil
51
- end
52
- end
4
+ describe "API" do
5
+ it "GET" do
6
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
7
+ RestClient.get('http://some/resource')
8
+ end
9
+
10
+ it "POST" do
11
+ RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
12
+ RestClient.post('http://some/resource', 'payload')
13
+ end
14
+
15
+ it "PUT" do
16
+ RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
17
+ RestClient.put('http://some/resource', 'payload')
18
+ end
19
+
20
+ it "DELETE" do
21
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
22
+ RestClient.delete('http://some/resource')
23
+ end
24
+
25
+ it "HEAD" do
26
+ RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
27
+ RestClient.head('http://some/resource')
28
+ end
29
+ end
30
+
31
+ describe "logging" do
32
+ after do
33
+ RestClient.log = nil
34
+ end
35
+
36
+ it "uses << if the log is not a string" do
37
+ log = RestClient.log = []
38
+ log.should_receive(:<<).with('xyz')
39
+ RestClient.log << 'xyz'
40
+ end
41
+
42
+ it "displays the log to stdout" do
43
+ RestClient.log = 'stdout'
44
+ STDOUT.should_receive(:puts).with('xyz')
45
+ RestClient.log << 'xyz'
46
+ end
47
+
48
+ it "displays the log to stderr" do
49
+ RestClient.log = 'stderr'
50
+ STDERR.should_receive(:puts).with('xyz')
51
+ RestClient.log << 'xyz'
52
+ end
53
+
54
+ it "append the log to the requested filename" do
55
+ RestClient.log = '/tmp/restclient.log'
56
+ f = mock('file handle')
57
+ File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
58
+ f.should_receive(:puts).with('xyz')
59
+ RestClient.log << 'xyz'
60
+ end
61
+ end
62
+
53
63
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client-next
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
8
- - Archiloque
8
+ - Julien Kirch
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-12-28 00:00:00 +01:00
13
+ date: 2010-01-25 00:00:00 +01:00
14
14
  default_executable: restclient
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -23,7 +23,7 @@ dependencies:
23
23
  - !ruby/object:Gem::Version
24
24
  version: "1.16"
25
25
  version:
26
- description: "A simple REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
26
+ description: "A simple Simple HTTP and REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
27
27
  email: rest.client@librelist.com
28
28
  executables:
29
29
  - restclient
@@ -31,6 +31,7 @@ extensions: []
31
31
 
32
32
  extra_rdoc_files:
33
33
  - README.rdoc
34
+ - history.md
34
35
  files:
35
36
  - README.rdoc
36
37
  - Rakefile
@@ -48,6 +49,7 @@ files:
48
49
  - lib/restclient/response.rb
49
50
  - spec/base.rb
50
51
  - spec/exceptions_spec.rb
52
+ - spec/integration_spec.rb
51
53
  - spec/master_shake.jpg
52
54
  - spec/mixin/response_spec.rb
53
55
  - spec/payload_spec.rb
@@ -56,6 +58,7 @@ files:
56
58
  - spec/resource_spec.rb
57
59
  - spec/response_spec.rb
58
60
  - spec/restclient_spec.rb
61
+ - history.md
59
62
  has_rdoc: true
60
63
  homepage: http://github.com/archiloque/rest-client
61
64
  licenses: []
@@ -79,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
82
  version:
80
83
  requirements: []
81
84
 
82
- rubyforge_project: rest-client-multipart
85
+ rubyforge_project: rest-client
83
86
  rubygems_version: 1.3.5
84
87
  signing_key:
85
88
  specification_version: 3
@@ -87,6 +90,7 @@ summary: Simple REST client for Ruby, inspired by microframework syntax for spec
87
90
  test_files:
88
91
  - spec/base.rb
89
92
  - spec/exceptions_spec.rb
93
+ - spec/integration_spec.rb
90
94
  - spec/mixin/response_spec.rb
91
95
  - spec/payload_spec.rb
92
96
  - spec/raw_response_spec.rb