rest-client 1.6.7 → 1.8.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.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +14 -0
  5. data/AUTHORS +81 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE +21 -0
  8. data/README.rdoc +63 -24
  9. data/Rakefile +85 -35
  10. data/bin/restclient +9 -8
  11. data/history.md +63 -1
  12. data/lib/restclient/abstract_response.rb +44 -15
  13. data/lib/restclient/exceptions.rb +20 -10
  14. data/lib/restclient/payload.rb +21 -18
  15. data/lib/restclient/platform.rb +30 -0
  16. data/lib/restclient/raw_response.rb +3 -2
  17. data/lib/restclient/request.rb +368 -63
  18. data/lib/restclient/resource.rb +3 -4
  19. data/lib/restclient/response.rb +2 -5
  20. data/lib/restclient/version.rb +7 -0
  21. data/lib/restclient/windows/root_certs.rb +105 -0
  22. data/lib/restclient/windows.rb +8 -0
  23. data/lib/restclient.rb +6 -15
  24. data/rest-client.gemspec +30 -0
  25. data/rest-client.windows.gemspec +19 -0
  26. data/spec/integration/capath_digicert/244b5494.0 +19 -0
  27. data/spec/integration/capath_digicert/81b9768f.0 +19 -0
  28. data/spec/integration/capath_digicert/README +8 -0
  29. data/spec/integration/capath_digicert/digicert.crt +19 -0
  30. data/spec/integration/capath_verisign/415660c1.0 +14 -0
  31. data/spec/integration/capath_verisign/7651b327.0 +14 -0
  32. data/spec/integration/capath_verisign/README +8 -0
  33. data/spec/integration/capath_verisign/verisign.crt +14 -0
  34. data/spec/integration/certs/digicert.crt +19 -0
  35. data/spec/{integration_spec.rb → integration/integration_spec.rb} +10 -13
  36. data/spec/integration/request_spec.rb +86 -7
  37. data/spec/spec_helper.rb +2 -0
  38. data/spec/{abstract_response_spec.rb → unit/abstract_response_spec.rb} +18 -15
  39. data/spec/{exceptions_spec.rb → unit/exceptions_spec.rb} +17 -20
  40. data/spec/unit/master_shake.jpg +0 -0
  41. data/spec/{payload_spec.rb → unit/payload_spec.rb} +42 -31
  42. data/spec/unit/raw_response_spec.rb +18 -0
  43. data/spec/{request2_spec.rb → unit/request2_spec.rb} +6 -14
  44. data/spec/unit/request_spec.rb +917 -0
  45. data/spec/{resource_spec.rb → unit/resource_spec.rb} +27 -31
  46. data/spec/{response_spec.rb → unit/response_spec.rb} +63 -57
  47. data/spec/{restclient_spec.rb → unit/restclient_spec.rb} +8 -2
  48. data/spec/unit/windows/root_certs_spec.rb +22 -0
  49. metadata +210 -112
  50. data/VERSION +0 -1
  51. data/lib/restclient/net_http_ext.rb +0 -55
  52. data/spec/base.rb +0 -16
  53. data/spec/integration/certs/equifax.crt +0 -19
  54. data/spec/master_shake.jpg +0 -0
  55. data/spec/raw_response_spec.rb +0 -17
  56. data/spec/request_spec.rb +0 -529
@@ -1,23 +1,20 @@
1
- require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
-
3
- require 'webmock/rspec'
4
- include WebMock
1
+ require 'spec_helper'
5
2
 
6
3
  describe RestClient::Exception do
7
4
  it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
8
5
  e = RestClient::Exception.new
9
- e.message.should == "RestClient::Exception"
6
+ e.message.should eq "RestClient::Exception"
10
7
  end
11
-
8
+
12
9
  it "returns the 'message' that was set" do
13
10
  e = RestClient::Exception.new
14
11
  message = "An explicitly set message"
15
12
  e.message = message
16
- e.message.should == message
13
+ e.message.should eq message
17
14
  end
18
-
15
+
19
16
  it "sets the exception message to ErrorMessage" do
20
- RestClient::ResourceNotFound.new.message.should == 'Resource Not Found'
17
+ RestClient::ResourceNotFound.new.message.should eq 'Resource Not Found'
21
18
  end
22
19
 
23
20
  it "contains exceptions in RestClient" do
@@ -29,13 +26,13 @@ end
29
26
  describe RestClient::ServerBrokeConnection do
30
27
  it "should have a default message of 'Server broke connection'" do
31
28
  e = RestClient::ServerBrokeConnection.new
32
- e.message.should == 'Server broke connection'
29
+ e.message.should eq 'Server broke connection'
33
30
  end
34
31
  end
35
32
 
36
33
  describe RestClient::RequestFailed do
37
34
  before do
38
- @response = mock('HTTP Response', :code => '502')
35
+ @response = double('HTTP Response', :code => '502')
39
36
  end
40
37
 
41
38
  it "stores the http response on the exception" do
@@ -43,17 +40,17 @@ describe RestClient::RequestFailed do
43
40
  begin
44
41
  raise RestClient::RequestFailed, response
45
42
  rescue RestClient::RequestFailed => e
46
- e.response.should == response
43
+ e.response.should eq response
47
44
  end
48
45
  end
49
46
 
50
47
  it "http_code convenience method for fetching the code as an integer" do
51
- RestClient::RequestFailed.new(@response).http_code.should == 502
48
+ RestClient::RequestFailed.new(@response).http_code.should eq 502
52
49
  end
53
50
 
54
51
  it "http_body convenience method for fetching the body (decoding when necessary)" do
55
- RestClient::RequestFailed.new(@response).http_code.should == 502
56
- RestClient::RequestFailed.new(@response).message.should == 'HTTP status code 502'
52
+ RestClient::RequestFailed.new(@response).http_code.should eq 502
53
+ RestClient::RequestFailed.new(@response).message.should eq 'HTTP status code 502'
57
54
  end
58
55
 
59
56
  it "shows the status code in the message" do
@@ -67,22 +64,22 @@ describe RestClient::ResourceNotFound do
67
64
  begin
68
65
  raise RestClient::ResourceNotFound, response
69
66
  rescue RestClient::ResourceNotFound => e
70
- e.response.should == response
67
+ e.response.should eq response
71
68
  end
72
69
  end
73
70
  end
74
71
 
75
72
  describe "backwards compatibility" do
76
73
  it "alias RestClient::Request::Redirect to RestClient::Redirect" do
77
- RestClient::Request::Redirect.should == RestClient::Redirect
74
+ RestClient::Request::Redirect.should eq RestClient::Redirect
78
75
  end
79
76
 
80
77
  it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
81
- RestClient::Request::Unauthorized.should == RestClient::Unauthorized
78
+ RestClient::Request::Unauthorized.should eq RestClient::Unauthorized
82
79
  end
83
80
 
84
81
  it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
85
- RestClient::Request::RequestFailed.should == RestClient::RequestFailed
82
+ RestClient::Request::RequestFailed.should eq RestClient::RequestFailed
86
83
  end
87
84
 
88
85
  it "make the exception's response act like an Net::HTTPResponse" do
@@ -92,7 +89,7 @@ describe "backwards compatibility" do
92
89
  RestClient.get "www.example.com"
93
90
  raise
94
91
  rescue RestClient::ResourceNotFound => e
95
- e.response.body.should == body
92
+ e.response.body.should eq body
96
93
  end
97
94
  end
98
95
  end
Binary file
@@ -1,29 +1,31 @@
1
- require File.join(File.dirname(File.expand_path(__FILE__)), 'base')
1
+ # encoding: binary
2
+
3
+ require 'spec_helper'
2
4
 
3
5
  describe RestClient::Payload do
4
6
  context "A regular Payload" do
5
7
  it "should use standard enctype as default content-type" do
6
8
  RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
7
- should == 'application/x-www-form-urlencoded'
9
+ should eq 'application/x-www-form-urlencoded'
8
10
  end
9
11
 
10
12
  it "should form properly encoded params" do
11
13
  RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
12
- should == "foo=bar"
14
+ should eq "foo=bar"
13
15
  ["foo=bar&baz=qux", "baz=qux&foo=bar"].should include(
14
16
  RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s)
15
17
  end
16
18
 
17
19
  it "should escape parameters" do
18
20
  RestClient::Payload::UrlEncoded.new({'foo ' => 'bar'}).to_s.
19
- should == "foo%20=bar"
21
+ should eq "foo%20=bar"
20
22
  end
21
23
 
22
24
  it "should properly handle hashes as parameter" do
23
25
  RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s.
24
- should == "foo[bar]=baz"
26
+ should eq "foo[bar]=baz"
25
27
  RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s.
26
- should == "foo[bar][baz]=qux"
28
+ should eq "foo[bar][baz]=qux"
27
29
  end
28
30
 
29
31
  it "should handle many attributes inside a hash" do
@@ -43,18 +45,18 @@ describe RestClient::Payload do
43
45
 
44
46
  it "should form properly use symbols as parameters" do
45
47
  RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s.
46
- should == "foo=bar"
48
+ should eq "foo=bar"
47
49
  RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s.
48
- should == "foo[bar]=baz"
50
+ should eq "foo[bar]=baz"
49
51
  end
50
52
 
51
53
  it "should properly handle arrays as repeated parameters" do
52
54
  RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s.
53
- should == "foo[]=bar"
55
+ should eq "foo[]=bar"
54
56
  RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s.
55
- should == "foo[]=bar&foo[]=baz"
57
+ should eq "foo[]=bar&foo[]=baz"
56
58
  end
57
-
59
+
58
60
  it 'should not close if stream already closed' do
59
61
  p = RestClient::Payload::UrlEncoded.new({'foo ' => 'bar'})
60
62
  3.times {p.close}
@@ -65,10 +67,10 @@ describe RestClient::Payload do
65
67
  context "A multipart Payload" do
66
68
  it "should use standard enctype as default content-type" do
67
69
  m = RestClient::Payload::Multipart.new({})
68
- m.stub!(:boundary).and_return(123)
69
- m.headers['Content-Type'].should == 'multipart/form-data; boundary=123'
70
+ m.stub(:boundary).and_return(123)
71
+ m.headers['Content-Type'].should eq 'multipart/form-data; boundary=123'
70
72
  end
71
-
73
+
72
74
  it 'should not error on close if stream already closed' do
73
75
  m = RestClient::Payload::Multipart.new(:file => File.new(File.join(File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg')))
74
76
  3.times {m.close}
@@ -76,7 +78,7 @@ describe RestClient::Payload do
76
78
 
77
79
  it "should form properly separated multipart data" do
78
80
  m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
79
- m.to_s.should == <<-EOS
81
+ m.to_s.should eq <<-EOS
80
82
  --#{m.boundary}\r
81
83
  Content-Disposition: form-data; name="bar"\r
82
84
  \r
@@ -91,7 +93,7 @@ bar\r
91
93
 
92
94
  it "should not escape parameters names" do
93
95
  m = RestClient::Payload::Multipart.new([["bar ", "baz"]])
94
- m.to_s.should == <<-EOS
96
+ m.to_s.should eq <<-EOS
95
97
  --#{m.boundary}\r
96
98
  Content-Disposition: form-data; name="bar "\r
97
99
  \r
@@ -103,12 +105,12 @@ baz\r
103
105
  it "should form properly separated multipart data" do
104
106
  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
105
107
  m = RestClient::Payload::Multipart.new({:foo => f})
106
- m.to_s.should == <<-EOS
108
+ m.to_s.should eq <<-EOS
107
109
  --#{m.boundary}\r
108
110
  Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r
109
111
  Content-Type: image/jpeg\r
110
112
  \r
111
- #{IO.read(f.path)}\r
113
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
112
114
  --#{m.boundary}--\r
113
115
  EOS
114
116
  end
@@ -116,12 +118,12 @@ Content-Type: image/jpeg\r
116
118
  it "should ignore the name attribute when it's not set" do
117
119
  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
118
120
  m = RestClient::Payload::Multipart.new({nil => f})
119
- m.to_s.should == <<-EOS
121
+ m.to_s.should eq <<-EOS
120
122
  --#{m.boundary}\r
121
123
  Content-Disposition: form-data; filename="master_shake.jpg"\r
122
124
  Content-Type: image/jpeg\r
123
125
  \r
124
- #{IO.read(f.path)}\r
126
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
125
127
  --#{m.boundary}--\r
126
128
  EOS
127
129
  end
@@ -131,19 +133,19 @@ Content-Type: image/jpeg\r
131
133
  f.instance_eval "def content_type; 'text/plain'; end"
132
134
  f.instance_eval "def original_filename; 'foo.txt'; end"
133
135
  m = RestClient::Payload::Multipart.new({:foo => f})
134
- m.to_s.should == <<-EOS
136
+ m.to_s.should eq <<-EOS
135
137
  --#{m.boundary}\r
136
138
  Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
137
139
  Content-Type: text/plain\r
138
140
  \r
139
- #{IO.read(f.path)}\r
141
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
140
142
  --#{m.boundary}--\r
141
143
  EOS
142
144
  end
143
145
 
144
146
  it "should handle hash in hash parameters" do
145
147
  m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
146
- m.to_s.should == <<-EOS
148
+ m.to_s.should eq <<-EOS
147
149
  --#{m.boundary}\r
148
150
  Content-Disposition: form-data; name="bar[baz]"\r
149
151
  \r
@@ -155,12 +157,12 @@ foo\r
155
157
  f.instance_eval "def content_type; 'text/plain'; end"
156
158
  f.instance_eval "def original_filename; 'foo.txt'; end"
157
159
  m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
158
- m.to_s.should == <<-EOS
160
+ m.to_s.should eq <<-EOS
159
161
  --#{m.boundary}\r
160
162
  Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
161
163
  Content-Type: text/plain\r
162
164
  \r
163
- #{IO.read(f.path)}\r
165
+ #{File.open(f.path, 'rb'){|bin| bin.read}}\r
164
166
  --#{m.boundary}--\r
165
167
  EOS
166
168
  end
@@ -171,23 +173,23 @@ Content-Type: text/plain\r
171
173
  it "should properly determine the size of file payloads" do
172
174
  f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
173
175
  payload = RestClient::Payload.generate(f)
174
- payload.size.should == 22_545
175
- payload.length.should == 22_545
176
+ payload.size.should eq 76_988
177
+ payload.length.should eq 76_988
176
178
  end
177
179
 
178
180
  it "should properly determine the size of other kinds of streaming payloads" do
179
181
  s = StringIO.new 'foo'
180
182
  payload = RestClient::Payload.generate(s)
181
- payload.size.should == 3
182
- payload.length.should == 3
183
+ payload.size.should eq 3
184
+ payload.length.should eq 3
183
185
 
184
186
  begin
185
187
  f = Tempfile.new "rest-client"
186
188
  f.write 'foo bar'
187
189
 
188
190
  payload = RestClient::Payload.generate(f)
189
- payload.size.should == 7
190
- payload.length.should == 7
191
+ payload.size.should eq 7
192
+ payload.length.should eq 7
191
193
  ensure
192
194
  f.close
193
195
  end
@@ -230,5 +232,14 @@ Content-Type: text/plain\r
230
232
  it "should recognize other payloads that can be streamed" do
231
233
  RestClient::Payload.generate(StringIO.new('foo')).should be_kind_of(RestClient::Payload::Streamed)
232
234
  end
235
+
236
+ # hashery gem introduces Hash#read convenience method. Existence of #read method used to determine of content is streameable :/
237
+ it "shouldn't treat hashes as streameable" do
238
+ RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
239
+ end
240
+ end
241
+
242
+ class HashMapForTesting < Hash
243
+ alias :read :[]
233
244
  end
234
245
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestClient::RawResponse do
4
+ before do
5
+ @tf = double("Tempfile", :read => "the answer is 42", :open => true)
6
+ @net_http_res = double('net http response')
7
+ @request = double('http request')
8
+ @response = RestClient::RawResponse.new(@tf, @net_http_res, {}, @request)
9
+ end
10
+
11
+ it "behaves like string" do
12
+ @response.to_s.should eq 'the answer is 42'
13
+ end
14
+
15
+ it "exposes a Tempfile" do
16
+ @response.file.should eq @tf
17
+ end
18
+ end
@@ -1,40 +1,32 @@
1
- require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
-
3
- require 'webmock/rspec'
4
- include WebMock
1
+ require 'spec_helper'
5
2
 
6
3
  describe RestClient::Request do
7
4
 
8
5
  it "manage params for get requests" do
9
6
  stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
10
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should == 'foo'
7
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should eq 'foo'
11
8
 
12
9
  stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar', 'params' => 'a'}).to_return(:body => 'foo', :status => 200)
13
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should == 'foo'
10
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should eq 'foo'
14
11
  end
15
12
 
16
13
  it "can use a block to process response" do
17
14
  response_value = nil
18
- block = Proc.new do |http_response|
15
+ block = proc do |http_response|
19
16
  response_value = http_response.body
20
17
  end
21
18
  stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
22
19
  RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
23
- response_value.should == "foo"
20
+ response_value.should eq "foo"
24
21
  end
25
22
 
26
23
  it 'closes payload if not nil' do
27
24
  test_file = File.new(File.join( File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg'))
28
- initial_count = tmp_count
29
25
 
30
26
  stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200)
31
27
  RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file})
32
28
 
33
- tmp_count.should == initial_count
29
+ test_file.closed?.should be_true
34
30
  end
35
31
 
36
32
  end
37
-
38
- def tmp_count
39
- Dir.glob(Dir::tmpdir + "/*").size
40
- end