httparty 0.13.3 → 0.13.4

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +92 -0
  4. data/.rubocop_todo.yml +124 -0
  5. data/.simplecov +1 -0
  6. data/Gemfile +8 -3
  7. data/Guardfile +1 -1
  8. data/README.md +1 -0
  9. data/Rakefile +4 -5
  10. data/bin/httparty +9 -10
  11. data/examples/README.md +3 -0
  12. data/examples/aaws.rb +2 -2
  13. data/examples/crack.rb +1 -1
  14. data/examples/custom_parsers.rb +1 -4
  15. data/examples/delicious.rb +3 -3
  16. data/examples/google.rb +2 -2
  17. data/examples/logging.rb +5 -7
  18. data/examples/nokogiri_html_parser.rb +0 -3
  19. data/examples/rescue_json.rb +17 -0
  20. data/examples/rubyurl.rb +3 -3
  21. data/examples/twitter.rb +2 -2
  22. data/examples/whoismyrep.rb +1 -1
  23. data/features/command_line.feature +85 -2
  24. data/features/steps/env.rb +16 -11
  25. data/features/steps/httparty_response_steps.rb +13 -13
  26. data/features/steps/mongrel_helper.rb +2 -2
  27. data/features/steps/remote_service_steps.rb +18 -6
  28. data/httparty.gemspec +4 -4
  29. data/lib/httparty.rb +37 -56
  30. data/lib/httparty/connection_adapter.rb +3 -4
  31. data/lib/httparty/cookie_hash.rb +2 -3
  32. data/lib/httparty/hash_conversions.rb +3 -5
  33. data/lib/httparty/logger/apache_logger.rb +1 -1
  34. data/lib/httparty/logger/logger.rb +1 -1
  35. data/lib/httparty/module_inheritable_attributes.rb +1 -1
  36. data/lib/httparty/net_digest_auth.rb +46 -16
  37. data/lib/httparty/request.rb +16 -16
  38. data/lib/httparty/response.rb +9 -4
  39. data/lib/httparty/version.rb +1 -1
  40. data/spec/httparty/connection_adapter_spec.rb +184 -100
  41. data/spec/httparty/cookie_hash_spec.rb +21 -21
  42. data/spec/httparty/exception_spec.rb +22 -7
  43. data/spec/httparty/hash_conversions_spec.rb +41 -0
  44. data/spec/httparty/logger/apache_logger_spec.rb +3 -3
  45. data/spec/httparty/logger/curl_logger_spec.rb +2 -2
  46. data/spec/httparty/logger/logger_spec.rb +7 -7
  47. data/spec/httparty/net_digest_auth_spec.rb +60 -32
  48. data/spec/httparty/parser_spec.rb +37 -35
  49. data/spec/httparty/request_spec.rb +249 -193
  50. data/spec/httparty/response_spec.rb +37 -29
  51. data/spec/httparty/ssl_spec.rb +21 -21
  52. data/spec/httparty_spec.rb +153 -164
  53. data/spec/spec_helper.rb +34 -12
  54. data/spec/support/ssl_test_helper.rb +2 -2
  55. data/spec/support/ssl_test_server.rb +21 -21
  56. data/spec/support/stub_response.rb +10 -10
  57. metadata +9 -4
  58. data/lib/httparty/core_extensions.rb +0 -32
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
2
 
3
- describe HTTParty::CookieHash do
3
+ RSpec.describe HTTParty::CookieHash do
4
4
  before(:each) do
5
5
  @cookie_hash = HTTParty::CookieHash.new
6
6
  end
@@ -10,45 +10,45 @@ describe HTTParty::CookieHash do
10
10
  it "should add new key/value pairs to the hash" do
11
11
  @cookie_hash.add_cookies(foo: "bar")
12
12
  @cookie_hash.add_cookies(rofl: "copter")
13
- @cookie_hash.length.should eql(2)
13
+ expect(@cookie_hash.length).to eql(2)
14
14
  end
15
15
 
16
16
  it "should overwrite any existing key" do
17
17
  @cookie_hash.add_cookies(foo: "bar")
18
18
  @cookie_hash.add_cookies(foo: "copter")
19
- @cookie_hash.length.should eql(1)
20
- @cookie_hash[:foo].should eql("copter")
19
+ expect(@cookie_hash.length).to eql(1)
20
+ expect(@cookie_hash[:foo]).to eql("copter")
21
21
  end
22
22
  end
23
23
 
24
24
  describe "with a string" do
25
25
  it "should add new key/value pairs to the hash" do
26
26
  @cookie_hash.add_cookies("first=one; second=two; third")
27
- @cookie_hash[:first].should == 'one'
28
- @cookie_hash[:second].should == 'two'
29
- @cookie_hash[:third].should == nil
27
+ expect(@cookie_hash[:first]).to eq('one')
28
+ expect(@cookie_hash[:second]).to eq('two')
29
+ expect(@cookie_hash[:third]).to eq(nil)
30
30
  end
31
31
 
32
32
  it "should overwrite any existing key" do
33
33
  @cookie_hash[:foo] = 'bar'
34
34
  @cookie_hash.add_cookies("foo=tar")
35
- @cookie_hash.length.should eql(1)
36
- @cookie_hash[:foo].should eql("tar")
35
+ expect(@cookie_hash.length).to eql(1)
36
+ expect(@cookie_hash[:foo]).to eql("tar")
37
37
  end
38
38
 
39
39
  it "should handle '=' within cookie value" do
40
- @cookie_hash.add_cookies("first=one=1; second=two=2==")
41
- @cookie_hash.keys.should include(:first, :second)
42
- @cookie_hash[:first].should == 'one=1'
43
- @cookie_hash[:second].should == 'two=2=='
40
+ @cookie_hash.add_cookies("first=one=1; second=two=2==")
41
+ expect(@cookie_hash.keys).to include(:first, :second)
42
+ expect(@cookie_hash[:first]).to eq('one=1')
43
+ expect(@cookie_hash[:second]).to eq('two=2==')
44
44
  end
45
45
  end
46
46
 
47
47
  describe 'with other class' do
48
48
  it "should error" do
49
- lambda {
50
- @cookie_hash.add_cookies(Array.new)
51
- }.should raise_error
49
+ expect {
50
+ @cookie_hash.add_cookies([])
51
+ }.to raise_error
52
52
  end
53
53
  end
54
54
  end
@@ -63,21 +63,21 @@ describe HTTParty::CookieHash do
63
63
  end
64
64
 
65
65
  it "should format the key/value pairs, delimited by semi-colons" do
66
- @s.should match(/foo=bar/)
67
- @s.should match(/rofl=copter/)
68
- @s.should match(/^\w+=\w+; \w+=\w+$/)
66
+ expect(@s).to match(/foo=bar/)
67
+ expect(@s).to match(/rofl=copter/)
68
+ expect(@s).to match(/^\w+=\w+; \w+=\w+$/)
69
69
  end
70
70
 
71
71
  it "should not include client side only cookies" do
72
72
  @cookie_hash.add_cookies(path: "/")
73
73
  @s = @cookie_hash.to_cookie_string
74
- @s.should_not match(/path=\//)
74
+ expect(@s).not_to 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
78
  @cookie_hash.add_cookies(Path: "/")
79
79
  @s = @cookie_hash.to_cookie_string
80
- @s.should_not match(/Path=\//)
80
+ expect(@s).not_to match(/Path=\//)
81
81
  end
82
82
  end
83
83
  end
@@ -1,23 +1,38 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
- describe HTTParty::Error do
3
+ RSpec.describe HTTParty::Error do
4
4
  subject { described_class }
5
5
 
6
- its(:ancestors) { should include(StandardError) }
6
+ describe '#ancestors' do
7
+ subject { super().ancestors }
8
+ it { is_expected.to include(StandardError) }
9
+ end
7
10
 
8
11
  describe HTTParty::UnsupportedFormat do
9
- its(:ancestors) { should include(HTTParty::Error) }
12
+ describe '#ancestors' do
13
+ subject { super().ancestors }
14
+ it { is_expected.to include(HTTParty::Error) }
15
+ end
10
16
  end
11
-
17
+
12
18
  describe HTTParty::UnsupportedURIScheme do
13
- its(:ancestors) { should include(HTTParty::Error) }
19
+ describe '#ancestors' do
20
+ subject { super().ancestors }
21
+ it { is_expected.to include(HTTParty::Error) }
22
+ end
14
23
  end
15
24
 
16
25
  describe HTTParty::ResponseError do
17
- its(:ancestors) { should include(HTTParty::Error) }
26
+ describe '#ancestors' do
27
+ subject { super().ancestors }
28
+ it { is_expected.to include(HTTParty::Error) }
29
+ end
18
30
  end
19
31
 
20
32
  describe HTTParty::RedirectionTooDeep do
21
- its(:ancestors) { should include(HTTParty::ResponseError) }
33
+ describe '#ancestors' do
34
+ subject { super().ancestors }
35
+ it { is_expected.to include(HTTParty::ResponseError) }
36
+ end
22
37
  end
23
38
  end
@@ -0,0 +1,41 @@
1
+ RSpec.describe HTTParty::HashConversions do
2
+ describe ".to_params" do
3
+ it "creates a params string from a hash" do
4
+ hash = {
5
+ name: "bob",
6
+ address: {
7
+ street: '111 ruby ave.',
8
+ city: 'ruby central',
9
+ phones: ['111-111-1111', '222-222-2222']
10
+ }
11
+ }
12
+ expect(HTTParty::HashConversions.to_params(hash)).to eq("name=bob&address[street]=111%20ruby%20ave.&address[city]=ruby%20central&address[phones][]=111-111-1111&address[phones][]=222-222-2222")
13
+ end
14
+ end
15
+
16
+ describe ".normalize_param" do
17
+ context "value is an array" do
18
+ it "creates a params string" do
19
+ expect(
20
+ HTTParty::HashConversions.normalize_param(:people, ["Bob Jones", "Mike Smith"])
21
+ ).to eq("people[]=Bob%20Jones&people[]=Mike%20Smith&")
22
+ end
23
+ end
24
+
25
+ context "value is hash" do
26
+ it "creates a params string" do
27
+ expect(
28
+ HTTParty::HashConversions.normalize_param(:person, { name: "Bob Jones" })
29
+ ).to eq("person[name]=Bob%20Jones&")
30
+ end
31
+ end
32
+
33
+ context "value is a string" do
34
+ it "creates a params string" do
35
+ expect(
36
+ HTTParty::HashConversions.normalize_param(:name, "Bob Jones")
37
+ ).to eq("name=Bob%20Jones&")
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
2
 
3
- describe HTTParty::Logger::ApacheLogger do
3
+ RSpec.describe HTTParty::Logger::ApacheLogger do
4
4
  let(:subject) { described_class.new(logger_double, :info) }
5
5
  let(:logger_double) { double('Logger') }
6
6
  let(:request_double) { double('Request', http_method: Net::HTTP::Get, path: "http://my.domain.com/my_path") }
@@ -8,7 +8,7 @@ describe HTTParty::Logger::ApacheLogger do
8
8
 
9
9
  before do
10
10
  subject.current_time = request_time
11
- logger_double.should_receive(:info).with(log_message)
11
+ expect(logger_double).to receive(:info).with(log_message)
12
12
  end
13
13
 
14
14
  describe "#format" do
@@ -32,7 +32,7 @@ describe HTTParty::Logger::ApacheLogger do
32
32
  code: 200,
33
33
  headers: { 'Content-Length' => 512 }
34
34
  )
35
- response_double.stub(:[]).with('Content-Length').and_raise(TypeError.new('no implicit conversion of String into Integer'))
35
+ allow(response_double).to receive(:[]).with('Content-Length').and_raise(TypeError.new('no implicit conversion of String into Integer'))
36
36
 
37
37
  subject.format(request_double, response_double)
38
38
  end
@@ -1,10 +1,10 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
2
 
3
- describe HTTParty::Logger::CurlLogger do
3
+ RSpec.describe HTTParty::Logger::CurlLogger do
4
4
  describe "#format" do
5
5
  it "formats a response in a style that resembles a -v curl" do
6
6
  logger_double = double
7
- logger_double.should_receive(:info).with(
7
+ expect(logger_double).to receive(:info).with(
8
8
  /\[HTTParty\] \[\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\ [+-]\d{4}\] > GET http:\/\/localhost/)
9
9
 
10
10
  subject = described_class.new(logger_double, :info)
@@ -1,22 +1,22 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
2
 
3
- describe HTTParty::Logger do
3
+ RSpec.describe HTTParty::Logger do
4
4
  describe ".build" do
5
5
  subject { HTTParty::Logger }
6
6
 
7
7
  it "defaults level to :info" do
8
- logger_double = double()
9
- subject.build(logger_double, nil, nil).level.should == :info
8
+ logger_double = double
9
+ expect(subject.build(logger_double, nil, nil).level).to eq(:info)
10
10
  end
11
11
 
12
12
  it "defaults format to :apache" do
13
- logger_double = double()
14
- subject.build(logger_double, nil, nil).should be_an_instance_of(HTTParty::Logger::ApacheLogger)
13
+ logger_double = double
14
+ expect(subject.build(logger_double, nil, nil)).to be_an_instance_of(HTTParty::Logger::ApacheLogger)
15
15
  end
16
16
 
17
17
  it "builds :curl style logger" do
18
- logger_double = double()
19
- subject.build(logger_double, nil, :curl).should be_an_instance_of(HTTParty::Logger::CurlLogger)
18
+ logger_double = double
19
+ expect(subject.build(logger_double, nil, :curl)).to be_an_instance_of(HTTParty::Logger::CurlLogger)
20
20
  end
21
21
  end
22
22
  end
@@ -1,11 +1,11 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
- describe Net::HTTPHeader::DigestAuthenticator do
3
+ RSpec.describe Net::HTTPHeader::DigestAuthenticator do
4
4
  def setup_digest(response)
5
5
  digest = Net::HTTPHeader::DigestAuthenticator.new("Mufasa",
6
- "Circle Of Life", "GET", "/dir/index.html", response)
7
- digest.stub(:random).and_return("deadbeef")
8
- Digest::MD5.stub(:hexdigest) { |str| "md5(#{str})" }
6
+ "Circle Of Life", "GET", "/dir/index.html", response)
7
+ allow(digest).to receive(:random).and_return("deadbeef")
8
+ allow(Digest::MD5).to receive(:hexdigest) { |str| "md5(#{str})" }
9
9
  digest
10
10
  end
11
11
 
@@ -13,6 +13,34 @@ describe Net::HTTPHeader::DigestAuthenticator do
13
13
  @digest.authorization_header.join(", ")
14
14
  end
15
15
 
16
+ def cookie_header
17
+ @digest.cookie_header
18
+ end
19
+
20
+ context "with a cookie value in the response header" do
21
+ before do
22
+ @digest = setup_digest({
23
+ 'www-authenticate' => 'Digest realm="myhost@testrealm.com"',
24
+ 'Set-Cookie' => 'custom-cookie=1234567'
25
+ })
26
+ end
27
+
28
+ it "should set cookie header" do
29
+ expect(cookie_header).to include('custom-cookie=1234567')
30
+ end
31
+ end
32
+
33
+ context "without a cookie value in the response header" do
34
+ before do
35
+ @digest = setup_digest({
36
+ 'www-authenticate' => 'Digest realm="myhost@testrealm.com"'
37
+ })
38
+ end
39
+
40
+ it "should set empty cookie header array" do
41
+ expect(cookie_header).to eql []
42
+ end
43
+ end
16
44
 
17
45
  context "with an opaque value in the response header" do
18
46
  before do
@@ -22,7 +50,7 @@ describe Net::HTTPHeader::DigestAuthenticator do
22
50
  end
23
51
 
24
52
  it "should set opaque" do
25
- authorization_header.should include(%Q(opaque="solid"))
53
+ expect(authorization_header).to include('opaque="solid"')
26
54
  end
27
55
  end
28
56
 
@@ -34,130 +62,130 @@ describe Net::HTTPHeader::DigestAuthenticator do
34
62
  end
35
63
 
36
64
  it "should not set opaque" do
37
- authorization_header.should_not include(%Q(opaque=))
65
+ expect(authorization_header).not_to include("opaque=")
38
66
  end
39
67
  end
40
68
 
41
69
  context "with specified quality of protection (qop)" do
42
70
  before do
43
71
  @digest = setup_digest({
44
- 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"',
72
+ 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"'
45
73
  })
46
74
  end
47
75
 
48
76
  it "should set prefix" do
49
- authorization_header.should =~ /^Digest /
77
+ expect(authorization_header).to match(/^Digest /)
50
78
  end
51
79
 
52
80
  it "should set username" do
53
- authorization_header.should include(%Q(username="Mufasa"))
81
+ expect(authorization_header).to include('username="Mufasa"')
54
82
  end
55
83
 
56
84
  it "should set digest-uri" do
57
- authorization_header.should include(%Q(uri="/dir/index.html"))
85
+ expect(authorization_header).to include('uri="/dir/index.html"')
58
86
  end
59
87
 
60
88
  it "should set qop" do
61
- authorization_header.should include(%Q(qop="auth"))
89
+ expect(authorization_header).to include('qop="auth"')
62
90
  end
63
91
 
64
92
  it "should set cnonce" do
65
- authorization_header.should include(%Q(cnonce="md5(deadbeef)"))
93
+ expect(authorization_header).to include('cnonce="md5(deadbeef)"')
66
94
  end
67
95
 
68
96
  it "should set nonce-count" do
69
- authorization_header.should include(%Q(nc=00000001))
97
+ expect(authorization_header).to include("nc=00000001")
70
98
  end
71
99
 
72
100
  it "should set response" do
73
101
  request_digest = "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life):NONCE:00000001:md5(deadbeef):auth:md5(GET:/dir/index.html))"
74
- authorization_header.should include(%Q(response="#{request_digest}"))
102
+ expect(authorization_header).to include(%(response="#{request_digest}"))
75
103
  end
76
104
  end
77
105
 
78
106
  context "when quality of protection (qop) is unquoted" do
79
107
  before do
80
108
  @digest = setup_digest({
81
- 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop=auth',
109
+ 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop=auth'
82
110
  })
83
111
  end
84
112
 
85
113
  it "should still set qop" do
86
- authorization_header.should include(%Q(qop="auth"))
114
+ expect(authorization_header).to include('qop="auth"')
87
115
  end
88
116
  end
89
117
 
90
118
  context "with unspecified quality of protection (qop)" do
91
119
  before do
92
120
  @digest = setup_digest({
93
- 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE"',
121
+ 'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE"'
94
122
  })
95
123
  end
96
124
 
97
125
  it "should set prefix" do
98
- authorization_header.should =~ /^Digest /
126
+ expect(authorization_header).to match(/^Digest /)
99
127
  end
100
128
 
101
129
  it "should set username" do
102
- authorization_header.should include(%Q(username="Mufasa"))
130
+ expect(authorization_header).to include('username="Mufasa"')
103
131
  end
104
132
 
105
133
  it "should set digest-uri" do
106
- authorization_header.should include(%Q(uri="/dir/index.html"))
134
+ expect(authorization_header).to include('uri="/dir/index.html"')
107
135
  end
108
136
 
109
137
  it "should not set qop" do
110
- authorization_header.should_not include(%Q(qop=))
138
+ expect(authorization_header).not_to include("qop=")
111
139
  end
112
140
 
113
141
  it "should not set cnonce" do
114
- authorization_header.should_not include(%Q(cnonce=))
142
+ expect(authorization_header).not_to include("cnonce=")
115
143
  end
116
144
 
117
145
  it "should not set nonce-count" do
118
- authorization_header.should_not include(%Q(nc=))
146
+ expect(authorization_header).not_to include("nc=")
119
147
  end
120
148
 
121
149
  it "should set response" do
122
150
  request_digest = "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life):NONCE:md5(GET:/dir/index.html))"
123
- authorization_header.should include(%Q(response="#{request_digest}"))
151
+ expect(authorization_header).to include(%(response="#{request_digest}"))
124
152
  end
125
153
  end
126
154
 
127
155
  context "with multiple authenticate headers" do
128
156
  before do
129
157
  @digest = setup_digest({
130
- 'www-authenticate' => 'NTLM, Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"',
158
+ 'www-authenticate' => 'NTLM, Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"'
131
159
  })
132
160
  end
133
161
 
134
162
  it "should set prefix" do
135
- authorization_header.should =~ /^Digest /
163
+ expect(authorization_header).to match(/^Digest /)
136
164
  end
137
165
 
138
166
  it "should set username" do
139
- authorization_header.should include(%Q(username="Mufasa"))
167
+ expect(authorization_header).to include('username="Mufasa"')
140
168
  end
141
169
 
142
170
  it "should set digest-uri" do
143
- authorization_header.should include(%Q(uri="/dir/index.html"))
171
+ expect(authorization_header).to include('uri="/dir/index.html"')
144
172
  end
145
173
 
146
174
  it "should set qop" do
147
- authorization_header.should include(%Q(qop="auth"))
175
+ expect(authorization_header).to include('qop="auth"')
148
176
  end
149
177
 
150
178
  it "should set cnonce" do
151
- authorization_header.should include(%Q(cnonce="md5(deadbeef)"))
179
+ expect(authorization_header).to include('cnonce="md5(deadbeef)"')
152
180
  end
153
181
 
154
182
  it "should set nonce-count" do
155
- authorization_header.should include(%Q(nc=00000001))
183
+ expect(authorization_header).to include("nc=00000001")
156
184
  end
157
185
 
158
186
  it "should set response" do
159
187
  request_digest = "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life):NONCE:00000001:md5(deadbeef):auth:md5(GET:/dir/index.html))"
160
- authorization_header.should include(%Q(response="#{request_digest}"))
188
+ expect(authorization_header).to include(%(response="#{request_digest}"))
161
189
  end
162
190
  end
163
191
  end