rest-client-next-dshelf 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ require File.join( File.dirname(File.expand_path(__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 "HEAD" do
18
+ RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
19
+ @resource.head
20
+ end
21
+
22
+ it "POST" do
23
+ 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')
24
+ @resource.post 'abc', :content_type => 'image/jpg'
25
+ end
26
+
27
+ it "PUT" do
28
+ 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')
29
+ @resource.put 'abc', :content_type => 'image/jpg'
30
+ end
31
+
32
+ it "DELETE" do
33
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
34
+ @resource.delete
35
+ end
36
+
37
+ it "overrides resource headers" do
38
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
39
+ @resource.get 'X-Something' => '2'
40
+ end
41
+ end
42
+
43
+ it "can instantiate with no user/password" do
44
+ @resource = RestClient::Resource.new('http://some/resource')
45
+ end
46
+
47
+ it "is backwards compatible with previous constructor" do
48
+ @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
49
+ @resource.user.should == 'user'
50
+ @resource.password.should == 'pass'
51
+ end
52
+
53
+ it "concatenates urls, inserting a slash when it needs one" do
54
+ @resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
55
+ end
56
+
57
+ it "concatenates urls, using no slash if the first url ends with a slash" do
58
+ @resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
59
+ end
60
+
61
+ it "concatenates urls, using no slash if the second url starts with a slash" do
62
+ @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
63
+ end
64
+
65
+ it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
66
+ @resource.concat_urls(:posts, 1).should == 'posts/1'
67
+ end
68
+
69
+ it "offers subresources via []" do
70
+ parent = RestClient::Resource.new('http://example.com')
71
+ parent['posts'].url.should == 'http://example.com/posts'
72
+ end
73
+
74
+ it "transports options to subresources" do
75
+ parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
76
+ parent['posts'].user.should == 'user'
77
+ parent['posts'].password.should == 'password'
78
+ end
79
+
80
+ it "passes a given block to subresources" do
81
+ block = Proc.new{|r| r}
82
+ parent = RestClient::Resource.new('http://example.com', &block)
83
+ parent['posts'].block.should == block
84
+ end
85
+
86
+ it "the block should be overrideable" do
87
+ block1 = Proc.new{|r| r}
88
+ block2 = Proc.new{|r| r}
89
+ parent = RestClient::Resource.new('http://example.com', &block1)
90
+ # parent['posts', &block2].block.should == block2 # ruby 1.9 syntax
91
+ parent.send(:[], 'posts', &block2).block.should == block2
92
+ end
93
+
94
+ it "the block should be overrideable in ruby 1.9 syntax" do
95
+ block = Proc.new{|r| r}
96
+ parent = RestClient::Resource.new('http://example.com', &block)
97
+ r19_syntax = %q{
98
+ parent['posts', &->(r){r}].block.should_not == block
99
+ }
100
+ if is_ruby_19?
101
+ eval(r19_syntax)
102
+ end
103
+ end
104
+
105
+ it "prints its url with to_s" do
106
+ RestClient::Resource.new('x').to_s.should == 'x'
107
+ end
108
+
109
+ describe 'block' do
110
+ it 'can use block when creating the resource' do
111
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
112
+ resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
113
+ resource.get.should == 'foo'
114
+ end
115
+
116
+ it 'can use block when executing the resource' do
117
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
118
+ resource = RestClient::Resource.new('www.example.com')
119
+ resource.get { |response, request| 'foo' }.should == 'foo'
120
+ end
121
+
122
+ it 'execution block override resource block' do
123
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
124
+ resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
125
+ resource.get { |response, request| 'bar' }.should == 'bar'
126
+ end
127
+
128
+ end
129
+ end
@@ -0,0 +1,169 @@
1
+ require File.join( File.dirname(File.expand_path(__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 "follows a redirection and keep the cookies" do
94
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => CGI::Cookie.new('Foo', 'Bar'), 'Location' => 'http://new/resource', })
95
+ stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
96
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Qux'
97
+ end
98
+
99
+ it "doesn't follow a 301 when the request is a post" do
100
+ net_http_res = mock('net http response', :code => 301)
101
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post})
102
+ lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
103
+ end
104
+
105
+ it "doesn't follow a 302 when the request is a post" do
106
+ net_http_res = mock('net http response', :code => 302)
107
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post})
108
+ lambda { response.return!(@request)}.should raise_error(RestClient::Found)
109
+ end
110
+
111
+ it "doesn't follow a 307 when the request is a post" do
112
+ net_http_res = mock('net http response', :code => 307)
113
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post})
114
+ lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
115
+ end
116
+
117
+ it "doesn't follow a redirection when the request is a put" do
118
+ net_http_res = mock('net http response', :code => 301)
119
+ response = RestClient::Response.create('abc', net_http_res, {:method => :put})
120
+ lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
121
+ end
122
+
123
+ it "follows a redirection when the request is a post and result is a 303" do
124
+ stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
125
+ stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
126
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should == 'Foo'
127
+ end
128
+
129
+ it "follows a redirection when the request is a head" do
130
+ stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
131
+ stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
132
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should == 'Foo'
133
+ end
134
+
135
+ it "handles redirects with relative paths" do
136
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
137
+ stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
138
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
139
+ end
140
+
141
+ it "handles redirects with relative path and query string" do
142
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
143
+ stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
144
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
145
+ end
146
+
147
+ it "follow a redirection when the request is a get and the response is in the 30x range" do
148
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
149
+ stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
150
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
151
+ end
152
+
153
+ it "follows no more than 10 redirections before raising error" do
154
+ stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
155
+ stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
156
+ lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get) }.should raise_error(RestClient::MaxRedirectsReached)
157
+ WebMock.should have_requested(:get, 'http://some/redirect-2').times(10)
158
+ end
159
+
160
+ it "follows no more than max_redirects redirections, if specified" do
161
+ stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
162
+ stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
163
+ lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get, :max_redirects => 5) }.should raise_error(RestClient::MaxRedirectsReached)
164
+ WebMock.should have_requested(:get, 'http://some/redirect-2').times(5)
165
+ end
166
+ end
167
+
168
+
169
+ end
@@ -0,0 +1,68 @@
1
+ require File.join( File.dirname(File.expand_path(__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
+
30
+ it "OPTIONS" do
31
+ RestClient::Request.should_receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
32
+ RestClient.options('http://some/resource')
33
+ end
34
+ end
35
+
36
+ describe "logging" do
37
+ after do
38
+ RestClient.log = nil
39
+ end
40
+
41
+ it "uses << if the log is not a string" do
42
+ log = RestClient.log = []
43
+ log.should_receive(:<<).with('xyz')
44
+ RestClient.log << 'xyz'
45
+ end
46
+
47
+ it "displays the log to stdout" do
48
+ RestClient.log = 'stdout'
49
+ STDOUT.should_receive(:puts).with('xyz')
50
+ RestClient.log << 'xyz'
51
+ end
52
+
53
+ it "displays the log to stderr" do
54
+ RestClient.log = 'stderr'
55
+ STDERR.should_receive(:puts).with('xyz')
56
+ RestClient.log << 'xyz'
57
+ end
58
+
59
+ it "append the log to the requested filename" do
60
+ RestClient.log = '/tmp/restclient.log'
61
+ f = mock('file handle')
62
+ File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
63
+ f.should_receive(:puts).with('xyz')
64
+ RestClient.log << 'xyz'
65
+ end
66
+ end
67
+
68
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-client-next-dshelf
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 6
9
+ - 1
10
+ version: 1.6.1
11
+ platform: ruby
12
+ authors:
13
+ - Adam Wiggins
14
+ - Julien Kirch
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-07-24 00:00:00 +04: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
+ - !ruby/object:Gem::Dependency
38
+ name: webmock
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 57
46
+ segments:
47
+ - 0
48
+ - 9
49
+ - 1
50
+ version: 0.9.1
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: "A simple HTTP and REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
68
+ email: rest.client@librelist.com
69
+ executables:
70
+ - restclient
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - README.rdoc
75
+ - history.md
76
+ files:
77
+ - README.rdoc
78
+ - Rakefile
79
+ - VERSION
80
+ - bin/restclient
81
+ - lib/rest-client.rb
82
+ - lib/rest_client.rb
83
+ - lib/restclient.rb
84
+ - lib/restclient/abstract_response.rb
85
+ - lib/restclient/exceptions.rb
86
+ - lib/restclient/net_http_ext.rb
87
+ - lib/restclient/payload.rb
88
+ - lib/restclient/raw_response.rb
89
+ - lib/restclient/request.rb
90
+ - lib/restclient/resource.rb
91
+ - lib/restclient/response.rb
92
+ - spec/abstract_response_spec.rb
93
+ - spec/base.rb
94
+ - spec/exceptions_spec.rb
95
+ - spec/integration/certs/equifax.crt
96
+ - spec/integration/certs/verisign.crt
97
+ - spec/integration/request_spec.rb
98
+ - spec/integration_spec.rb
99
+ - spec/master_shake.jpg
100
+ - spec/payload_spec.rb
101
+ - spec/raw_response_spec.rb
102
+ - spec/request2_spec.rb
103
+ - spec/request_spec.rb
104
+ - spec/resource_spec.rb
105
+ - spec/response_spec.rb
106
+ - spec/restclient_spec.rb
107
+ - history.md
108
+ has_rdoc: true
109
+ homepage: http://github.com/archiloque/rest-client
110
+ licenses: []
111
+
112
+ post_install_message:
113
+ rdoc_options:
114
+ - --charset=UTF-8
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ requirements: []
136
+
137
+ rubyforge_project: rest-client
138
+ rubygems_version: 1.3.7
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
142
+ test_files:
143
+ - spec/abstract_response_spec.rb
144
+ - spec/base.rb
145
+ - spec/exceptions_spec.rb
146
+ - spec/integration/request_spec.rb
147
+ - spec/integration_spec.rb
148
+ - spec/payload_spec.rb
149
+ - spec/raw_response_spec.rb
150
+ - spec/request2_spec.rb
151
+ - spec/request_spec.rb
152
+ - spec/resource_spec.rb
153
+ - spec/response_spec.rb
154
+ - spec/restclient_spec.rb