larsburgess-rest-client 1.6.1

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.
@@ -0,0 +1,124 @@
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 "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 "passes a given block to subresources" do
76
+ block = Proc.new{|r| r}
77
+ parent = RestClient::Resource.new('http://example.com', &block)
78
+ parent['posts'].block.should == block
79
+ end
80
+
81
+ it "the block should be overrideable" do
82
+ block1 = Proc.new{|r| r}
83
+ block2 = Proc.new{|r| r}
84
+ parent = RestClient::Resource.new('http://example.com', &block1)
85
+ # parent['posts', &block2].block.should == block2 # ruby 1.9 syntax
86
+ parent.send(:[], 'posts', &block2).block.should == block2
87
+ end
88
+
89
+ it "the block should be overrideable in ruby 1.9 syntax" do
90
+ block = Proc.new{|r| r}
91
+ parent = RestClient::Resource.new('http://example.com', &block)
92
+ r19_syntax = %q{
93
+ parent['posts', &->(r){r}].block.should_not == block
94
+ }
95
+ if is_ruby_19?
96
+ eval(r19_syntax)
97
+ end
98
+ end
99
+
100
+ it "prints its url with to_s" do
101
+ RestClient::Resource.new('x').to_s.should == 'x'
102
+ end
103
+
104
+ describe 'block' do
105
+ it 'can use block when creating the resource' do
106
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
107
+ resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
108
+ resource.get.should == 'foo'
109
+ end
110
+
111
+ it 'can use block when executing the resource' do
112
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
113
+ resource = RestClient::Resource.new('www.example.com')
114
+ resource.get { |response, request| 'foo' }.should == 'foo'
115
+ end
116
+
117
+ it 'execution block override resource block' do
118
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
119
+ resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
120
+ resource.get { |response, request| 'bar' }.should == 'bar'
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,157 @@
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
+
154
+ end
155
+
156
+
157
+ 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,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: larsburgess-rest-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 6
9
+ - 1
10
+ version: 1.6.1
11
+ platform: ruby
12
+ authors:
13
+ - Adam Wiggins
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-16 00:00:00 -08:00
19
+ default_executable: restclient
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mime-types
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 47
30
+ segments:
31
+ - 1
32
+ - 16
33
+ version: "1.16"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: webmock
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 57
45
+ segments:
46
+ - 0
47
+ - 9
48
+ - 1
49
+ version: 0.9.1
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ description: "A simple HTTP and REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
67
+ email: rest.client@librelist.com
68
+ executables:
69
+ - restclient
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - README.rdoc
74
+ files:
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - bin/restclient
79
+ - lib/rest-client.rb
80
+ - lib/rest_client.rb
81
+ - lib/restclient.rb
82
+ - lib/restclient/abstract_response.rb
83
+ - lib/restclient/exceptions.rb
84
+ - lib/restclient/net_http_ext.rb
85
+ - lib/restclient/payload.rb
86
+ - lib/restclient/raw_response.rb
87
+ - lib/restclient/request.rb
88
+ - lib/restclient/resource.rb
89
+ - lib/restclient/response.rb
90
+ - spec/abstract_response_spec.rb
91
+ - spec/base.rb
92
+ - spec/exceptions_spec.rb
93
+ - spec/integration/certs/equifax.crt
94
+ - spec/integration/certs/verisign.crt
95
+ - spec/integration/request_spec.rb
96
+ - spec/integration_spec.rb
97
+ - spec/master_shake.jpg
98
+ - spec/payload_spec.rb
99
+ - spec/raw_response_spec.rb
100
+ - spec/request2_spec.rb
101
+ - spec/request_spec.rb
102
+ - spec/resource_spec.rb
103
+ - spec/response_spec.rb
104
+ - spec/restclient_spec.rb
105
+ has_rdoc: true
106
+ homepage: http://github.com/archiloque/rest-client
107
+ licenses: []
108
+
109
+ post_install_message:
110
+ rdoc_options: []
111
+
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ requirements: []
133
+
134
+ rubyforge_project: rest-client
135
+ rubygems_version: 1.5.0
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.
139
+ test_files:
140
+ - spec/abstract_response_spec.rb
141
+ - spec/base.rb
142
+ - spec/exceptions_spec.rb
143
+ - spec/integration/request_spec.rb
144
+ - spec/integration_spec.rb
145
+ - spec/payload_spec.rb
146
+ - spec/raw_response_spec.rb
147
+ - spec/request2_spec.rb
148
+ - spec/request_spec.rb
149
+ - spec/resource_spec.rb
150
+ - spec/response_spec.rb
151
+ - spec/restclient_spec.rb