rest-client 0.8.2 → 0.9

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

@@ -0,0 +1,48 @@
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
+ end
25
+
26
+ describe "logging" do
27
+ after do
28
+ RestClient.log = nil
29
+ end
30
+
31
+ it "gets the log source from the RESTCLIENT_LOG environment variable" do
32
+ ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return('from env')
33
+ RestClient.log = 'from class method'
34
+ RestClient.log.should == 'from env'
35
+ end
36
+
37
+ it "sets a destination for log output, used if no environment variable is set" do
38
+ ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
39
+ RestClient.log = 'from class method'
40
+ RestClient.log.should == 'from class method'
41
+ end
42
+
43
+ it "returns nil (no logging) if neither are set (default)" do
44
+ ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
45
+ RestClient.log.should == nil
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: "0.9"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-03 00:00:00 -08:00
12
+ date: 2009-01-24 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,13 +23,19 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - Rakefile
26
- - lib/request_errors.rb
27
- - lib/resource.rb
28
26
  - lib/rest_client.rb
27
+ - lib/restclient
28
+ - lib/restclient/exceptions.rb
29
+ - lib/restclient/request.rb
30
+ - lib/restclient/resource.rb
31
+ - lib/restclient/response.rb
32
+ - lib/restclient.rb
29
33
  - spec/base.rb
30
- - spec/request_errors_spec.rb
34
+ - spec/exceptions_spec.rb
35
+ - spec/request_spec.rb
31
36
  - spec/resource_spec.rb
32
- - spec/rest_client_spec.rb
37
+ - spec/response_spec.rb
38
+ - spec/restclient_spec.rb
33
39
  has_rdoc: true
34
40
  homepage: http://rest-client.heroku.com/
35
41
  post_install_message:
@@ -1,332 +0,0 @@
1
- require File.dirname(__FILE__) + '/base'
2
-
3
- describe RestClient do
4
- context "public 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
- end
25
-
26
- context "logging" do
27
- after do
28
- RestClient.log = nil
29
- end
30
-
31
- it "gets the log source from the RESTCLIENT_LOG environment variable" do
32
- ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return('from env')
33
- RestClient.log = 'from class method'
34
- RestClient.log.should == 'from env'
35
- end
36
-
37
- it "sets a destination for log output, used if no environment variable is set" do
38
- ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
39
- RestClient.log = 'from class method'
40
- RestClient.log.should == 'from class method'
41
- end
42
-
43
- it "returns nil (no logging) if neither are set (default)" do
44
- ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
45
- RestClient.log.should == nil
46
- end
47
- end
48
-
49
- context RestClient::Request do
50
- before do
51
- @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
52
-
53
- @uri = mock("uri")
54
- @uri.stub!(:request_uri).and_return('/resource')
55
- @uri.stub!(:host).and_return('some')
56
- @uri.stub!(:port).and_return(80)
57
-
58
- @net = mock("net::http base")
59
- @http = mock("net::http connection")
60
- Net::HTTP.stub!(:new).and_return(@net)
61
- @net.stub!(:start).and_yield(@http)
62
- @net.stub!(:use_ssl=)
63
- @net.stub!(:verify_mode=)
64
- end
65
-
66
- it "requests xml mimetype" do
67
- @request.default_headers[:accept].should == 'application/xml'
68
- end
69
-
70
- it "decodes an uncompressed result body by passing it straight through" do
71
- @request.decode(nil, 'xyz').should == 'xyz'
72
- end
73
-
74
- it "decodes a gzip body" do
75
- @request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
76
- end
77
-
78
- it "decodes a deflated body" do
79
- @request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
80
- end
81
-
82
- it "processes a successful result" do
83
- res = mock("result")
84
- res.stub!(:code).and_return("200")
85
- res.stub!(:body).and_return('body')
86
- res.stub!(:[]).with('content-encoding').and_return(nil)
87
- @request.process_result(res).should == 'body'
88
- end
89
-
90
- it "parses a url into a URI object" do
91
- URI.should_receive(:parse).with('http://example.com/resource')
92
- @request.parse_url('http://example.com/resource')
93
- end
94
-
95
- it "adds http:// to the front of resources specified in the syntax example.com/resource" do
96
- URI.should_receive(:parse).with('http://example.com/resource')
97
- @request.parse_url('example.com/resource')
98
- end
99
-
100
- it "extracts the username and password when parsing http://user:password@example.com/" do
101
- URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
102
- @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
103
- @request.user.should == 'joe'
104
- @request.password.should == 'pass1'
105
- end
106
-
107
- it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
108
- URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
109
- @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
110
- @request.parse_url_with_auth('http://example.com/resource')
111
- @request.user.should == 'beth'
112
- @request.password.should == 'pass2'
113
- end
114
-
115
- it "determines the Net::HTTP class to instantiate by the method name" do
116
- @request.net_http_request_class(:put).should == Net::HTTP::Put
117
- end
118
-
119
- it "merges user headers with the default headers" do
120
- @request.should_receive(:default_headers).and_return({ '1' => '2' })
121
- @request.make_headers('3' => '4').should == { '1' => '2', '3' => '4' }
122
- end
123
-
124
- it "prefers the user header when the same header exists in the defaults" do
125
- @request.should_receive(:default_headers).and_return({ '1' => '2' })
126
- @request.make_headers('1' => '3').should == { '1' => '3' }
127
- end
128
-
129
- it "converts header symbols from :content_type to 'Content-type'" do
130
- @request.should_receive(:default_headers).and_return({})
131
- @request.make_headers(:content_type => 'abc').should == { 'Content-type' => 'abc' }
132
- end
133
-
134
- it "converts header values to strings" do
135
- @request.make_headers('A' => 1)['A'].should == '1'
136
- end
137
-
138
- it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
139
- @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
140
- klass = mock("net:http class")
141
- @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
142
- klass.should_receive(:new).and_return('result')
143
- @request.should_receive(:transmit).with(@uri, 'result', 'payload')
144
- @request.execute_inner
145
- end
146
-
147
- it "transmits the request with Net::HTTP" do
148
- @http.should_receive(:request).with('req', 'payload')
149
- @request.should_receive(:process_result)
150
- @request.should_receive(:response_log)
151
- @request.transmit(@uri, 'req', 'payload')
152
- end
153
-
154
- it "uses SSL when the URI refers to a https address" do
155
- @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
156
- @net.should_receive(:use_ssl=).with(true)
157
- @http.stub!(:request)
158
- @request.stub!(:process_result)
159
- @request.stub!(:response_log)
160
- @request.transmit(@uri, 'req', 'payload')
161
- end
162
-
163
- it "doesn't send nil payloads" do
164
- @http.should_receive(:request).with('req', '')
165
- @request.should_receive(:process_result)
166
- @request.stub!(:response_log)
167
- @request.transmit(@uri, 'req', nil)
168
- end
169
-
170
- it "passes non-hash payloads straight through" do
171
- @request.process_payload("x").should == "x"
172
- end
173
-
174
- it "converts a hash payload to urlencoded data" do
175
- @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
176
- end
177
-
178
- it "accepts nested hashes in payload" do
179
- payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
180
- payload.should include('user[name]=joe')
181
- payload.should include('user[location][country]=USA')
182
- payload.should include('user[location][state]=CA')
183
- end
184
-
185
- it "set urlencoded content_type header on hash payloads" do
186
- @request.process_payload(:a => 1)
187
- @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
188
- end
189
-
190
- it "sets up the credentials prior to the request" do
191
- @http.stub!(:request)
192
- @request.stub!(:process_result)
193
- @request.stub!(:response_log)
194
-
195
- @request.stub!(:user).and_return('joe')
196
- @request.stub!(:password).and_return('mypass')
197
- @request.should_receive(:setup_credentials).with('req')
198
-
199
- @request.transmit(@uri, 'req', nil)
200
- end
201
-
202
- it "does not attempt to send any credentials if user is nil" do
203
- @request.stub!(:user).and_return(nil)
204
- req = mock("request")
205
- req.should_not_receive(:basic_auth)
206
- @request.setup_credentials(req)
207
- end
208
-
209
- it "setup credentials when there's a user" do
210
- @request.stub!(:user).and_return('joe')
211
- @request.stub!(:password).and_return('mypass')
212
- req = mock("request")
213
- req.should_receive(:basic_auth).with('joe', 'mypass')
214
- @request.setup_credentials(req)
215
- end
216
-
217
- it "catches EOFError and shows the more informative ServerBrokeConnection" do
218
- @http.stub!(:request).and_raise(EOFError)
219
- lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
220
- end
221
-
222
- it "execute calls execute_inner" do
223
- @request.should_receive(:execute_inner)
224
- @request.execute
225
- end
226
-
227
- it "class method execute wraps constructor" do
228
- req = mock("rest request")
229
- RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
230
- req.should_receive(:execute)
231
- RestClient::Request.execute(1 => 2)
232
- end
233
-
234
- it "raises a Redirect with the new location when the response is in the 30x range" do
235
- res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
236
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
237
- end
238
-
239
- it "handles redirects with relative paths" do
240
- res = mock('response', :code => '301', :header => { 'Location' => 'index' })
241
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
242
- end
243
-
244
- it "handles redirects with absolute paths" do
245
- @request.instance_variable_set('@url', 'http://some/place/else')
246
- res = mock('response', :code => '301', :header => { 'Location' => '/index' })
247
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
248
- end
249
-
250
- it "raises Unauthorized when the response is 401" do
251
- res = mock('response', :code => '401')
252
- lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
253
- end
254
-
255
- it "raises ResourceNotFound when the response is 404" do
256
- res = mock('response', :code => '404')
257
- lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
258
- end
259
-
260
- it "raises RequestFailed otherwise" do
261
- res = mock('response', :code => '500')
262
- lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
263
- end
264
-
265
- it "creates a proxy class if a proxy url is given" do
266
- RestClient.stub!(:proxy).and_return("http://example.com/")
267
- @request.net_http_class.should include(Net::HTTP::ProxyDelta)
268
- end
269
-
270
- it "creates a non-proxy class if a proxy url is not given" do
271
- @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
272
- end
273
-
274
- it "logs a get request" do
275
- RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
276
- 'RestClient.get "http://url"'
277
- end
278
-
279
- it "logs a post request with a small payload" do
280
- RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
281
- 'RestClient.post "http://url", "foo"'
282
- end
283
-
284
- it "logs a post request with a large payload" do
285
- RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
286
- 'RestClient.post "http://url", "(1000 byte payload)"'
287
- end
288
-
289
- it "logs input headers as a hash" do
290
- RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
291
- 'RestClient.get "http://url", :accept=>"text/plain"'
292
- end
293
-
294
- it "logs a response including the status code, content type, and result body size in bytes" do
295
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
296
- res.stub!(:[]).with('Content-type').and_return('text/html')
297
- @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
298
- end
299
-
300
- it "logs a response with a nil Content-type" do
301
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
302
- res.stub!(:[]).with('Content-type').and_return(nil)
303
- @request.response_log(res).should == "# => 200 OK | 4 bytes"
304
- end
305
-
306
- it "strips the charset from the response content type" do
307
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
308
- res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
309
- @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
310
- end
311
-
312
- it "displays the log to stdout" do
313
- RestClient.stub!(:log).and_return('stdout')
314
- STDOUT.should_receive(:puts).with('xyz')
315
- @request.display_log('xyz')
316
- end
317
-
318
- it "displays the log to stderr" do
319
- RestClient.stub!(:log).and_return('stderr')
320
- STDERR.should_receive(:puts).with('xyz')
321
- @request.display_log('xyz')
322
- end
323
-
324
- it "append the log to the requested filename" do
325
- RestClient.stub!(:log).and_return('/tmp/restclient.log')
326
- f = mock('file handle')
327
- File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
328
- f.should_receive(:puts).with('xyz')
329
- @request.display_log('xyz')
330
- end
331
- end
332
- end