rest-client 1.6.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.

Potentially problematic release.


This version of rest-client might be problematic. Click here for more details.

@@ -0,0 +1,99 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
6
+ describe RestClient::Resource do
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, request| '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, request| '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, request| 'foo' }
95
+ resource.get { |response, request| 'bar' }.should == 'bar'
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,151 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
6
+ describe RestClient::Response do
7
+ before do
8
+ @net_http_res = mock('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
9
+ @request = mock('http request', :user => nil, :password => nil)
10
+ @response = RestClient::Response.create('abc', @net_http_res, {})
11
+ end
12
+
13
+ it "behaves like string" do
14
+ @response.should.to_s == 'abc'
15
+ @response.to_str.should == 'abc'
16
+ @response.to_i.should == 200
17
+ end
18
+
19
+ it "accepts nil strings and sets it to empty for the case of HEAD" do
20
+ RestClient::Response.create(nil, @net_http_res, {}).should.to_s == ""
21
+ end
22
+
23
+ it "test headers and raw headers" do
24
+ @response.raw_headers["Status"][0].should == "200 OK"
25
+ @response.headers[:status].should == "200 OK"
26
+ end
27
+
28
+ describe "cookie processing" do
29
+ it "should correctly deal with one Set-Cookie header with one cookie inside" do
30
+ 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"]})
31
+ response = RestClient::Response.create('abc', net_http_res, {})
32
+ response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
33
+ response.cookies.should == { "main_page" => "main_page_no_rewrite" }
34
+ end
35
+
36
+ it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
37
+ 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"]})
38
+ response = RestClient::Response.create('abc', net_http_res, {})
39
+ 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"]
40
+ response.cookies.should == {
41
+ "main_page" => "main_page_no_rewrite",
42
+ "remember_me" => "",
43
+ "user" => "somebody"
44
+ }
45
+ end
46
+
47
+ it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
48
+ 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"]})
49
+ response = RestClient::Response.create('abc', net_http_res, {})
50
+ response.cookies.should == {
51
+ "main_page" => "main_page_no_rewrite",
52
+ "remember_me" => "",
53
+ "user" => "somebody"
54
+ }
55
+ end
56
+ end
57
+
58
+ describe "exceptions processing" do
59
+ it "should return itself for normal codes" do
60
+ (200..206).each do |code|
61
+ net_http_res = mock('net http response', :code => '200')
62
+ response = RestClient::Response.create('abc', net_http_res, {})
63
+ response.return! @request
64
+ end
65
+ end
66
+
67
+ it "should throw an exception for other codes" do
68
+ RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
69
+ unless (200..207).include? code
70
+ net_http_res = mock('net http response', :code => code.to_i)
71
+ response = RestClient::Response.create('abc', net_http_res, {})
72
+ lambda { response.return!}.should raise_error
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ describe "redirection" do
80
+
81
+ it "follows a redirection when the request is a get" do
82
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
83
+ stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
84
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
85
+ end
86
+
87
+ it "follows a redirection and keep the parameters" do
88
+ stub_request(:get, 'http://foo:bar@some/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
89
+ stub_request(:get, 'http://foo:bar@new/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => 'Foo')
90
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should == 'Foo'
91
+ end
92
+
93
+ it "doesn't follow a 301 when the request is a post" do
94
+ net_http_res = mock('net http response', :code => 301)
95
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post})
96
+ lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
97
+ end
98
+
99
+ it "doesn't follow a 302 when the request is a post" do
100
+ net_http_res = mock('net http response', :code => 302)
101
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post})
102
+ lambda { response.return!(@request)}.should raise_error(RestClient::Found)
103
+ end
104
+
105
+ it "doesn't follow a 307 when the request is a post" do
106
+ net_http_res = mock('net http response', :code => 307)
107
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post})
108
+ lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
109
+ end
110
+
111
+ it "doesn't follow a redirection when the request is a put" do
112
+ net_http_res = mock('net http response', :code => 301)
113
+ response = RestClient::Response.create('abc', net_http_res, {:method => :put})
114
+ lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
115
+ end
116
+
117
+ it "follows a redirection when the request is a post and result is a 303" do
118
+ stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
119
+ stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
120
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should == 'Foo'
121
+ end
122
+
123
+ it "follows a redirection when the request is a head" do
124
+ stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
125
+ stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
126
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should == 'Foo'
127
+ end
128
+
129
+ it "handles redirects with relative paths" do
130
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
131
+ stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
132
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
133
+ end
134
+
135
+ it "handles redirects with relative path and query string" do
136
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
137
+ stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
138
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
139
+ end
140
+
141
+ it "follow a redirection when the request is a get and the response is in the 30x range" do
142
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
143
+ stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
144
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
145
+ end
146
+
147
+
148
+ end
149
+
150
+
151
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
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 "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
+
63
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 6
9
+ - 0
10
+ version: 1.6.0
11
+ platform: ruby
12
+ authors:
13
+ - Adam Wiggins
14
+ - Julien Kirch
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-06-28 00:00:00 +02:00
20
+ default_executable: restclient
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: mime-types
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 47
31
+ segments:
32
+ - 1
33
+ - 16
34
+ version: "1.16"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: "A simple Simple HTTP and REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
38
+ email: rest.client@librelist.com
39
+ executables:
40
+ - restclient
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.rdoc
45
+ - history.md
46
+ files:
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - bin/restclient
51
+ - lib/rest_client.rb
52
+ - lib/rest-client.rb
53
+ - lib/restclient.rb
54
+ - lib/restclient/exceptions.rb
55
+ - lib/restclient/abstract_response.rb
56
+ - lib/restclient/net_http_ext.rb
57
+ - lib/restclient/payload.rb
58
+ - lib/restclient/raw_response.rb
59
+ - lib/restclient/request.rb
60
+ - lib/restclient/resource.rb
61
+ - lib/restclient/response.rb
62
+ - spec/base.rb
63
+ - spec/exceptions_spec.rb
64
+ - spec/integration_spec.rb
65
+ - spec/master_shake.jpg
66
+ - spec/abstract_response_spec.rb
67
+ - spec/payload_spec.rb
68
+ - spec/raw_response_spec.rb
69
+ - spec/request_spec.rb
70
+ - spec/request2_spec.rb
71
+ - spec/resource_spec.rb
72
+ - spec/response_spec.rb
73
+ - spec/restclient_spec.rb
74
+ - spec/integration/certs/equifax.crt
75
+ - spec/integration/certs/verisign.crt
76
+ - spec/integration/request_spec.rb
77
+ - history.md
78
+ has_rdoc: true
79
+ homepage: http://github.com/archiloque/rest-client
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project: rest-client
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Simple REST client for Ruby, inspired by microframework syntax for specifying actions.
112
+ test_files:
113
+ - spec/base.rb
114
+ - spec/exceptions_spec.rb
115
+ - spec/integration_spec.rb
116
+ - spec/abstract_response_spec.rb
117
+ - spec/payload_spec.rb
118
+ - spec/raw_response_spec.rb
119
+ - spec/request_spec.rb
120
+ - spec/request2_spec.rb
121
+ - spec/resource_spec.rb
122
+ - spec/response_spec.rb
123
+ - spec/restclient_spec.rb
124
+ - spec/integration/request_spec.rb