httparty 0.13.1 → 0.13.2

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

Potentially problematic release.


This version of httparty might be problematic. Click here for more details.

Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +3 -3
  3. data/History +74 -11
  4. data/README.md +4 -1
  5. data/Rakefile +1 -2
  6. data/bin/httparty +4 -4
  7. data/examples/README.md +64 -0
  8. data/examples/aaws.rb +3 -3
  9. data/examples/basic.rb +5 -5
  10. data/examples/crack.rb +1 -1
  11. data/examples/delicious.rb +5 -5
  12. data/examples/headers_and_user_agents.rb +1 -1
  13. data/examples/logging.rb +38 -0
  14. data/examples/rubyurl.rb +1 -1
  15. data/examples/stackexchange.rb +1 -1
  16. data/examples/tripit_sign_in.rb +5 -5
  17. data/examples/twitter.rb +5 -5
  18. data/examples/whoismyrep.rb +1 -1
  19. data/features/steps/httparty_steps.rb +2 -2
  20. data/httparty.gemspec +2 -0
  21. data/lib/httparty.rb +35 -17
  22. data/lib/httparty/connection_adapter.rb +4 -2
  23. data/lib/httparty/cookie_hash.rb +1 -1
  24. data/lib/httparty/hash_conversions.rb +12 -12
  25. data/lib/httparty/logger/apache_logger.rb +1 -1
  26. data/lib/httparty/logger/logger.rb +1 -1
  27. data/lib/httparty/net_digest_auth.rb +4 -1
  28. data/lib/httparty/request.rb +41 -21
  29. data/lib/httparty/version.rb +1 -1
  30. data/spec/httparty/connection_adapter_spec.rb +52 -36
  31. data/spec/httparty/cookie_hash_spec.rb +8 -8
  32. data/spec/httparty/logger/apache_logger_spec.rb +29 -14
  33. data/spec/httparty/net_digest_auth_spec.rb +11 -0
  34. data/spec/httparty/parser_spec.rb +10 -10
  35. data/spec/httparty/request_spec.rb +209 -30
  36. data/spec/httparty/response_spec.rb +41 -41
  37. data/spec/httparty/ssl_spec.rb +4 -4
  38. data/spec/httparty_spec.rb +78 -59
  39. data/spec/support/ssl_test_helper.rb +6 -6
  40. data/spec/support/stub_response.rb +2 -2
  41. data/website/index.html +3 -3
  42. metadata +15 -14
  43. data/spec/spec.opts +0 -2
@@ -1,3 +1,3 @@
1
1
  module HTTParty
2
- VERSION = "0.13.1"
2
+ VERSION = "0.13.2"
3
3
  end
@@ -26,7 +26,7 @@ describe HTTParty::ConnectionAdapter do
26
26
  end
27
27
 
28
28
  it "sets the options" do
29
- options = {:foo => :bar}
29
+ options = {foo: :bar}
30
30
  adapter = HTTParty::ConnectionAdapter.new(uri, options)
31
31
  adapter.options.should be options
32
32
  end
@@ -34,7 +34,7 @@ describe HTTParty::ConnectionAdapter do
34
34
 
35
35
  describe ".call" do
36
36
  it "generates an HTTParty::ConnectionAdapter instance with the given uri and options" do
37
- HTTParty::ConnectionAdapter.should_receive(:new).with(@uri, @options).and_return(stub(:connection => nil))
37
+ HTTParty::ConnectionAdapter.should_receive(:new).with(@uri, @options).and_return(stub(connection: nil))
38
38
  HTTParty::ConnectionAdapter.call(@uri, @options)
39
39
  end
40
40
 
@@ -42,7 +42,7 @@ describe HTTParty::ConnectionAdapter do
42
42
  adapter = mock('Adapter')
43
43
  connection = mock('Connection')
44
44
  adapter.should_receive(:connection).and_return(connection)
45
- HTTParty::ConnectionAdapter.stub(:new => adapter)
45
+ HTTParty::ConnectionAdapter.stub(new: adapter)
46
46
  HTTParty::ConnectionAdapter.call(@uri, @options).should be connection
47
47
  end
48
48
  end
@@ -76,7 +76,7 @@ describe HTTParty::ConnectionAdapter do
76
76
 
77
77
  context "should use the specified cert store, when one is given" do
78
78
  let(:custom_cert_store) { mock('custom_cert_store') }
79
- let(:options) { {:cert_store => custom_cert_store} }
79
+ let(:options) { {cert_store: custom_cert_store} }
80
80
  it { should use_cert_store(custom_cert_store) }
81
81
  end
82
82
 
@@ -96,7 +96,7 @@ describe HTTParty::ConnectionAdapter do
96
96
 
97
97
 
98
98
  context "when ssl version is set" do
99
- let(:options) { {:ssl_version => :TLSv1} }
99
+ let(:options) { {ssl_version: :TLSv1} }
100
100
 
101
101
  it "sets ssl version" do
102
102
  subject.ssl_version.should == :TLSv1
@@ -113,7 +113,7 @@ describe HTTParty::ConnectionAdapter do
113
113
  end
114
114
 
115
115
  context "specifying ciphers" do
116
- let(:options) { {:ciphers => 'RC4-SHA' } }
116
+ let(:options) { {ciphers: 'RC4-SHA' } }
117
117
 
118
118
  it "should set the ciphers on the connection" do
119
119
  subject.ciphers.should == 'RC4-SHA'
@@ -122,10 +122,10 @@ describe HTTParty::ConnectionAdapter do
122
122
 
123
123
  context "when timeout is not set" do
124
124
  it "doesn't set the timeout" do
125
- http = mock("http", :null_object => true)
125
+ http = mock("http", null_object: true)
126
126
  http.should_not_receive(:open_timeout=)
127
127
  http.should_not_receive(:read_timeout=)
128
- Net::HTTP.stub(:new => http)
128
+ Net::HTTP.stub(new: http)
129
129
 
130
130
  adapter.connection
131
131
  end
@@ -133,20 +133,20 @@ describe HTTParty::ConnectionAdapter do
133
133
 
134
134
  context "when setting timeout" do
135
135
  context "to 5 seconds" do
136
- let(:options) { {:timeout => 5} }
136
+ let(:options) { {timeout: 5} }
137
137
 
138
138
  its(:open_timeout) { should == 5 }
139
139
  its(:read_timeout) { should == 5 }
140
140
  end
141
141
 
142
142
  context "and timeout is a string" do
143
- let(:options) { {:timeout => "five seconds"} }
143
+ let(:options) { {timeout: "five seconds"} }
144
144
 
145
145
  it "doesn't set the timeout" do
146
- http = mock("http", :null_object => true)
146
+ http = mock("http", null_object: true)
147
147
  http.should_not_receive(:open_timeout=)
148
148
  http.should_not_receive(:read_timeout=)
149
- Net::HTTP.stub(:new => http)
149
+ Net::HTTP.stub(new: http)
150
150
 
151
151
  adapter.connection
152
152
  end
@@ -154,57 +154,57 @@ describe HTTParty::ConnectionAdapter do
154
154
  end
155
155
 
156
156
  context "when timeout is not set and read_timeout is set to 6 seconds" do
157
- let(:options) { {:read_timeout => 6} }
157
+ let(:options) { {read_timeout: 6} }
158
158
 
159
159
  its(:read_timeout) { should == 6 }
160
160
 
161
161
  it "should not set the open_timeout" do
162
- http = mock("http", :null_object => true)
162
+ http = mock("http", null_object: true)
163
163
  http.should_not_receive(:open_timeout=)
164
- Net::HTTP.stub(:new => http)
164
+ Net::HTTP.stub(new: http)
165
165
  adapter.connection
166
166
  end
167
167
  end
168
168
 
169
169
  context "when timeout is set and read_timeout is set to 6 seconds" do
170
- let(:options) { {:timeout => 5, :read_timeout => 6} }
170
+ let(:options) { {timeout: 5, read_timeout: 6} }
171
171
 
172
172
  its(:open_timeout) { should == 5 }
173
173
  its(:read_timeout) { should == 6 }
174
174
 
175
175
  it "should override the timeout option" do
176
- http = mock("http", :null_object => true)
176
+ http = mock("http", null_object: true)
177
177
  http.should_receive(:open_timeout=)
178
178
  http.should_receive(:read_timeout=).twice
179
- Net::HTTP.stub(:new => http)
179
+ Net::HTTP.stub(new: http)
180
180
  adapter.connection
181
181
  end
182
182
  end
183
183
 
184
184
  context "when timeout is not set and open_timeout is set to 7 seconds" do
185
- let(:options) { {:open_timeout => 7} }
185
+ let(:options) { {open_timeout: 7} }
186
186
 
187
187
  its(:open_timeout) { should == 7 }
188
188
 
189
189
  it "should not set the read_timeout" do
190
- http = mock("http", :null_object => true)
190
+ http = mock("http", null_object: true)
191
191
  http.should_not_receive(:read_timeout=)
192
- Net::HTTP.stub(:new => http)
192
+ Net::HTTP.stub(new: http)
193
193
  adapter.connection
194
194
  end
195
195
  end
196
196
 
197
197
  context "when timeout is set and open_timeout is set to 7 seconds" do
198
- let(:options) { {:timeout => 5, :open_timeout => 7} }
198
+ let(:options) { {timeout: 5, open_timeout: 7} }
199
199
 
200
200
  its(:open_timeout) { should == 7 }
201
201
  its(:read_timeout) { should == 5 }
202
202
 
203
203
  it "should override the timeout option" do
204
- http = mock("http", :null_object => true)
204
+ http = mock("http", null_object: true)
205
205
  http.should_receive(:open_timeout=).twice
206
206
  http.should_receive(:read_timeout=)
207
- Net::HTTP.stub(:new => http)
207
+ Net::HTTP.stub(new: http)
208
208
  adapter.connection
209
209
  end
210
210
  end
@@ -212,11 +212,11 @@ describe HTTParty::ConnectionAdapter do
212
212
  context "when debug_output" do
213
213
  let(:http) { Net::HTTP.new(uri) }
214
214
  before do
215
- Net::HTTP.stub(:new => http)
215
+ Net::HTTP.stub(new: http)
216
216
  end
217
217
 
218
218
  context "is set to $stderr" do
219
- let(:options) { {:debug_output => $stderr} }
219
+ let(:options) { {debug_output: $stderr} }
220
220
  it "has debug output set" do
221
221
  http.should_receive(:set_debug_output).with($stderr)
222
222
  adapter.connection
@@ -232,7 +232,7 @@ describe HTTParty::ConnectionAdapter do
232
232
  end
233
233
 
234
234
  context 'when providing proxy address and port' do
235
- let(:options) { {:http_proxyaddr => '1.2.3.4', :http_proxyport => 8080} }
235
+ let(:options) { {http_proxyaddr: '1.2.3.4', http_proxyport: 8080} }
236
236
 
237
237
  it { should be_a_proxy }
238
238
  its(:proxy_address) { should == '1.2.3.4' }
@@ -240,8 +240,8 @@ describe HTTParty::ConnectionAdapter do
240
240
 
241
241
  context 'as well as proxy user and password' do
242
242
  let(:options) do
243
- {:http_proxyaddr => '1.2.3.4', :http_proxyport => 8080,
244
- :http_proxyuser => 'user', :http_proxypass => 'pass'}
243
+ {http_proxyaddr: '1.2.3.4', http_proxyport: 8080,
244
+ http_proxyuser: 'user', http_proxypass: 'pass'}
245
245
  end
246
246
  its(:proxy_user) { should == 'user' }
247
247
  its(:proxy_pass) { should == 'pass' }
@@ -259,7 +259,7 @@ describe HTTParty::ConnectionAdapter do
259
259
  end
260
260
 
261
261
  context 'when providing a local bind address and port' do
262
- let(:options) { {:local_host => "127.0.0.1", :local_port => 12345 } }
262
+ let(:options) { {local_host: "127.0.0.1", local_port: 12345 } }
263
263
 
264
264
  its(:local_host) { should == '127.0.0.1' }
265
265
  its(:local_port) { should == 12345 }
@@ -267,7 +267,7 @@ describe HTTParty::ConnectionAdapter do
267
267
 
268
268
  context "when providing PEM certificates" do
269
269
  let(:pem) { :pem_contents }
270
- let(:options) { {:pem => pem, :pem_password => "password"} }
270
+ let(:options) { {pem: pem, pem_password: "password"} }
271
271
 
272
272
  context "when scheme is https" do
273
273
  let(:uri) { URI 'https://google.com' }
@@ -279,7 +279,7 @@ describe HTTParty::ConnectionAdapter do
279
279
  OpenSSL::PKey::RSA.should_receive(:new).with(pem, "password").and_return(key)
280
280
  end
281
281
 
282
- it "uses the provided PEM certificate " do
282
+ it "uses the provided PEM certificate" do
283
283
  subject.cert.should == cert
284
284
  subject.key.should == key
285
285
  end
@@ -287,6 +287,14 @@ describe HTTParty::ConnectionAdapter do
287
287
  it "will verify the certificate" do
288
288
  subject.verify_mode.should == OpenSSL::SSL::VERIFY_PEER
289
289
  end
290
+
291
+ context "when options include verify_peer=false" do
292
+ let(:options) { {pem: pem, pem_password: "password", verify_peer: false} }
293
+
294
+ it "should not verify the certificate" do
295
+ subject.verify_mode.should == OpenSSL::SSL::VERIFY_NONE
296
+ end
297
+ end
290
298
  end
291
299
 
292
300
  context "when scheme is not https" do
@@ -294,7 +302,7 @@ describe HTTParty::ConnectionAdapter do
294
302
  let(:http) { Net::HTTP.new(uri) }
295
303
 
296
304
  before do
297
- Net::HTTP.stub(:new => http)
305
+ Net::HTTP.stub(new: http)
298
306
  OpenSSL::X509::Certificate.should_not_receive(:new).with(pem)
299
307
  OpenSSL::PKey::RSA.should_not_receive(:new).with(pem, "password")
300
308
  http.should_not_receive(:cert=)
@@ -310,11 +318,11 @@ describe HTTParty::ConnectionAdapter do
310
318
 
311
319
  context "when providing PKCS12 certificates" do
312
320
  let(:p12) { :p12_contents }
313
- let(:options) { {:p12 => p12, :p12_password => "password"} }
321
+ let(:options) { {p12: p12, p12_password: "password"} }
314
322
 
315
323
  context "when scheme is https" do
316
324
  let(:uri) { URI 'https://google.com' }
317
- let(:pkcs12) { mock("OpenSSL::PKCS12", :certificate => cert, :key => key) }
325
+ let(:pkcs12) { mock("OpenSSL::PKCS12", certificate: cert, key: key) }
318
326
  let(:cert) { mock("OpenSSL::X509::Certificate") }
319
327
  let(:key) { mock("OpenSSL::PKey::RSA") }
320
328
 
@@ -330,6 +338,14 @@ describe HTTParty::ConnectionAdapter do
330
338
  it "will verify the certificate" do
331
339
  subject.verify_mode.should == OpenSSL::SSL::VERIFY_PEER
332
340
  end
341
+
342
+ context "when options include verify_peer=false" do
343
+ let(:options) { {p12: p12, p12_password: "password", verify_peer: false} }
344
+
345
+ it "should not verify the certificate" do
346
+ subject.verify_mode.should == OpenSSL::SSL::VERIFY_NONE
347
+ end
348
+ end
333
349
  end
334
350
 
335
351
  context "when scheme is not https" do
@@ -337,7 +353,7 @@ describe HTTParty::ConnectionAdapter do
337
353
  let(:http) { Net::HTTP.new(uri) }
338
354
 
339
355
  before do
340
- Net::HTTP.stub(:new => http)
356
+ Net::HTTP.stub(new: http)
341
357
  OpenSSL::PKCS12.new.should_not_receive(:new).with(p12, "password")
342
358
  http.should_not_receive(:cert=)
343
359
  http.should_not_receive(:key=)
@@ -8,14 +8,14 @@ describe HTTParty::CookieHash do
8
8
  describe "#add_cookies" do
9
9
  describe "with a hash" do
10
10
  it "should add new key/value pairs to the hash" do
11
- @cookie_hash.add_cookies(:foo => "bar")
12
- @cookie_hash.add_cookies(:rofl => "copter")
11
+ @cookie_hash.add_cookies(foo: "bar")
12
+ @cookie_hash.add_cookies(rofl: "copter")
13
13
  @cookie_hash.length.should eql(2)
14
14
  end
15
15
 
16
16
  it "should overwrite any existing key" do
17
- @cookie_hash.add_cookies(:foo => "bar")
18
- @cookie_hash.add_cookies(:foo => "copter")
17
+ @cookie_hash.add_cookies(foo: "bar")
18
+ @cookie_hash.add_cookies(foo: "copter")
19
19
  @cookie_hash.length.should eql(1)
20
20
  @cookie_hash[:foo].should eql("copter")
21
21
  end
@@ -57,8 +57,8 @@ describe HTTParty::CookieHash do
57
57
  # a hardcoded string was randomly failing.
58
58
  describe "#to_cookie_string" do
59
59
  before(:each) do
60
- @cookie_hash.add_cookies(:foo => "bar")
61
- @cookie_hash.add_cookies(:rofl => "copter")
60
+ @cookie_hash.add_cookies(foo: "bar")
61
+ @cookie_hash.add_cookies(rofl: "copter")
62
62
  @s = @cookie_hash.to_cookie_string
63
63
  end
64
64
 
@@ -69,13 +69,13 @@ describe HTTParty::CookieHash do
69
69
  end
70
70
 
71
71
  it "should not include client side only cookies" do
72
- @cookie_hash.add_cookies(:path => "/")
72
+ @cookie_hash.add_cookies(path: "/")
73
73
  @s = @cookie_hash.to_cookie_string
74
74
  @s.should_not match(/path=\//)
75
75
  end
76
76
 
77
77
  it "should not include client side only cookies even when attributes use camal case" do
78
- @cookie_hash.add_cookies(:Path => "/")
78
+ @cookie_hash.add_cookies(Path: "/")
79
79
  @s = @cookie_hash.to_cookie_string
80
80
  @s.should_not match(/Path=\//)
81
81
  end
@@ -1,26 +1,41 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
2
 
3
3
  describe HTTParty::Logger::ApacheLogger do
4
+ let(:subject) { described_class.new(logger_double, :info) }
5
+ let(:logger_double) { double('Logger') }
6
+ let(:request_double) { double('Request', http_method: Net::HTTP::Get, path: "http://my.domain.com/my_path") }
7
+ let(:request_time) { Time.new.strftime("%Y-%m-%d %H:%M:%S %z") }
8
+
9
+ before do
10
+ subject.current_time = request_time
11
+ logger_double.should_receive(:info).with(log_message)
12
+ end
13
+
4
14
  describe "#format" do
5
- it "formats a response in a style that resembles apache's access log" do
6
- request_time = Time.new.strftime("%Y-%m-%d %H:%M:%S %z")
7
- log_message = "[HTTParty] [#{request_time}] 302 \"GET http://my.domain.com/my_path\" - "
15
+ let(:log_message) { "[HTTParty] [#{request_time}] 302 \"GET http://my.domain.com/my_path\" - " }
8
16
 
9
- request_double = double(
10
- :http_method => Net::HTTP::Get,
11
- :path => "http://my.domain.com/my_path"
12
- )
17
+ it "formats a response in a style that resembles apache's access log" do
13
18
  response_double = double(
14
- :code => 302,
15
- :[] => nil
19
+ code: 302,
20
+ :[] => nil
16
21
  )
17
22
 
18
- logger_double = double
19
- logger_double.should_receive(:info).with(log_message)
20
-
21
- subject = described_class.new(logger_double, :info)
22
- subject.current_time = request_time
23
23
  subject.format(request_double, response_double)
24
24
  end
25
+
26
+ context 'when there is a parsed response' do
27
+ let(:log_message) { "[HTTParty] [#{request_time}] 200 \"GET http://my.domain.com/my_path\" 512 "}
28
+
29
+ it "can handle the Content-Length header" do
30
+ # Simulate a parsed response that is an array, where accessing a string key will raise an error. See Issue #299.
31
+ response_double = double(
32
+ code: 200,
33
+ headers: { 'Content-Length' => 512 }
34
+ )
35
+ response_double.stub(:[]).with('Content-Length').and_raise(TypeError.new('no implicit conversion of String into Integer'))
36
+
37
+ subject.format(request_double, response_double)
38
+ end
39
+ end
25
40
  end
26
41
  end
@@ -75,6 +75,17 @@ describe Net::HTTPHeader::DigestAuthenticator do
75
75
  end
76
76
  end
77
77
 
78
+ context "when quality of protection (qop) is unquoted" do
79
+ before do
80
+ @digest = setup_digest({
81
+ 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop=auth',
82
+ })
83
+ end
84
+
85
+ it "should still set qop" do
86
+ authorization_header.should include(%Q(qop="auth"))
87
+ end
88
+ end
78
89
 
79
90
  context "with unspecified quality of protection (qop)" do
80
91
  before do
@@ -9,14 +9,14 @@ describe HTTParty::Parser do
9
9
 
10
10
  describe ".call" do
11
11
  it "generates an HTTParty::Parser instance with the given body and format" do
12
- HTTParty::Parser.should_receive(:new).with('body', :plain).and_return(stub(:parse => nil))
12
+ HTTParty::Parser.should_receive(:new).with('body', :plain).and_return(stub(parse: nil))
13
13
  HTTParty::Parser.call('body', :plain)
14
14
  end
15
15
 
16
16
  it "calls #parse on the parser" do
17
17
  parser = mock('Parser')
18
18
  parser.should_receive(:parse)
19
- HTTParty::Parser.stub(:new => parser)
19
+ HTTParty::Parser.stub(new: parser)
20
20
  parser = HTTParty::Parser.call('body', :plain)
21
21
  end
22
22
  end
@@ -52,12 +52,12 @@ describe HTTParty::Parser do
52
52
 
53
53
  describe ".supports_format?" do
54
54
  it "returns true for a supported format" do
55
- HTTParty::Parser.stub(:supported_formats => [:json])
55
+ HTTParty::Parser.stub(supported_formats: [:json])
56
56
  HTTParty::Parser.supports_format?(:json).should be_true
57
57
  end
58
58
 
59
59
  it "returns false for an unsupported format" do
60
- HTTParty::Parser.stub(:supported_formats => [])
60
+ HTTParty::Parser.stub(supported_formats: [])
61
61
  HTTParty::Parser.supports_format?(:json).should be_false
62
62
  end
63
63
  end
@@ -68,33 +68,33 @@ describe HTTParty::Parser do
68
68
  end
69
69
 
70
70
  it "attempts to parse supported formats" do
71
- @parser.stub(:supports_format? => true)
71
+ @parser.stub(supports_format?: true)
72
72
  @parser.should_receive(:parse_supported_format)
73
73
  @parser.parse
74
74
  end
75
75
 
76
76
  it "returns the unparsed body when the format is unsupported" do
77
- @parser.stub(:supports_format? => false)
77
+ @parser.stub(supports_format?: false)
78
78
  @parser.parse.should == @parser.body
79
79
  end
80
80
 
81
81
  it "returns nil for an empty body" do
82
- @parser.stub(:body => '')
82
+ @parser.stub(body: '')
83
83
  @parser.parse.should be_nil
84
84
  end
85
85
 
86
86
  it "returns nil for a nil body" do
87
- @parser.stub(:body => nil)
87
+ @parser.stub(body: nil)
88
88
  @parser.parse.should be_nil
89
89
  end
90
90
 
91
91
  it "returns nil for a 'null' body" do
92
- @parser.stub(:body => "null")
92
+ @parser.stub(body: "null")
93
93
  @parser.parse.should be_nil
94
94
  end
95
95
 
96
96
  it "returns nil for a body with spaces only" do
97
- @parser.stub(:body => " ")
97
+ @parser.stub(body: " ")
98
98
  @parser.parse.should be_nil
99
99
  end
100
100
  end