rest-client-next 1.1.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,46 +1,46 @@
1
1
  require File.dirname(__FILE__) + '/../base'
2
2
 
3
3
  class MockResponse
4
- include RestClient::Mixin::Response
4
+ include RestClient::Mixin::Response
5
5
 
6
- def initialize(body, res)
7
- @net_http_res = res
8
- @body = @body
9
- end
6
+ def initialize(body, res)
7
+ @net_http_res = res
8
+ @body = body
9
+ end
10
10
  end
11
11
 
12
12
  describe RestClient::Mixin::Response do
13
- before do
14
- @net_http_res = mock('net http response')
15
- @response = MockResponse.new('abc', @net_http_res)
16
- end
17
-
18
- it "fetches the numeric response code" do
19
- @net_http_res.should_receive(:code).and_return('200')
20
- @response.code.should == 200
21
- end
22
-
23
- it "beautifies the headers by turning the keys to symbols" do
24
- h = RestClient::Response.beautify_headers('content-type' => [ 'x' ])
25
- h.keys.first.should == :content_type
26
- end
27
-
28
- it "beautifies the headers by turning the values to strings instead of one-element arrays" do
29
- h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] )
30
- h.values.first.should == 'text/html'
31
- end
32
-
33
- it "fetches the headers" do
34
- @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
35
- @response.headers.should == { :content_type => 'text/html' }
36
- end
37
-
38
- it "extracts cookies from response headers" do
39
- @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
40
- @response.cookies.should == { 'session_id' => '1' }
41
- end
42
-
43
- it "can access the net http result directly" do
44
- @response.net_http_res.should == @net_http_res
45
- end
13
+ before do
14
+ @net_http_res = mock('net http response')
15
+ @response = MockResponse.new('abc', @net_http_res)
16
+ end
17
+
18
+ it "fetches the numeric response code" do
19
+ @net_http_res.should_receive(:code).and_return('200')
20
+ @response.code.should == 200
21
+ end
22
+
23
+ it "beautifies the headers by turning the keys to symbols" do
24
+ h = RestClient::Response.beautify_headers('content-type' => [ 'x' ])
25
+ h.keys.first.should == :content_type
26
+ end
27
+
28
+ it "beautifies the headers by turning the values to strings instead of one-element arrays" do
29
+ h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] )
30
+ h.values.first.should == 'text/html'
31
+ end
32
+
33
+ it "fetches the headers" do
34
+ @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
35
+ @response.headers.should == { :content_type => 'text/html' }
36
+ end
37
+
38
+ it "extracts cookies from response headers" do
39
+ @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
40
+ @response.cookies.should == { 'session_id' => '1' }
41
+ end
42
+
43
+ it "can access the net http result directly" do
44
+ @response.net_http_res.should == @net_http_res
45
+ end
46
46
  end
@@ -1,131 +1,131 @@
1
1
  require File.dirname(__FILE__) + "/base"
2
2
 
3
3
  describe RestClient::Payload do
4
- context "A regular Payload" do
5
- it "should use standard enctype as default content-type" do
6
- RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
7
- should == 'application/x-www-form-urlencoded'
8
- end
9
-
10
- it "should form properly encoded params" do
11
- RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
12
- should == "foo=bar"
13
- end
14
-
15
- it "should properly handle hashes as parameter" do
16
- RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz' }}).to_s.
17
- should == "foo[bar]=baz"
18
- RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux' }}}).to_s.
19
- should == "foo[bar][baz]=qux"
20
- end
21
-
22
- it "should form properly use symbols as parameters" do
23
- RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s.
24
- should == "foo=bar"
25
- RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz }}).to_s.
26
- should == "foo[bar]=baz"
27
- end
28
-
29
- end
30
-
31
- context "A multipart Payload" do
32
- it "should use standard enctype as default content-type" do
33
- m = RestClient::Payload::Multipart.new({})
34
- m.stub!(:boundary).and_return(123)
35
- m.headers['Content-Type'].should == 'multipart/form-data; boundary="123"'
36
- end
37
-
38
- it "should form properly seperated multipart data" do
39
- m = RestClient::Payload::Multipart.new([[:bar , "baz"], [:foo, "bar"]])
40
- m.to_s.should == <<-EOS
4
+ context "A regular Payload" do
5
+ it "should use standard enctype as default content-type" do
6
+ RestClient::Payload::UrlEncoded.new({}).headers['Content-Type'].
7
+ should == 'application/x-www-form-urlencoded'
8
+ end
9
+
10
+ it "should form properly encoded params" do
11
+ RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s.
12
+ should == "foo=bar"
13
+ end
14
+
15
+ it "should properly handle hashes as parameter" do
16
+ RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz' }}).to_s.
17
+ should == "foo[bar]=baz"
18
+ RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux' }}}).to_s.
19
+ should == "foo[bar][baz]=qux"
20
+ end
21
+
22
+ it "should form properly use symbols as parameters" do
23
+ RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s.
24
+ should == "foo=bar"
25
+ RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz }}).to_s.
26
+ should == "foo[bar]=baz"
27
+ end
28
+
29
+ end
30
+
31
+ context "A multipart Payload" do
32
+ it "should use standard enctype as default content-type" do
33
+ m = RestClient::Payload::Multipart.new({})
34
+ m.stub!(:boundary).and_return(123)
35
+ m.headers['Content-Type'].should == 'multipart/form-data; boundary="123"'
36
+ end
37
+
38
+ it "should form properly separated multipart data" do
39
+ m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
40
+ m.to_s.should == <<-EOS
41
41
  --#{m.boundary}\r
42
- Content-Disposition: multipart/form-data; name="bar"\r
42
+ Content-Disposition: form-data; name="bar"\r
43
43
  \r
44
44
  baz\r
45
45
  --#{m.boundary}\r
46
- Content-Disposition: multipart/form-data; name="foo"\r
46
+ Content-Disposition: form-data; name="foo"\r
47
47
  \r
48
48
  bar\r
49
49
  --#{m.boundary}--\r
50
- EOS
51
- end
50
+ EOS
51
+ end
52
52
 
53
- it "should form properly seperated multipart data" do
54
- f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
55
- m = RestClient::Payload::Multipart.new({:foo => f})
56
- m.to_s.should == <<-EOS
53
+ it "should form properly separated multipart data" do
54
+ f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
55
+ m = RestClient::Payload::Multipart.new({:foo => f})
56
+ m.to_s.should == <<-EOS
57
57
  --#{m.boundary}\r
58
- Content-Disposition: multipart/form-data; name="foo"; filename="master_shake.jpg"\r
58
+ Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r
59
59
  Content-Type: image/jpeg\r
60
60
  \r
61
61
  #{IO.read(f.path)}\r
62
62
  --#{m.boundary}--\r
63
- EOS
64
- end
65
-
66
- it "should detect optional (original) content type and filename" do
67
- f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
68
- f.instance_eval "def content_type; 'text/plain'; end"
69
- f.instance_eval "def original_filename; 'foo.txt'; end"
70
- m = RestClient::Payload::Multipart.new({:foo => f})
71
- m.to_s.should == <<-EOS
63
+ EOS
64
+ end
65
+
66
+ it "should detect optional (original) content type and filename" do
67
+ f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
68
+ f.instance_eval "def content_type; 'text/plain'; end"
69
+ f.instance_eval "def original_filename; 'foo.txt'; end"
70
+ m = RestClient::Payload::Multipart.new({:foo => f})
71
+ m.to_s.should == <<-EOS
72
72
  --#{m.boundary}\r
73
- Content-Disposition: multipart/form-data; name="foo"; filename="foo.txt"\r
73
+ Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
74
74
  Content-Type: text/plain\r
75
75
  \r
76
76
  #{IO.read(f.path)}\r
77
77
  --#{m.boundary}--\r
78
- EOS
79
- end
78
+ EOS
79
+ end
80
80
 
81
- it "should handle hash in hash parameters" do
82
- m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
83
- m.to_s.should == <<-EOS
81
+ it "should handle hash in hash parameters" do
82
+ m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
83
+ m.to_s.should == <<-EOS
84
84
  --#{m.boundary}\r
85
- Content-Disposition: multipart/form-data; name="bar[baz]"\r
85
+ Content-Disposition: form-data; name="bar[baz]"\r
86
86
  \r
87
87
  foo\r
88
88
  --#{m.boundary}--\r
89
- EOS
89
+ EOS
90
90
 
91
- f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
92
- f.instance_eval "def content_type; 'text/plain'; end"
93
- f.instance_eval "def original_filename; 'foo.txt'; end"
94
- m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
95
- m.to_s.should == <<-EOS
91
+ f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
92
+ f.instance_eval "def content_type; 'text/plain'; end"
93
+ f.instance_eval "def original_filename; 'foo.txt'; end"
94
+ m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
95
+ m.to_s.should == <<-EOS
96
96
  --#{m.boundary}\r
97
- Content-Disposition: multipart/form-data; name="foo[bar]"; filename="foo.txt"\r
97
+ Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
98
98
  Content-Type: text/plain\r
99
99
  \r
100
100
  #{IO.read(f.path)}\r
101
101
  --#{m.boundary}--\r
102
- EOS
103
- end
104
-
105
- end
106
-
107
- context "Payload generation" do
108
- it "should recognize standard urlencoded params" do
109
- RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
110
- end
111
-
112
- it "should recognize multipart params" do
113
- f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
114
- RestClient::Payload.generate({"foo" => f}).should be_kind_of(RestClient::Payload::Multipart)
115
- end
116
-
117
- it "should be multipart if forced" do
118
- RestClient::Payload.generate({"foo" => "bar", :multipart => true}).should be_kind_of(RestClient::Payload::Multipart)
119
- end
120
-
121
- it "should return data if no of the above" do
122
- RestClient::Payload.generate("data").should be_kind_of(RestClient::Payload::Base)
123
- end
124
-
125
- it "should recognize nested multipart payloads" do
126
- f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
127
- RestClient::Payload.generate({"foo" => {"file" => f}}).should be_kind_of(RestClient::Payload::Multipart)
128
- end
129
-
130
- end
102
+ EOS
103
+ end
104
+
105
+ end
106
+
107
+ context "Payload generation" do
108
+ it "should recognize standard urlencoded params" do
109
+ RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
110
+ end
111
+
112
+ it "should recognize multipart params" do
113
+ f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
114
+ RestClient::Payload.generate({"foo" => f}).should be_kind_of(RestClient::Payload::Multipart)
115
+ end
116
+
117
+ it "should be multipart if forced" do
118
+ RestClient::Payload.generate({"foo" => "bar", :multipart => true}).should be_kind_of(RestClient::Payload::Multipart)
119
+ end
120
+
121
+ it "should return data if no of the above" do
122
+ RestClient::Payload.generate("data").should be_kind_of(RestClient::Payload::Base)
123
+ end
124
+
125
+ it "should recognize nested multipart payloads" do
126
+ f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
127
+ RestClient::Payload.generate({"foo" => {"file" => f}}).should be_kind_of(RestClient::Payload::Multipart)
128
+ end
129
+
130
+ end
131
131
  end
@@ -1,17 +1,17 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
3
  describe RestClient::RawResponse do
4
- before do
5
- @tf = mock("Tempfile", :read => "the answer is 42", :open => true)
6
- @net_http_res = mock('net http response')
7
- @response = RestClient::RawResponse.new(@tf, @net_http_res)
8
- end
4
+ before do
5
+ @tf = mock("Tempfile", :read => "the answer is 42", :open => true)
6
+ @net_http_res = mock('net http response')
7
+ @response = RestClient::RawResponse.new(@tf, @net_http_res)
8
+ end
9
9
 
10
- it "behaves like string" do
11
- @response.to_s.should == 'the answer is 42'
12
- end
10
+ it "behaves like string" do
11
+ @response.to_s.should == 'the answer is 42'
12
+ end
13
13
 
14
- it "exposes a Tempfile" do
15
- @response.file.should == @tf
16
- end
14
+ it "exposes a Tempfile" do
15
+ @response.file.should == @tf
16
+ end
17
17
  end
@@ -1,497 +1,546 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
3
  describe RestClient::Request do
4
- before do
5
- @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
6
-
7
- @uri = mock("uri")
8
- @uri.stub!(:request_uri).and_return('/resource')
9
- @uri.stub!(:host).and_return('some')
10
- @uri.stub!(:port).and_return(80)
11
-
12
- @net = mock("net::http base")
13
- @http = mock("net::http connection")
14
- Net::HTTP.stub!(:new).and_return(@net)
15
- @net.stub!(:start).and_yield(@http)
16
- @net.stub!(:use_ssl=)
17
- @net.stub!(:verify_mode=)
18
- end
19
-
20
- it "accept */* mimetype, preferring xml" do
21
- @request.default_headers[:accept].should == '*/*; q=0.5, application/xml'
22
- end
23
-
24
- it "decodes an uncompressed result body by passing it straight through" do
25
- RestClient::Request.decode(nil, 'xyz').should == 'xyz'
26
- end
27
-
28
- it "decodes a gzip body" do
29
- RestClient::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"
30
- end
31
-
32
- it "ingores gzip for empty bodies" do
33
- RestClient::Request.decode('gzip', '').should be_empty
34
- end
35
-
36
- it "decodes a deflated body" do
37
- RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
38
- end
39
-
40
- it "processes a successful result" do
41
- res = mock("result")
42
- res.stub!(:code).and_return("200")
43
- res.stub!(:body).and_return('body')
44
- res.stub!(:[]).with('content-encoding').and_return(nil)
45
- @request.process_result(res).should == 'body'
46
- end
47
-
48
- it "doesn't classify successful requests as failed" do
49
- 203.upto(206) do |code|
50
- res = mock("result")
51
- res.stub!(:code).and_return(code.to_s)
52
- res.stub!(:body).and_return("")
53
- res.stub!(:[]).with('content-encoding').and_return(nil)
54
- @request.process_result(res).should be_empty
55
- end
56
- end
57
-
58
- it "parses a url into a URI object" do
59
- URI.should_receive(:parse).with('http://example.com/resource')
60
- @request.parse_url('http://example.com/resource')
61
- end
62
-
63
- it "adds http:// to the front of resources specified in the syntax example.com/resource" do
64
- URI.should_receive(:parse).with('http://example.com/resource')
65
- @request.parse_url('example.com/resource')
66
- end
67
-
68
- it "extracts the username and password when parsing http://user:password@example.com/" do
69
- URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
70
- @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
71
- @request.user.should == 'joe'
72
- @request.password.should == 'pass1'
73
- end
74
-
75
- 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
76
- URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
77
- @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
78
- @request.parse_url_with_auth('http://example.com/resource')
79
- @request.user.should == 'beth'
80
- @request.password.should == 'pass2'
81
- end
82
-
83
- it "correctly formats cookies provided to the constructor" do
84
- URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
85
- @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
86
- @request.should_receive(:default_headers).and_return({'foo' => 'bar'})
87
- headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
88
- end
89
-
90
- it "determines the Net::HTTP class to instantiate by the method name" do
91
- @request.net_http_request_class(:put).should == Net::HTTP::Put
92
- end
93
-
94
- it "merges user headers with the default headers" do
95
- @request.should_receive(:default_headers).and_return({ '1' => '2' })
96
- headers = @request.make_headers('3' => '4')
97
- headers.should have_key('1')
98
- headers['1'].should == '2'
99
- headers.should have_key('3')
100
- headers['3'].should == '4'
101
- end
102
-
103
- it "prefers the user header when the same header exists in the defaults" do
104
- @request.should_receive(:default_headers).and_return({ '1' => '2' })
105
- headers = @request.make_headers('1' => '3')
106
- headers.should have_key('1')
107
- headers['1'].should == '3'
108
- end
109
-
110
- it "converts header symbols from :content_type to 'Content-type'" do
111
- @request.should_receive(:default_headers).and_return({})
112
- headers = @request.make_headers(:content_type => 'abc')
113
- headers.should have_key('Content-type')
114
- headers['Content-type'].should == 'abc'
115
- end
116
-
117
- it "converts header values to strings" do
118
- @request.make_headers('A' => 1)['A'].should == '1'
119
- end
120
-
121
- it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
122
- @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
123
- klass = mock("net:http class")
124
- @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
125
- klass.should_receive(:new).and_return('result')
126
- @request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
127
- @request.execute_inner
128
- end
129
-
130
- it "transmits the request with Net::HTTP" do
131
- @http.should_receive(:request).with('req', 'payload')
132
- @request.should_receive(:process_result)
133
- @request.should_receive(:response_log)
134
- @request.transmit(@uri, 'req', 'payload')
135
- end
136
-
137
- it "uses SSL when the URI refers to a https address" do
138
- @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
139
- @net.should_receive(:use_ssl=).with(true)
140
- @http.stub!(:request)
141
- @request.stub!(:process_result)
142
- @request.stub!(:response_log)
143
- @request.transmit(@uri, 'req', 'payload')
144
- end
145
-
146
- it "sends nil payloads" do
147
- @http.should_receive(:request).with('req', nil)
148
- @request.should_receive(:process_result)
149
- @request.stub!(:response_log)
150
- @request.transmit(@uri, 'req', nil)
151
- end
152
-
153
- it "passes non-hash payloads straight through" do
154
- @request.process_payload("x").should == "x"
155
- end
156
-
157
- it "converts a hash payload to urlencoded data" do
158
- @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
159
- end
160
-
161
- it "accepts nested hashes in payload" do
162
- payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
163
- payload.should include('user[name]=joe')
164
- payload.should include('user[location][country]=USA')
165
- payload.should include('user[location][state]=CA')
166
- end
167
-
168
- it "set urlencoded content_type header on hash payloads" do
169
- @request.process_payload(:a => 1)
170
- @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
171
- end
172
-
173
- it "sets up the credentials prior to the request" do
174
- @http.stub!(:request)
175
- @request.stub!(:process_result)
176
- @request.stub!(:response_log)
177
-
178
- @request.stub!(:user).and_return('joe')
179
- @request.stub!(:password).and_return('mypass')
180
- @request.should_receive(:setup_credentials).with('req')
181
-
182
- @request.transmit(@uri, 'req', nil)
183
- end
184
-
185
- it "does not attempt to send any credentials if user is nil" do
186
- @request.stub!(:user).and_return(nil)
187
- req = mock("request")
188
- req.should_not_receive(:basic_auth)
189
- @request.setup_credentials(req)
190
- end
191
-
192
- it "setup credentials when there's a user" do
193
- @request.stub!(:user).and_return('joe')
194
- @request.stub!(:password).and_return('mypass')
195
- req = mock("request")
196
- req.should_receive(:basic_auth).with('joe', 'mypass')
197
- @request.setup_credentials(req)
198
- end
199
-
200
- it "catches EOFError and shows the more informative ServerBrokeConnection" do
201
- @http.stub!(:request).and_raise(EOFError)
202
- lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
203
- end
204
-
205
- it "execute calls execute_inner" do
206
- @request.should_receive(:execute_inner)
207
- @request.execute
208
- end
209
-
210
- it "class method execute wraps constructor" do
211
- req = mock("rest request")
212
- RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
213
- req.should_receive(:execute)
214
- RestClient::Request.execute(1 => 2)
215
- end
216
-
217
- it "raises a Redirect with the new location when the response is in the 30x range" do
218
- res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
219
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
220
- end
221
-
222
- it "handles redirects with relative paths" do
223
- res = mock('response', :code => '301', :header => { 'Location' => 'index' })
224
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
225
- end
226
-
227
- it "handles redirects with absolute paths" do
228
- @request.instance_variable_set('@url', 'http://some/place/else')
229
- res = mock('response', :code => '301', :header => { 'Location' => '/index' })
230
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
231
- end
232
-
233
- it "uses GET and clears payload when following 30x redirects" do
234
- url = "http://example.com/redirected"
235
-
236
- @request.should_receive(:execute_inner).once.ordered.and_raise(RestClient::Redirect.new(url))
237
-
238
- @request.should_receive(:execute_inner).once.ordered do
239
- @request.url.should == url
240
- @request.method.should == :get
241
- @request.payload.should be_nil
242
- end
243
-
244
- @request.execute
245
- end
246
-
247
- it "raises Unauthorized when the response is 401" do
248
- res = mock('response', :code => '401')
249
- lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
250
- end
251
-
252
- it "raises ResourceNotFound when the response is 404" do
253
- res = mock('response', :code => '404')
254
- lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
255
- end
256
-
257
- it "raises RequestFailed otherwise" do
258
- res = mock('response', :code => '500')
259
- lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
260
- end
261
-
262
- it "creates a proxy class if a proxy url is given" do
263
- RestClient.stub!(:proxy).and_return("http://example.com/")
264
- @request.net_http_class.should include(Net::HTTP::ProxyDelta)
265
- end
266
-
267
- it "creates a non-proxy class if a proxy url is not given" do
268
- @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
269
- end
270
-
271
- it "logs a get request" do
272
- RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
273
- 'RestClient.get "http://url"'
274
- end
275
-
276
- it "logs a post request with a small payload" do
277
- RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
278
- 'RestClient.post "http://url", "foo"'
279
- end
280
-
281
- it "logs a post request with a large payload" do
282
- RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
283
- 'RestClient.post "http://url", "(1000 byte payload)"'
284
- end
285
-
286
- it "logs input headers as a hash" do
287
- RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
288
- 'RestClient.get "http://url", :accept=>"text/plain"'
289
- end
290
-
291
- it "logs a response including the status code, content type, and result body size in bytes" do
292
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
293
- res.stub!(:[]).with('Content-type').and_return('text/html')
294
- @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
295
- end
296
-
297
- it "logs a response with a nil Content-type" do
298
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
299
- res.stub!(:[]).with('Content-type').and_return(nil)
300
- @request.response_log(res).should == "# => 200 OK | 4 bytes"
301
- end
302
-
303
- it "logs a response with a nil body" do
304
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
305
- res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
306
- @request.response_log(res).should == "# => 200 OK | text/html 0 bytes"
307
- end
308
-
309
- it "strips the charset from the response content type" do
310
- res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
311
- res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
312
- @request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
313
- end
314
-
315
- it "displays the log to stdout" do
316
- RestClient.stub!(:log).and_return('stdout')
317
- STDOUT.should_receive(:puts).with('xyz')
318
- @request.display_log('xyz')
319
- end
320
-
321
- it "displays the log to stderr" do
322
- RestClient.stub!(:log).and_return('stderr')
323
- STDERR.should_receive(:puts).with('xyz')
324
- @request.display_log('xyz')
325
- end
326
-
327
- it "append the log to the requested filename" do
328
- RestClient.stub!(:log).and_return('/tmp/restclient.log')
329
- f = mock('file handle')
330
- File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
331
- f.should_receive(:puts).with('xyz')
332
- @request.display_log('xyz')
333
- end
334
-
335
- it "set read_timeout" do
336
- @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
337
- @http.stub!(:request)
338
- @request.stub!(:process_result)
339
- @request.stub!(:response_log)
340
-
341
- @net.should_receive(:read_timeout=).with(123)
342
-
343
- @request.transmit(@uri, 'req', nil)
344
- end
345
-
346
- it "set open_timeout" do
347
- @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
348
- @http.stub!(:request)
349
- @request.stub!(:process_result)
350
- @request.stub!(:response_log)
351
-
352
- @net.should_receive(:open_timeout=).with(123)
353
-
354
- @request.transmit(@uri, 'req', nil)
355
- end
356
-
357
- it "should default to not verifying ssl certificates" do
358
- @request.verify_ssl.should == false
359
- end
360
-
361
- it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
362
- @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
363
- @http.stub!(:request)
364
- @request.stub!(:process_result)
365
- @request.stub!(:response_log)
366
- @request.transmit(@uri, 'req', 'payload')
367
- end
368
-
369
- it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
370
- @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
371
- @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
372
- @http.stub!(:request)
373
- @request.stub!(:process_result)
374
- @request.stub!(:response_log)
375
- @request.transmit(@uri, 'req', 'payload')
376
- end
377
-
378
- it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
379
- mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
380
- @request = RestClient::Request.new( :method => :put,
381
- :url => 'https://some/resource',
382
- :payload => 'payload',
383
- :verify_ssl => mode )
384
- @net.should_receive(:verify_mode=).with(mode)
385
- @http.stub!(:request)
386
- @request.stub!(:process_result)
387
- @request.stub!(:response_log)
388
- @request.transmit(@uri, 'req', 'payload')
389
- end
390
-
391
- it "should default to not having an ssl_client_cert" do
392
- @request.ssl_client_cert.should be(nil)
393
- end
394
-
395
- it "should set the ssl_client_cert if provided" do
396
- @request = RestClient::Request.new(
397
- :method => :put,
398
- :url => 'https://some/resource',
399
- :payload => 'payload',
400
- :ssl_client_cert => "whatsupdoc!"
401
- )
402
- @net.should_receive(:cert=).with("whatsupdoc!")
403
- @http.stub!(:request)
404
- @request.stub!(:process_result)
405
- @request.stub!(:response_log)
406
- @request.transmit(@uri, 'req', 'payload')
407
- end
408
-
409
- it "should not set the ssl_client_cert if it is not provided" do
410
- @request = RestClient::Request.new(
411
- :method => :put,
412
- :url => 'https://some/resource',
413
- :payload => 'payload'
414
- )
415
- @net.should_not_receive(:cert=).with("whatsupdoc!")
416
- @http.stub!(:request)
417
- @request.stub!(:process_result)
418
- @request.stub!(:response_log)
419
- @request.transmit(@uri, 'req', 'payload')
420
- end
421
-
422
- it "should default to not having an ssl_client_key" do
423
- @request.ssl_client_key.should be(nil)
424
- end
425
-
426
- it "should set the ssl_client_key if provided" do
427
- @request = RestClient::Request.new(
428
- :method => :put,
429
- :url => 'https://some/resource',
430
- :payload => 'payload',
431
- :ssl_client_key => "whatsupdoc!"
432
- )
433
- @net.should_receive(:key=).with("whatsupdoc!")
434
- @http.stub!(:request)
435
- @request.stub!(:process_result)
436
- @request.stub!(:response_log)
437
- @request.transmit(@uri, 'req', 'payload')
438
- end
439
-
440
- it "should not set the ssl_client_key if it is not provided" do
441
- @request = RestClient::Request.new(
442
- :method => :put,
443
- :url => 'https://some/resource',
444
- :payload => 'payload'
445
- )
446
- @net.should_not_receive(:key=).with("whatsupdoc!")
447
- @http.stub!(:request)
448
- @request.stub!(:process_result)
449
- @request.stub!(:response_log)
450
- @request.transmit(@uri, 'req', 'payload')
451
- end
452
-
453
- it "should default to not having an ssl_ca_file" do
454
- @request.ssl_ca_file.should be(nil)
455
- end
456
-
457
- it "should set the ssl_ca_file if provided" do
458
- @request = RestClient::Request.new(
459
- :method => :put,
460
- :url => 'https://some/resource',
461
- :payload => 'payload',
462
- :ssl_ca_file => "Certificate Authority File"
463
- )
464
- @net.should_receive(:ca_file=).with("Certificate Authority File")
465
- @http.stub!(:request)
466
- @request.stub!(:process_result)
467
- @request.stub!(:response_log)
468
- @request.transmit(@uri, 'req', 'payload')
469
- end
470
-
471
- it "should not set the ssl_ca_file if it is not provided" do
472
- @request = RestClient::Request.new(
473
- :method => :put,
474
- :url => 'https://some/resource',
475
- :payload => 'payload'
476
- )
477
- @net.should_not_receive(:ca_file=).with("Certificate Authority File")
478
- @http.stub!(:request)
479
- @request.stub!(:process_result)
480
- @request.stub!(:response_log)
481
- @request.transmit(@uri, 'req', 'payload')
482
- end
483
-
484
- it "should still return a response object for 204 No Content responses" do
485
- @request = RestClient::Request.new(
486
- :method => :put,
487
- :url => 'https://some/resource',
488
- :payload => 'payload'
489
- )
490
- net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
491
- net_http_res.stub(:read_body).and_return(nil)
492
- @http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
493
- response = @request.transmit(@uri, 'req', 'payload')
494
- response.should_not be_nil
495
- response.code.should equal(204)
496
- end
4
+ before do
5
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
6
+
7
+ @uri = mock("uri")
8
+ @uri.stub!(:request_uri).and_return('/resource')
9
+ @uri.stub!(:host).and_return('some')
10
+ @uri.stub!(:port).and_return(80)
11
+
12
+ @net = mock("net::http base")
13
+ @http = mock("net::http connection")
14
+ Net::HTTP.stub!(:new).and_return(@net)
15
+ @net.stub!(:start).and_yield(@http)
16
+ @net.stub!(:use_ssl=)
17
+ @net.stub!(:verify_mode=)
18
+ RestClient.log = nil
19
+ end
20
+
21
+ it "accept */* mimetype, preferring xml" do
22
+ @request.default_headers[:accept].should == '*/*; q=0.5, application/xml'
23
+ end
24
+
25
+ describe "compression" do
26
+ it "decodes an uncompressed result body by passing it straight through" do
27
+ RestClient::Request.decode(nil, 'xyz').should == 'xyz'
28
+ end
29
+
30
+ it "decodes a gzip body" do
31
+ RestClient::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"
32
+ end
33
+
34
+ it "ingores gzip for empty bodies" do
35
+ RestClient::Request.decode('gzip', '').should be_empty
36
+ end
37
+
38
+ it "decodes a deflated body" do
39
+ RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
40
+ end
41
+ end
42
+
43
+ it "processes a successful result" do
44
+ res = mock("result")
45
+ res.stub!(:code).and_return("200")
46
+ res.stub!(:body).and_return('body')
47
+ res.stub!(:[]).with('content-encoding').and_return(nil)
48
+ @request.process_result(res).should == 'body'
49
+ end
50
+
51
+ it "doesn't classify successful requests as failed" do
52
+ 203.upto(206) do |code|
53
+ res = mock("result")
54
+ res.stub!(:code).and_return(code.to_s)
55
+ res.stub!(:body).and_return("")
56
+ res.stub!(:[]).with('content-encoding').and_return(nil)
57
+ @request.process_result(res).should be_empty
58
+ end
59
+ end
60
+
61
+ it "parses a url into a URI object" do
62
+ URI.should_receive(:parse).with('http://example.com/resource')
63
+ @request.parse_url('http://example.com/resource')
64
+ end
65
+
66
+ it "adds http:// to the front of resources specified in the syntax example.com/resource" do
67
+ URI.should_receive(:parse).with('http://example.com/resource')
68
+ @request.parse_url('example.com/resource')
69
+ end
70
+
71
+ describe "user - password" do
72
+ it "extracts the username and password when parsing http://user:password@example.com/" do
73
+ URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
74
+ @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
75
+ @request.user.should == 'joe'
76
+ @request.password.should == 'pass1'
77
+ end
78
+
79
+ 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
80
+ URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
81
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
82
+ @request.parse_url_with_auth('http://example.com/resource')
83
+ @request.user.should == 'beth'
84
+ @request.password.should == 'pass2'
85
+ end
86
+ end
87
+
88
+ it "correctly formats cookies provided to the constructor" do
89
+ URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
90
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" })
91
+ @request.should_receive(:default_headers).and_return({'foo' => 'bar'})
92
+ headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1,user_id=someone'}
93
+ end
94
+
95
+ it "determines the Net::HTTP class to instantiate by the method name" do
96
+ @request.net_http_request_class(:put).should == Net::HTTP::Put
97
+ end
98
+
99
+ describe "user headers" do
100
+ it "merges user headers with the default headers" do
101
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
102
+ headers = @request.make_headers('3' => '4')
103
+ headers.should have_key('1')
104
+ headers['1'].should == '2'
105
+ headers.should have_key('3')
106
+ headers['3'].should == '4'
107
+ end
108
+
109
+ it "prefers the user header when the same header exists in the defaults" do
110
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
111
+ headers = @request.make_headers('1' => '3')
112
+ headers.should have_key('1')
113
+ headers['1'].should == '3'
114
+ end
115
+ end
116
+
117
+ describe "header symbols" do
118
+
119
+ it "converts header symbols from :content_type to 'Content-type'" do
120
+ @request.should_receive(:default_headers).and_return({})
121
+ headers = @request.make_headers(:content_type => 'abc')
122
+ headers.should have_key('Content-type')
123
+ headers['Content-type'].should == 'abc'
124
+ end
125
+
126
+ it "converts content-type from extension to real content-type" do
127
+ @request.should_receive(:default_headers).and_return({})
128
+ headers = @request.make_headers(:content_type => 'json')
129
+ headers.should have_key('Content-type')
130
+ headers['Content-type'].should == 'application/json'
131
+ end
132
+
133
+ it "converts accept from extension(s) to real content-type(s)" do
134
+ @request.should_receive(:default_headers).and_return({})
135
+ headers = @request.make_headers(:accept => 'json, mp3')
136
+ headers.should have_key('Accept')
137
+ headers['Accept'].should == 'application/json, audio/mpeg'
138
+
139
+ @request.should_receive(:default_headers).and_return({})
140
+ headers = @request.make_headers(:accept => :json)
141
+ headers.should have_key('Accept')
142
+ headers['Accept'].should == 'application/json'
143
+ end
144
+
145
+ it "converts header values to strings" do
146
+ @request.make_headers('A' => 1)['A'].should == '1'
147
+ end
148
+ end
149
+
150
+ it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
151
+ @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
152
+ klass = mock("net:http class")
153
+ @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
154
+ klass.should_receive(:new).and_return('result')
155
+ @request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
156
+ @request.execute_inner
157
+ end
158
+
159
+ it "transmits the request with Net::HTTP" do
160
+ @http.should_receive(:request).with('req', 'payload')
161
+ @request.should_receive(:process_result)
162
+ @request.transmit(@uri, 'req', 'payload')
163
+ end
164
+
165
+ describe "payload" do
166
+ it "sends nil payloads" do
167
+ @http.should_receive(:request).with('req', nil)
168
+ @request.should_receive(:process_result)
169
+ @request.stub!(:response_log)
170
+ @request.transmit(@uri, 'req', nil)
171
+ end
172
+
173
+ it "passes non-hash payloads straight through" do
174
+ @request.process_payload("x").should == "x"
175
+ end
176
+
177
+ it "converts a hash payload to urlencoded data" do
178
+ @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
179
+ end
180
+
181
+ it "accepts nested hashes in payload" do
182
+ payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
183
+ payload.should include('user[name]=joe')
184
+ payload.should include('user[location][country]=USA')
185
+ payload.should include('user[location][state]=CA')
186
+ end
187
+ end
188
+
189
+ it "set urlencoded content_type header on hash payloads" do
190
+ @request.process_payload(:a => 1)
191
+ @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
192
+ end
193
+
194
+ describe "credentials" do
195
+ it "sets up the credentials prior to the request" do
196
+ @http.stub!(:request)
197
+ @request.stub!(:process_result)
198
+ @request.stub!(:response_log)
199
+
200
+ @request.stub!(:user).and_return('joe')
201
+ @request.stub!(:password).and_return('mypass')
202
+ @request.should_receive(:setup_credentials).with('req')
203
+
204
+ @request.transmit(@uri, 'req', nil)
205
+ end
206
+
207
+ it "does not attempt to send any credentials if user is nil" do
208
+ @request.stub!(:user).and_return(nil)
209
+ req = mock("request")
210
+ req.should_not_receive(:basic_auth)
211
+ @request.setup_credentials(req)
212
+ end
213
+
214
+ it "setup credentials when there's a user" do
215
+ @request.stub!(:user).and_return('joe')
216
+ @request.stub!(:password).and_return('mypass')
217
+ req = mock("request")
218
+ req.should_receive(:basic_auth).with('joe', 'mypass')
219
+ @request.setup_credentials(req)
220
+ end
221
+ end
222
+
223
+ it "catches EOFError and shows the more informative ServerBrokeConnection" do
224
+ @http.stub!(:request).and_raise(EOFError)
225
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
226
+ end
227
+
228
+ it "execute calls execute_inner" do
229
+ @request.should_receive(:execute_inner)
230
+ @request.execute
231
+ end
232
+
233
+ it "class method execute wraps constructor" do
234
+ req = mock("rest request")
235
+ RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
236
+ req.should_receive(:execute)
237
+ RestClient::Request.execute(1 => 2)
238
+ end
239
+
240
+ describe "redirection" do
241
+ it "raises a Redirect with the new location when the response is in the 30x range" do
242
+ res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource'}, :[] => ['content-encoding' => ''], :body => '' )
243
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
244
+ end
245
+
246
+ it "handles redirects with relative paths" do
247
+ res = mock('response', :code => '301', :header => { 'Location' => 'index' }, :[] => ['content-encoding' => ''], :body => '' )
248
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
249
+ end
250
+
251
+ it "handles redirects with absolute paths" do
252
+ @request.instance_variable_set('@url', 'http://some/place/else')
253
+ res = mock('response', :code => '301', :header => { 'Location' => '/index' }, :[] => ['content-encoding' => ''], :body => '' )
254
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
255
+ end
256
+
257
+ it "uses GET and clears payload and removes possible harmful headers when following 30x redirects" do
258
+ url = "http://example.com/redirected"
259
+
260
+ @request.should_receive(:execute_inner).once.ordered.and_raise(RestClient::Redirect.new(url))
261
+
262
+ @request.should_receive(:execute_inner).once.ordered do
263
+ @request.processed_headers.should_not have_key("Content-Length")
264
+ @request.url.should == url
265
+ @request.method.should == :get
266
+ @request.payload.should be_nil
267
+ end
268
+
269
+ @request.execute
270
+ end
271
+ end
272
+
273
+ describe "exception" do
274
+ it "raises Unauthorized when the response is 401" do
275
+ res = mock('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
276
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
277
+ end
278
+
279
+ it "raises ResourceNotFound when the response is 404" do
280
+ res = mock('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' )
281
+ lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
282
+ end
283
+
284
+ it "raises RequestFailed otherwise" do
285
+ res = mock('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' )
286
+ lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError)
287
+ end
288
+ end
289
+
290
+ describe "block usage" do
291
+ it "returns what asked to" do
292
+ res = mock('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
293
+ @request.process_result(res){|response| "foo"}.should == "foo"
294
+ end
295
+ end
296
+
297
+ describe "proxy" do
298
+ it "creates a proxy class if a proxy url is given" do
299
+ RestClient.stub!(:proxy).and_return("http://example.com/")
300
+ @request.net_http_class.should include(Net::HTTP::ProxyDelta)
301
+ end
302
+
303
+ it "creates a non-proxy class if a proxy url is not given" do
304
+ @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
305
+ end
306
+ end
307
+
308
+
309
+ describe "logging" do
310
+ it "logs a get request" do
311
+ log = RestClient.log = []
312
+ RestClient::Request.new(:method => :get, :url => 'http://url').log_request
313
+ ['RestClient.get "http://url", "Accept-encoding"=>"gzip, deflate", "Accept"=>"*/*; q=0.5, application/xml"' + "\n",
314
+ 'RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate"' + "\n"].should include(log[0])
315
+ end
316
+
317
+ it "logs a post request with a small payload" do
318
+ log = RestClient.log = []
319
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request
320
+ ['RestClient.post "http://url", "foo", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"3", "Accept"=>"*/*; q=0.5, application/xml"' + "\n",
321
+ 'RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"3"' + "\n"].should include(log[0])
322
+ end
323
+
324
+ it "logs a post request with a large payload" do
325
+ log = RestClient.log = []
326
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request
327
+ ['RestClient.post "http://url", 1000 byte length, "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"1000", "Accept"=>"*/*; q=0.5, application/xml"' + "\n",
328
+ 'RestClient.post "http://url", 1000 byte length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"1000"' + "\n"].should include(log[0])
329
+ end
330
+
331
+ it "logs input headers as a hash" do
332
+ log = RestClient.log = []
333
+ RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request
334
+ ['RestClient.get "http://url", "Accept-encoding"=>"gzip, deflate", "Accept"=>"text/plain"' + "\n",
335
+ 'RestClient.get "http://url", "Accept"=>"text/plain", "Accept-encoding"=>"gzip, deflate"' + "\n"].should include(log[0])
336
+ end
337
+
338
+ it "logs a response including the status code, content type, and result body size in bytes" do
339
+ log = RestClient.log = []
340
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
341
+ res.stub!(:[]).with('Content-type').and_return('text/html')
342
+ @request.log_response res
343
+ log[0].should == "# => 200 OK | text/html 4 bytes\n"
344
+ end
345
+
346
+ it "logs a response with a nil Content-type" do
347
+ log = RestClient.log = []
348
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
349
+ res.stub!(:[]).with('Content-type').and_return(nil)
350
+ @request.log_response res
351
+ log[0].should == "# => 200 OK | 4 bytes\n"
352
+ end
353
+
354
+ it "logs a response with a nil body" do
355
+ log = RestClient.log = []
356
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
357
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
358
+ @request.log_response res
359
+ log[0].should == "# => 200 OK | text/html 0 bytes\n"
360
+ end
361
+ end
362
+
363
+ it "strips the charset from the response content type" do
364
+ log = RestClient.log = []
365
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
366
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
367
+ @request.log_response res
368
+ log[0].should == "# => 200 OK | text/html 4 bytes\n"
369
+ end
370
+
371
+ describe "timeout" do
372
+ it "set read_timeout" do
373
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
374
+ @http.stub!(:request)
375
+ @request.stub!(:process_result)
376
+ @request.stub!(:response_log)
377
+
378
+ @net.should_receive(:read_timeout=).with(123)
379
+
380
+ @request.transmit(@uri, 'req', nil)
381
+ end
382
+
383
+ it "set open_timeout" do
384
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
385
+ @http.stub!(:request)
386
+ @request.stub!(:process_result)
387
+ @request.stub!(:response_log)
388
+
389
+ @net.should_receive(:open_timeout=).with(123)
390
+
391
+ @request.transmit(@uri, 'req', nil)
392
+ end
393
+ end
394
+
395
+ describe "ssl" do
396
+ it "uses SSL when the URI refers to a https address" do
397
+ @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
398
+ @net.should_receive(:use_ssl=).with(true)
399
+ @http.stub!(:request)
400
+ @request.stub!(:process_result)
401
+ @request.stub!(:response_log)
402
+ @request.transmit(@uri, 'req', 'payload')
403
+ end
404
+
405
+ it "should default to not verifying ssl certificates" do
406
+ @request.verify_ssl.should == false
407
+ end
408
+
409
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
410
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
411
+ @http.stub!(:request)
412
+ @request.stub!(:process_result)
413
+ @request.stub!(:response_log)
414
+ @request.transmit(@uri, 'req', 'payload')
415
+ end
416
+
417
+ it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
418
+ @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
419
+ @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
420
+ @http.stub!(:request)
421
+ @request.stub!(:process_result)
422
+ @request.stub!(:response_log)
423
+ @request.transmit(@uri, 'req', 'payload')
424
+ end
425
+
426
+ it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
427
+ mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
428
+ @request = RestClient::Request.new( :method => :put,
429
+ :url => 'https://some/resource',
430
+ :payload => 'payload',
431
+ :verify_ssl => mode )
432
+ @net.should_receive(:verify_mode=).with(mode)
433
+ @http.stub!(:request)
434
+ @request.stub!(:process_result)
435
+ @request.stub!(:response_log)
436
+ @request.transmit(@uri, 'req', 'payload')
437
+ end
438
+
439
+ it "should default to not having an ssl_client_cert" do
440
+ @request.ssl_client_cert.should be(nil)
441
+ end
442
+
443
+ it "should set the ssl_client_cert if provided" do
444
+ @request = RestClient::Request.new(
445
+ :method => :put,
446
+ :url => 'https://some/resource',
447
+ :payload => 'payload',
448
+ :ssl_client_cert => "whatsupdoc!"
449
+ )
450
+ @net.should_receive(:cert=).with("whatsupdoc!")
451
+ @http.stub!(:request)
452
+ @request.stub!(:process_result)
453
+ @request.stub!(:response_log)
454
+ @request.transmit(@uri, 'req', 'payload')
455
+ end
456
+
457
+ it "should not set the ssl_client_cert if it is not provided" do
458
+ @request = RestClient::Request.new(
459
+ :method => :put,
460
+ :url => 'https://some/resource',
461
+ :payload => 'payload'
462
+ )
463
+ @net.should_not_receive(:cert=).with("whatsupdoc!")
464
+ @http.stub!(:request)
465
+ @request.stub!(:process_result)
466
+ @request.stub!(:response_log)
467
+ @request.transmit(@uri, 'req', 'payload')
468
+ end
469
+
470
+ it "should default to not having an ssl_client_key" do
471
+ @request.ssl_client_key.should be(nil)
472
+ end
473
+
474
+ it "should set the ssl_client_key if provided" do
475
+ @request = RestClient::Request.new(
476
+ :method => :put,
477
+ :url => 'https://some/resource',
478
+ :payload => 'payload',
479
+ :ssl_client_key => "whatsupdoc!"
480
+ )
481
+ @net.should_receive(:key=).with("whatsupdoc!")
482
+ @http.stub!(:request)
483
+ @request.stub!(:process_result)
484
+ @request.stub!(:response_log)
485
+ @request.transmit(@uri, 'req', 'payload')
486
+ end
487
+
488
+ it "should not set the ssl_client_key if it is not provided" do
489
+ @request = RestClient::Request.new(
490
+ :method => :put,
491
+ :url => 'https://some/resource',
492
+ :payload => 'payload'
493
+ )
494
+ @net.should_not_receive(:key=).with("whatsupdoc!")
495
+ @http.stub!(:request)
496
+ @request.stub!(:process_result)
497
+ @request.stub!(:response_log)
498
+ @request.transmit(@uri, 'req', 'payload')
499
+ end
500
+
501
+ it "should default to not having an ssl_ca_file" do
502
+ @request.ssl_ca_file.should be(nil)
503
+ end
504
+
505
+ it "should set the ssl_ca_file if provided" do
506
+ @request = RestClient::Request.new(
507
+ :method => :put,
508
+ :url => 'https://some/resource',
509
+ :payload => 'payload',
510
+ :ssl_ca_file => "Certificate Authority File"
511
+ )
512
+ @net.should_receive(:ca_file=).with("Certificate Authority File")
513
+ @http.stub!(:request)
514
+ @request.stub!(:process_result)
515
+ @request.stub!(:response_log)
516
+ @request.transmit(@uri, 'req', 'payload')
517
+ end
518
+
519
+ it "should not set the ssl_ca_file if it is not provided" do
520
+ @request = RestClient::Request.new(
521
+ :method => :put,
522
+ :url => 'https://some/resource',
523
+ :payload => 'payload'
524
+ )
525
+ @net.should_not_receive(:ca_file=).with("Certificate Authority File")
526
+ @http.stub!(:request)
527
+ @request.stub!(:process_result)
528
+ @request.stub!(:response_log)
529
+ @request.transmit(@uri, 'req', 'payload')
530
+ end
531
+ end
532
+
533
+ it "should still return a response object for 204 No Content responses" do
534
+ @request = RestClient::Request.new(
535
+ :method => :put,
536
+ :url => 'https://some/resource',
537
+ :payload => 'payload'
538
+ )
539
+ net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
540
+ net_http_res.stub!(:read_body).and_return(nil)
541
+ @http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
542
+ response = @request.transmit(@uri, 'req', 'payload')
543
+ response.should_not be_nil
544
+ response.code.should equal(204)
545
+ end
497
546
  end