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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +92 -0
- data/.rubocop_todo.yml +124 -0
- data/.simplecov +1 -0
- data/Gemfile +8 -3
- data/Guardfile +1 -1
- data/README.md +1 -0
- data/Rakefile +4 -5
- data/bin/httparty +9 -10
- data/examples/README.md +3 -0
- data/examples/aaws.rb +2 -2
- data/examples/crack.rb +1 -1
- data/examples/custom_parsers.rb +1 -4
- data/examples/delicious.rb +3 -3
- data/examples/google.rb +2 -2
- data/examples/logging.rb +5 -7
- data/examples/nokogiri_html_parser.rb +0 -3
- data/examples/rescue_json.rb +17 -0
- data/examples/rubyurl.rb +3 -3
- data/examples/twitter.rb +2 -2
- data/examples/whoismyrep.rb +1 -1
- data/features/command_line.feature +85 -2
- data/features/steps/env.rb +16 -11
- data/features/steps/httparty_response_steps.rb +13 -13
- data/features/steps/mongrel_helper.rb +2 -2
- data/features/steps/remote_service_steps.rb +18 -6
- data/httparty.gemspec +4 -4
- data/lib/httparty.rb +37 -56
- data/lib/httparty/connection_adapter.rb +3 -4
- data/lib/httparty/cookie_hash.rb +2 -3
- data/lib/httparty/hash_conversions.rb +3 -5
- data/lib/httparty/logger/apache_logger.rb +1 -1
- data/lib/httparty/logger/logger.rb +1 -1
- data/lib/httparty/module_inheritable_attributes.rb +1 -1
- data/lib/httparty/net_digest_auth.rb +46 -16
- data/lib/httparty/request.rb +16 -16
- data/lib/httparty/response.rb +9 -4
- data/lib/httparty/version.rb +1 -1
- data/spec/httparty/connection_adapter_spec.rb +184 -100
- data/spec/httparty/cookie_hash_spec.rb +21 -21
- data/spec/httparty/exception_spec.rb +22 -7
- data/spec/httparty/hash_conversions_spec.rb +41 -0
- data/spec/httparty/logger/apache_logger_spec.rb +3 -3
- data/spec/httparty/logger/curl_logger_spec.rb +2 -2
- data/spec/httparty/logger/logger_spec.rb +7 -7
- data/spec/httparty/net_digest_auth_spec.rb +60 -32
- data/spec/httparty/parser_spec.rb +37 -35
- data/spec/httparty/request_spec.rb +249 -193
- data/spec/httparty/response_spec.rb +37 -29
- data/spec/httparty/ssl_spec.rb +21 -21
- data/spec/httparty_spec.rb +153 -164
- data/spec/spec_helper.rb +34 -12
- data/spec/support/ssl_test_helper.rb +2 -2
- data/spec/support/ssl_test_server.rb +21 -21
- data/spec/support/stub_response.rb +10 -10
- metadata +9 -4
- data/lib/httparty/core_extensions.rb +0 -32
@@ -1,12 +1,12 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe HTTParty::Response do
|
3
|
+
RSpec.describe HTTParty::Response do
|
4
4
|
before do
|
5
5
|
@last_modified = Date.new(2010, 1, 15).to_s
|
6
6
|
@content_length = '1024'
|
7
7
|
@request_object = HTTParty::Request.new Net::HTTP::Get, '/'
|
8
8
|
@response_object = Net::HTTPOK.new('1.1', 200, 'OK')
|
9
|
-
@response_object.
|
9
|
+
allow(@response_object).to receive_messages(body: "{foo:'bar'}")
|
10
10
|
@response_object['last-modified'] = @last_modified
|
11
11
|
@response_object['content-length'] = @content_length
|
12
12
|
@parsed_response = lambda { {"foo" => "bar"} }
|
@@ -15,79 +15,79 @@ describe HTTParty::Response do
|
|
15
15
|
|
16
16
|
describe ".underscore" do
|
17
17
|
it "works with one capitalized word" do
|
18
|
-
HTTParty::Response.underscore("Accepted").
|
18
|
+
expect(HTTParty::Response.underscore("Accepted")).to eq("accepted")
|
19
19
|
end
|
20
20
|
|
21
21
|
it "works with titlecase" do
|
22
|
-
HTTParty::Response.underscore("BadGateway").
|
22
|
+
expect(HTTParty::Response.underscore("BadGateway")).to eq("bad_gateway")
|
23
23
|
end
|
24
24
|
|
25
25
|
it "works with all caps" do
|
26
|
-
HTTParty::Response.underscore("OK").
|
26
|
+
expect(HTTParty::Response.underscore("OK")).to eq("ok")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "initialization" do
|
31
31
|
it "should set the Net::HTTP Response" do
|
32
|
-
@response.response.
|
32
|
+
expect(@response.response).to eq(@response_object)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should set body" do
|
36
|
-
@response.body.
|
36
|
+
expect(@response.body).to eq(@response_object.body)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should set code" do
|
40
|
-
@response.code.
|
40
|
+
expect(@response.code).to eq(@response_object.code)
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should set code as a Fixnum" do
|
44
|
-
@response.code.
|
44
|
+
expect(@response.code).to be_an_instance_of(Fixnum)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
it "returns response headers" do
|
49
49
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
50
|
-
response.headers.
|
50
|
+
expect(response.headers).to eq({'last-modified' => [@last_modified], 'content-length' => [@content_length]})
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should send missing methods to delegate" do
|
54
54
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
55
|
-
response['foo'].
|
55
|
+
expect(response['foo']).to eq('bar')
|
56
56
|
end
|
57
57
|
|
58
58
|
it "response to request" do
|
59
59
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
60
|
-
response.respond_to?(:request).
|
60
|
+
expect(response.respond_to?(:request)).to be_truthy
|
61
61
|
end
|
62
62
|
|
63
63
|
it "responds to response" do
|
64
64
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
65
|
-
response.respond_to?(:response).
|
65
|
+
expect(response.respond_to?(:response)).to be_truthy
|
66
66
|
end
|
67
67
|
|
68
68
|
it "responds to body" do
|
69
69
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
70
|
-
response.respond_to?(:body).
|
70
|
+
expect(response.respond_to?(:body)).to be_truthy
|
71
71
|
end
|
72
72
|
|
73
73
|
it "responds to headers" do
|
74
74
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
75
|
-
response.respond_to?(:headers).
|
75
|
+
expect(response.respond_to?(:headers)).to be_truthy
|
76
76
|
end
|
77
77
|
|
78
78
|
it "responds to parsed_response" do
|
79
79
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
80
|
-
response.respond_to?(:parsed_response).
|
80
|
+
expect(response.respond_to?(:parsed_response)).to be_truthy
|
81
81
|
end
|
82
82
|
|
83
83
|
it "responds to anything parsed_response responds to" do
|
84
84
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
85
|
-
response.respond_to?(:[]).
|
85
|
+
expect(response.respond_to?(:[])).to be_truthy
|
86
86
|
end
|
87
87
|
|
88
88
|
it "should be able to iterate if it is array" do
|
89
89
|
response = HTTParty::Response.new(@request_object, @response_object, lambda { [{'foo' => 'bar'}, {'foo' => 'baz'}] })
|
90
|
-
response.size.
|
90
|
+
expect(response.size).to eq(2)
|
91
91
|
expect {
|
92
92
|
response.each { |item| }
|
93
93
|
}.to_not raise_error
|
@@ -95,14 +95,14 @@ describe HTTParty::Response do
|
|
95
95
|
|
96
96
|
it "allows headers to be accessed by mixed-case names in hash notation" do
|
97
97
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
98
|
-
response.headers['Content-LENGTH'].
|
98
|
+
expect(response.headers['Content-LENGTH']).to eq(@content_length)
|
99
99
|
end
|
100
100
|
|
101
101
|
it "returns a comma-delimited value when multiple values exist" do
|
102
102
|
@response_object.add_field 'set-cookie', 'csrf_id=12345; path=/'
|
103
103
|
@response_object.add_field 'set-cookie', '_github_ses=A123CdE; path=/'
|
104
104
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
105
|
-
response.headers['set-cookie'].
|
105
|
+
expect(response.headers['set-cookie']).to eq("csrf_id=12345; path=/, _github_ses=A123CdE; path=/")
|
106
106
|
end
|
107
107
|
|
108
108
|
# Backwards-compatibility - previously, #headers returned a Hash
|
@@ -110,14 +110,14 @@ describe HTTParty::Response do
|
|
110
110
|
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
|
111
111
|
hash_methods = {}.methods - response.headers.methods
|
112
112
|
hash_methods.each do |method_name|
|
113
|
-
response.headers.respond_to?(method_name).
|
113
|
+
expect(response.headers.respond_to?(method_name)).to be_truthy
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
117
|
describe "semantic methods for response codes" do
|
118
118
|
def response_mock(klass)
|
119
119
|
response = klass.new('', '', '')
|
120
|
-
response.
|
120
|
+
allow(response).to receive(:body)
|
121
121
|
response
|
122
122
|
end
|
123
123
|
|
@@ -125,31 +125,31 @@ describe HTTParty::Response do
|
|
125
125
|
it "is information" do
|
126
126
|
net_response = response_mock(Net::HTTPInformation)
|
127
127
|
response = HTTParty::Response.new(@request_object, net_response, '')
|
128
|
-
response.information
|
128
|
+
expect(response.information?).to be_truthy
|
129
129
|
end
|
130
130
|
|
131
131
|
it "is success" do
|
132
132
|
net_response = response_mock(Net::HTTPSuccess)
|
133
133
|
response = HTTParty::Response.new(@request_object, net_response, '')
|
134
|
-
response.success
|
134
|
+
expect(response.success?).to be_truthy
|
135
135
|
end
|
136
136
|
|
137
137
|
it "is redirection" do
|
138
138
|
net_response = response_mock(Net::HTTPRedirection)
|
139
139
|
response = HTTParty::Response.new(@request_object, net_response, '')
|
140
|
-
response.redirection
|
140
|
+
expect(response.redirection?).to be_truthy
|
141
141
|
end
|
142
142
|
|
143
143
|
it "is client error" do
|
144
144
|
net_response = response_mock(Net::HTTPClientError)
|
145
145
|
response = HTTParty::Response.new(@request_object, net_response, '')
|
146
|
-
response.client_error
|
146
|
+
expect(response.client_error?).to be_truthy
|
147
147
|
end
|
148
148
|
|
149
149
|
it "is server error" do
|
150
150
|
net_response = response_mock(Net::HTTPServerError)
|
151
151
|
response = HTTParty::Response.new(@request_object, net_response, '')
|
152
|
-
response.server_error
|
152
|
+
expect(response.server_error?).to be_truthy
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
@@ -206,7 +206,7 @@ describe HTTParty::Response do
|
|
206
206
|
it "responds to #{method}" do
|
207
207
|
net_response = response_mock(klass)
|
208
208
|
response = HTTParty::Response.new(@request_object, net_response, '')
|
209
|
-
response.__send__(method).
|
209
|
+
expect(response.__send__(method)).to be_truthy
|
210
210
|
end
|
211
211
|
end
|
212
212
|
end
|
@@ -215,7 +215,15 @@ describe HTTParty::Response do
|
|
215
215
|
describe "headers" do
|
216
216
|
it "can initialize without headers" do
|
217
217
|
headers = HTTParty::Response::Headers.new
|
218
|
-
headers.
|
218
|
+
expect(headers).to eq({})
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "#tap" do
|
223
|
+
it "is possible to tap into a response" do
|
224
|
+
result = @response.tap(&:code)
|
225
|
+
|
226
|
+
expect(result).to eq @response
|
219
227
|
end
|
220
228
|
end
|
221
229
|
end
|
data/spec/httparty/ssl_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
2
|
|
3
|
-
describe HTTParty::Request do
|
3
|
+
RSpec.describe HTTParty::Request do
|
4
4
|
context "SSL certificate verification" do
|
5
5
|
before do
|
6
6
|
FakeWeb.allow_net_connect = true
|
@@ -11,64 +11,64 @@ describe HTTParty::Request do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should fail when no trusted CA list is specified, by default" do
|
14
|
-
|
14
|
+
expect do
|
15
15
|
ssl_verify_test(nil, nil, "selfsigned.crt")
|
16
|
-
end.
|
16
|
+
end.to raise_error OpenSSL::SSL::SSLError
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should work when no trusted CA list is specified, when the verify option is set to false" do
|
20
|
-
ssl_verify_test(nil, nil, "selfsigned.crt", verify: false).
|
20
|
+
expect(ssl_verify_test(nil, nil, "selfsigned.crt", verify: false).parsed_response).to eq({'success' => true})
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should fail when no trusted CA list is specified, with a bogus hostname, by default" do
|
24
|
-
|
24
|
+
expect do
|
25
25
|
ssl_verify_test(nil, nil, "bogushost.crt")
|
26
|
-
end.
|
26
|
+
end.to raise_error OpenSSL::SSL::SSLError
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should work when no trusted CA list is specified, even with a bogus hostname, when the verify option is set to true" do
|
30
|
-
ssl_verify_test(nil, nil, "bogushost.crt", verify: false).
|
30
|
+
expect(ssl_verify_test(nil, nil, "bogushost.crt", verify: false).parsed_response).to eq({'success' => true})
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should work when using ssl_ca_file with a self-signed CA" do
|
34
|
-
ssl_verify_test(:ssl_ca_file, "selfsigned.crt", "selfsigned.crt").
|
34
|
+
expect(ssl_verify_test(:ssl_ca_file, "selfsigned.crt", "selfsigned.crt").parsed_response).to eq({'success' => true})
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should work when using ssl_ca_file with a certificate authority" do
|
38
|
-
ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt").
|
38
|
+
expect(ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt").parsed_response).to eq({'success' => true})
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should work when using ssl_ca_path with a certificate authority" do
|
42
42
|
http = Net::HTTP.new('www.google.com', 443)
|
43
|
-
response =
|
44
|
-
http.
|
45
|
-
Net::HTTP.
|
46
|
-
http.
|
43
|
+
response = double(Net::HTTPResponse, :[] => '', body: '', to_hash: {})
|
44
|
+
allow(http).to receive(:request).and_return(response)
|
45
|
+
expect(Net::HTTP).to receive(:new).with('www.google.com', 443).and_return(http)
|
46
|
+
expect(http).to receive(:ca_path=).with('/foo/bar')
|
47
47
|
HTTParty.get('https://www.google.com', ssl_ca_path: '/foo/bar')
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should fail when using ssl_ca_file and the server uses an unrecognized certificate authority" do
|
51
|
-
|
51
|
+
expect do
|
52
52
|
ssl_verify_test(:ssl_ca_file, "ca.crt", "selfsigned.crt")
|
53
|
-
end.
|
53
|
+
end.to raise_error(OpenSSL::SSL::SSLError)
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should fail when using ssl_ca_path and the server uses an unrecognized certificate authority" do
|
57
|
-
|
57
|
+
expect do
|
58
58
|
ssl_verify_test(:ssl_ca_path, ".", "selfsigned.crt")
|
59
|
-
end.
|
59
|
+
end.to raise_error(OpenSSL::SSL::SSLError)
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should fail when using ssl_ca_file and the server uses a bogus hostname" do
|
63
|
-
|
63
|
+
expect do
|
64
64
|
ssl_verify_test(:ssl_ca_file, "ca.crt", "bogushost.crt")
|
65
|
-
end.
|
65
|
+
end.to raise_error(OpenSSL::SSL::SSLError)
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should fail when using ssl_ca_path and the server uses a bogus hostname" do
|
69
|
-
|
69
|
+
expect do
|
70
70
|
ssl_verify_test(:ssl_ca_path, ".", "bogushost.crt")
|
71
|
-
end.
|
71
|
+
end.to raise_error(OpenSSL::SSL::SSLError)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
data/spec/httparty_spec.rb
CHANGED
@@ -1,67 +1,52 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
2
|
|
3
|
-
describe HTTParty do
|
3
|
+
RSpec.describe HTTParty do
|
4
4
|
before(:each) do
|
5
5
|
@klass = Class.new
|
6
6
|
@klass.instance_eval { include HTTParty }
|
7
7
|
end
|
8
8
|
|
9
|
-
describe "AllowedFormats deprecated" do
|
10
|
-
before do
|
11
|
-
Kernel.stub(:warn)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "warns with a deprecation message" do
|
15
|
-
Kernel.should_receive(:warn).with("Deprecated: Use HTTParty::Parser::SupportedFormats")
|
16
|
-
HTTParty::AllowedFormats
|
17
|
-
end
|
18
|
-
|
19
|
-
it "returns HTTPart::Parser::SupportedFormats" do
|
20
|
-
HTTParty::AllowedFormats.should == HTTParty::Parser::SupportedFormats
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
9
|
describe "pem" do
|
25
10
|
it 'should set the pem content' do
|
26
11
|
@klass.pem 'PEM-CONTENT'
|
27
|
-
@klass.default_options[:pem].
|
12
|
+
expect(@klass.default_options[:pem]).to eq('PEM-CONTENT')
|
28
13
|
end
|
29
14
|
|
30
15
|
it "should set the password to nil if it's not provided" do
|
31
16
|
@klass.pem 'PEM-CONTENT'
|
32
|
-
@klass.default_options[:pem_password].
|
17
|
+
expect(@klass.default_options[:pem_password]).to be_nil
|
33
18
|
end
|
34
19
|
|
35
20
|
it 'should set the password' do
|
36
21
|
@klass.pem 'PEM-CONTENT', 'PASSWORD'
|
37
|
-
@klass.default_options[:pem_password].
|
22
|
+
expect(@klass.default_options[:pem_password]).to eq('PASSWORD')
|
38
23
|
end
|
39
24
|
end
|
40
25
|
|
41
26
|
describe "pkcs12" do
|
42
27
|
it 'should set the p12 content' do
|
43
28
|
@klass.pkcs12 'P12-CONTENT', 'PASSWORD'
|
44
|
-
@klass.default_options[:p12].
|
29
|
+
expect(@klass.default_options[:p12]).to eq('P12-CONTENT')
|
45
30
|
end
|
46
31
|
|
47
32
|
it 'should set the password' do
|
48
33
|
@klass.pkcs12 'P12-CONTENT', 'PASSWORD'
|
49
|
-
@klass.default_options[:p12_password].
|
34
|
+
expect(@klass.default_options[:p12_password]).to eq('PASSWORD')
|
50
35
|
end
|
51
36
|
end
|
52
37
|
|
53
38
|
describe 'ssl_version' do
|
54
39
|
it 'should set the ssl_version content' do
|
55
40
|
@klass.ssl_version :SSLv3
|
56
|
-
@klass.default_options[:ssl_version].
|
41
|
+
expect(@klass.default_options[:ssl_version]).to eq(:SSLv3)
|
57
42
|
end
|
58
43
|
end
|
59
44
|
|
60
45
|
describe 'ciphers' do
|
61
46
|
it 'should set the ciphers content' do
|
62
|
-
@klass.default_options[:ciphers].
|
47
|
+
expect(@klass.default_options[:ciphers]).to be_nil
|
63
48
|
@klass.ciphers 'RC4-SHA'
|
64
|
-
@klass.default_options[:ciphers].
|
49
|
+
expect(@klass.default_options[:ciphers]).to eq('RC4-SHA')
|
65
50
|
end
|
66
51
|
end
|
67
52
|
|
@@ -69,15 +54,15 @@ describe HTTParty do
|
|
69
54
|
it 'should set the address' do
|
70
55
|
@klass.http_proxy 'proxy.foo.com', 80
|
71
56
|
options = @klass.default_options
|
72
|
-
options[:http_proxyaddr].
|
73
|
-
options[:http_proxyport].
|
57
|
+
expect(options[:http_proxyaddr]).to eq('proxy.foo.com')
|
58
|
+
expect(options[:http_proxyport]).to eq(80)
|
74
59
|
end
|
75
60
|
|
76
61
|
it 'should set the proxy user and pass when they are provided' do
|
77
62
|
@klass.http_proxy 'proxy.foo.com', 80, 'user', 'pass'
|
78
63
|
options = @klass.default_options
|
79
|
-
options[:http_proxyuser].
|
80
|
-
options[:http_proxypass].
|
64
|
+
expect(options[:http_proxyuser]).to eq('user')
|
65
|
+
expect(options[:http_proxypass]).to eq('pass')
|
81
66
|
end
|
82
67
|
end
|
83
68
|
|
@@ -87,71 +72,71 @@ describe HTTParty do
|
|
87
72
|
end
|
88
73
|
|
89
74
|
it "should have reader" do
|
90
|
-
@klass.base_uri.
|
75
|
+
expect(@klass.base_uri).to eq('http://api.foo.com/v1')
|
91
76
|
end
|
92
77
|
|
93
78
|
it 'should have writer' do
|
94
79
|
@klass.base_uri('http://api.foobar.com')
|
95
|
-
@klass.base_uri.
|
80
|
+
expect(@klass.base_uri).to eq('http://api.foobar.com')
|
96
81
|
end
|
97
82
|
|
98
83
|
it 'should not modify the parameter during assignment' do
|
99
84
|
uri = 'http://api.foobar.com'
|
100
85
|
@klass.base_uri(uri)
|
101
|
-
uri.
|
86
|
+
expect(uri).to eq('http://api.foobar.com')
|
102
87
|
end
|
103
88
|
end
|
104
89
|
|
105
90
|
describe ".disable_rails_query_string_format" do
|
106
91
|
it "sets the query string normalizer to HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER" do
|
107
92
|
@klass.disable_rails_query_string_format
|
108
|
-
@klass.default_options[:query_string_normalizer].
|
93
|
+
expect(@klass.default_options[:query_string_normalizer]).to eq(HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER)
|
109
94
|
end
|
110
95
|
end
|
111
96
|
|
112
97
|
describe ".normalize_base_uri" do
|
113
98
|
it "should add http if not present for non ssl requests" do
|
114
99
|
uri = HTTParty.normalize_base_uri('api.foobar.com')
|
115
|
-
uri.
|
100
|
+
expect(uri).to eq('http://api.foobar.com')
|
116
101
|
end
|
117
102
|
|
118
103
|
it "should add https if not present for ssl requests" do
|
119
104
|
uri = HTTParty.normalize_base_uri('api.foo.com/v1:443')
|
120
|
-
uri.
|
105
|
+
expect(uri).to eq('https://api.foo.com/v1:443')
|
121
106
|
end
|
122
107
|
|
123
108
|
it "should not remove https for ssl requests" do
|
124
109
|
uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
|
125
|
-
uri.
|
110
|
+
expect(uri).to eq('https://api.foo.com/v1:443')
|
126
111
|
end
|
127
112
|
|
128
113
|
it 'should not modify the parameter' do
|
129
114
|
uri = 'http://api.foobar.com'
|
130
115
|
HTTParty.normalize_base_uri(uri)
|
131
|
-
uri.
|
116
|
+
expect(uri).to eq('http://api.foobar.com')
|
132
117
|
end
|
133
118
|
|
134
119
|
it "should not treat uri's with a port of 4430 as ssl" do
|
135
120
|
uri = HTTParty.normalize_base_uri('http://api.foo.com:4430/v1')
|
136
|
-
uri.
|
121
|
+
expect(uri).to eq('http://api.foo.com:4430/v1')
|
137
122
|
end
|
138
123
|
end
|
139
124
|
|
140
125
|
describe "headers" do
|
141
|
-
def expect_headers(header={})
|
142
|
-
HTTParty::Request.
|
126
|
+
def expect_headers(header = {})
|
127
|
+
expect(HTTParty::Request).to receive(:new) \
|
143
128
|
.with(anything, anything, hash_including({ headers: header })) \
|
144
|
-
.and_return(
|
129
|
+
.and_return(double("mock response", perform: nil))
|
145
130
|
end
|
146
131
|
|
147
132
|
it "should default to empty hash" do
|
148
|
-
@klass.headers.
|
133
|
+
expect(@klass.headers).to eq({})
|
149
134
|
end
|
150
135
|
|
151
136
|
it "should be able to be updated" do
|
152
137
|
init_headers = {foo: 'bar', baz: 'spax'}
|
153
138
|
@klass.headers init_headers
|
154
|
-
@klass.headers.
|
139
|
+
expect(@klass.headers).to eq(init_headers)
|
155
140
|
end
|
156
141
|
|
157
142
|
it "uses the class headers when sending a request" do
|
@@ -187,10 +172,10 @@ describe HTTParty do
|
|
187
172
|
end
|
188
173
|
|
189
174
|
it 'doesnt modify default_options' do
|
190
|
-
@klass.headers.
|
175
|
+
expect(@klass.headers).to eq({})
|
191
176
|
expect_headers('cookie' => 'type=snickerdoodle')
|
192
177
|
@klass.get('', cookies: {type: 'snickerdoodle'})
|
193
|
-
@klass.default_options[:headers].
|
178
|
+
expect(@klass.default_options[:headers]).to eq({})
|
194
179
|
end
|
195
180
|
|
196
181
|
it 'adds optional cookies to the optional headers' do
|
@@ -202,21 +187,21 @@ describe HTTParty do
|
|
202
187
|
|
203
188
|
describe "cookies" do
|
204
189
|
def expect_cookie_header(s)
|
205
|
-
HTTParty::Request.
|
190
|
+
expect(HTTParty::Request).to receive(:new) \
|
206
191
|
.with(anything, anything, hash_including({ headers: { "cookie" => s } })) \
|
207
|
-
.and_return(
|
192
|
+
.and_return(double("mock response", perform: nil))
|
208
193
|
end
|
209
194
|
|
210
195
|
it "should not be in the headers by default" do
|
211
|
-
HTTParty::Request.
|
196
|
+
allow(HTTParty::Request).to receive(:new).and_return(double(nil, perform: nil))
|
212
197
|
@klass.get("")
|
213
|
-
@klass.headers.keys.
|
198
|
+
expect(@klass.headers.keys).not_to include("cookie")
|
214
199
|
end
|
215
200
|
|
216
201
|
it "should raise an ArgumentError if passed a non-Hash" do
|
217
|
-
|
202
|
+
expect do
|
218
203
|
@klass.cookies("nonsense")
|
219
|
-
end.
|
204
|
+
end.to raise_error(ArgumentError)
|
220
205
|
end
|
221
206
|
|
222
207
|
it "should allow a cookie to be specified with a one-off request" do
|
@@ -273,79 +258,79 @@ describe HTTParty do
|
|
273
258
|
|
274
259
|
describe "default params" do
|
275
260
|
it "should default to empty hash" do
|
276
|
-
@klass.default_params.
|
261
|
+
expect(@klass.default_params).to eq({})
|
277
262
|
end
|
278
263
|
|
279
264
|
it "should be able to be updated" do
|
280
265
|
new_defaults = {foo: 'bar', baz: 'spax'}
|
281
266
|
@klass.default_params new_defaults
|
282
|
-
@klass.default_params.
|
267
|
+
expect(@klass.default_params).to eq(new_defaults)
|
283
268
|
end
|
284
269
|
end
|
285
270
|
|
286
271
|
describe "default timeout" do
|
287
272
|
it "should default to nil" do
|
288
|
-
@klass.default_options[:timeout].
|
273
|
+
expect(@klass.default_options[:timeout]).to eq(nil)
|
289
274
|
end
|
290
275
|
|
291
276
|
it "should support updating" do
|
292
277
|
@klass.default_timeout 10
|
293
|
-
@klass.default_options[:timeout].
|
278
|
+
expect(@klass.default_options[:timeout]).to eq(10)
|
294
279
|
end
|
295
280
|
|
296
281
|
it "should support floats" do
|
297
282
|
@klass.default_timeout 0.5
|
298
|
-
@klass.default_options[:timeout].
|
283
|
+
expect(@klass.default_options[:timeout]).to eq(0.5)
|
299
284
|
end
|
300
285
|
end
|
301
286
|
|
302
287
|
describe "debug_output" do
|
303
288
|
it "stores the given stream as a default_option" do
|
304
289
|
@klass.debug_output $stdout
|
305
|
-
@klass.default_options[:debug_output].
|
290
|
+
expect(@klass.default_options[:debug_output]).to eq($stdout)
|
306
291
|
end
|
307
292
|
|
308
293
|
it "stores the $stderr stream by default" do
|
309
294
|
@klass.debug_output
|
310
|
-
@klass.default_options[:debug_output].
|
295
|
+
expect(@klass.default_options[:debug_output]).to eq($stderr)
|
311
296
|
end
|
312
297
|
end
|
313
298
|
|
314
299
|
describe "basic http authentication" do
|
315
300
|
it "should work" do
|
316
301
|
@klass.basic_auth 'foobar', 'secret'
|
317
|
-
@klass.default_options[:basic_auth].
|
302
|
+
expect(@klass.default_options[:basic_auth]).to eq({username: 'foobar', password: 'secret'})
|
318
303
|
end
|
319
304
|
end
|
320
305
|
|
321
306
|
describe "digest http authentication" do
|
322
307
|
it "should work" do
|
323
308
|
@klass.digest_auth 'foobar', 'secret'
|
324
|
-
@klass.default_options[:digest_auth].
|
309
|
+
expect(@klass.default_options[:digest_auth]).to eq({username: 'foobar', password: 'secret'})
|
325
310
|
end
|
326
311
|
end
|
327
312
|
|
328
313
|
describe "parser" do
|
329
314
|
class CustomParser
|
330
315
|
def self.parse(body)
|
331
|
-
|
316
|
+
{sexy: true}
|
332
317
|
end
|
333
318
|
end
|
334
319
|
|
335
320
|
let(:parser) do
|
336
|
-
|
321
|
+
proc { |data, format| CustomParser.parse(data) }
|
337
322
|
end
|
338
323
|
|
339
324
|
it "should set parser options" do
|
340
325
|
@klass.parser parser
|
341
|
-
@klass.default_options[:parser].
|
326
|
+
expect(@klass.default_options[:parser]).to eq(parser)
|
342
327
|
end
|
343
328
|
|
344
329
|
it "should be able parse response with custom parser" do
|
345
330
|
@klass.parser parser
|
346
331
|
FakeWeb.register_uri(:get, 'http://twitter.com/statuses/public_timeline.xml', body: 'tweets')
|
347
332
|
custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml')
|
348
|
-
custom_parsed_response[:sexy].
|
333
|
+
expect(custom_parsed_response[:sexy]).to eq(true)
|
349
334
|
end
|
350
335
|
|
351
336
|
it "raises UnsupportedFormat when the parser cannot handle the format" do
|
@@ -362,134 +347,134 @@ describe HTTParty do
|
|
362
347
|
expect do
|
363
348
|
@klass.format :json
|
364
349
|
@klass.parser lambda {|body, format|}
|
365
|
-
end.to_not raise_error
|
350
|
+
end.to_not raise_error
|
366
351
|
end
|
367
352
|
end
|
368
353
|
|
369
354
|
describe "connection_adapter" do
|
370
355
|
let(:uri) { 'http://google.com/api.json' }
|
371
|
-
let(:connection_adapter) {
|
356
|
+
let(:connection_adapter) { double('CustomConnectionAdapter') }
|
372
357
|
|
373
358
|
it "should set the connection_adapter" do
|
374
359
|
@klass.connection_adapter connection_adapter
|
375
|
-
@klass.default_options[:connection_adapter].
|
360
|
+
expect(@klass.default_options[:connection_adapter]).to be connection_adapter
|
376
361
|
end
|
377
362
|
|
378
363
|
it "should set the connection_adapter_options when provided" do
|
379
364
|
options = {foo: :bar}
|
380
365
|
@klass.connection_adapter connection_adapter, options
|
381
|
-
@klass.default_options[:connection_adapter_options].
|
366
|
+
expect(@klass.default_options[:connection_adapter_options]).to be options
|
382
367
|
end
|
383
368
|
|
384
369
|
it "should not set the connection_adapter_options when not provided" do
|
385
370
|
@klass.connection_adapter connection_adapter
|
386
|
-
@klass.default_options[:connection_adapter_options].
|
371
|
+
expect(@klass.default_options[:connection_adapter_options]).to be_nil
|
387
372
|
end
|
388
373
|
|
389
374
|
it "should process a request with a connection from the adapter" do
|
390
375
|
connection_adapter_options = {foo: :bar}
|
391
|
-
connection_adapter.
|
392
|
-
o[:connection_adapter_options].
|
393
|
-
HTTParty::ConnectionAdapter.call(u,o)
|
394
|
-
|
376
|
+
expect(connection_adapter).to receive(:call) { |u, o|
|
377
|
+
expect(o[:connection_adapter_options]).to eq(connection_adapter_options)
|
378
|
+
HTTParty::ConnectionAdapter.call(u, o)
|
379
|
+
}.with(URI.parse(uri), kind_of(Hash))
|
395
380
|
FakeWeb.register_uri(:get, uri, body: 'stuff')
|
396
381
|
@klass.connection_adapter connection_adapter, connection_adapter_options
|
397
|
-
@klass.get(uri).
|
382
|
+
expect(@klass.get(uri).parsed_response).to eq('stuff')
|
398
383
|
end
|
399
384
|
end
|
400
385
|
|
401
386
|
describe "format" do
|
402
387
|
it "should allow xml" do
|
403
388
|
@klass.format :xml
|
404
|
-
@klass.default_options[:format].
|
389
|
+
expect(@klass.default_options[:format]).to eq(:xml)
|
405
390
|
end
|
406
391
|
|
407
392
|
it "should allow csv" do
|
408
393
|
@klass.format :csv
|
409
|
-
@klass.default_options[:format].
|
394
|
+
expect(@klass.default_options[:format]).to eq(:csv)
|
410
395
|
end
|
411
396
|
|
412
397
|
it "should allow json" do
|
413
398
|
@klass.format :json
|
414
|
-
@klass.default_options[:format].
|
399
|
+
expect(@klass.default_options[:format]).to eq(:json)
|
415
400
|
end
|
416
401
|
|
417
402
|
it "should allow plain" do
|
418
403
|
@klass.format :plain
|
419
|
-
@klass.default_options[:format].
|
404
|
+
expect(@klass.default_options[:format]).to eq(:plain)
|
420
405
|
end
|
421
406
|
|
422
407
|
it 'should not allow funky format' do
|
423
|
-
|
408
|
+
expect do
|
424
409
|
@klass.format :foobar
|
425
|
-
end.
|
410
|
+
end.to raise_error(HTTParty::UnsupportedFormat)
|
426
411
|
end
|
427
412
|
|
428
413
|
it 'should only print each format once with an exception' do
|
429
|
-
|
414
|
+
expect do
|
430
415
|
@klass.format :foobar
|
431
|
-
end.
|
416
|
+
end.to raise_error(HTTParty::UnsupportedFormat, "':foobar' Must be one of: csv, html, json, plain, xml")
|
432
417
|
end
|
433
418
|
|
434
419
|
it 'sets the default parser' do
|
435
|
-
@klass.default_options[:parser].
|
420
|
+
expect(@klass.default_options[:parser]).to be_nil
|
436
421
|
@klass.format :json
|
437
|
-
@klass.default_options[:parser].
|
422
|
+
expect(@klass.default_options[:parser]).to eq(HTTParty::Parser)
|
438
423
|
end
|
439
424
|
|
440
425
|
it 'does not reset parser to the default parser' do
|
441
426
|
my_parser = lambda {}
|
442
427
|
@klass.parser my_parser
|
443
428
|
@klass.format :json
|
444
|
-
@klass.parser.
|
429
|
+
expect(@klass.parser).to eq(my_parser)
|
445
430
|
end
|
446
431
|
end
|
447
432
|
|
448
433
|
describe "#no_follow" do
|
449
434
|
it "sets no_follow to false by default" do
|
450
435
|
@klass.no_follow
|
451
|
-
@klass.default_options[:no_follow].
|
436
|
+
expect(@klass.default_options[:no_follow]).to be_falsey
|
452
437
|
end
|
453
438
|
|
454
439
|
it "sets the no_follow option to true" do
|
455
440
|
@klass.no_follow true
|
456
|
-
@klass.default_options[:no_follow].
|
441
|
+
expect(@klass.default_options[:no_follow]).to be_truthy
|
457
442
|
end
|
458
443
|
end
|
459
444
|
|
460
445
|
describe "#maintain_method_across_redirects" do
|
461
446
|
it "sets maintain_method_across_redirects to true by default" do
|
462
447
|
@klass.maintain_method_across_redirects
|
463
|
-
@klass.default_options[:maintain_method_across_redirects].
|
448
|
+
expect(@klass.default_options[:maintain_method_across_redirects]).to be_truthy
|
464
449
|
end
|
465
450
|
|
466
451
|
it "sets the maintain_method_across_redirects option to false" do
|
467
452
|
@klass.maintain_method_across_redirects false
|
468
|
-
@klass.default_options[:maintain_method_across_redirects].
|
453
|
+
expect(@klass.default_options[:maintain_method_across_redirects]).to be_falsey
|
469
454
|
end
|
470
455
|
end
|
471
|
-
|
456
|
+
|
472
457
|
describe "#resend_on_redirect" do
|
473
458
|
it "sets resend_on_redirect to true by default" do
|
474
459
|
@klass.resend_on_redirect
|
475
|
-
@klass.default_options[:resend_on_redirect].
|
460
|
+
expect(@klass.default_options[:resend_on_redirect]).to be_truthy
|
476
461
|
end
|
477
|
-
|
462
|
+
|
478
463
|
it "sets resend_on_redirect option to false" do
|
479
464
|
@klass.resend_on_redirect false
|
480
|
-
@klass.default_options[:resend_on_redirect].
|
465
|
+
expect(@klass.default_options[:resend_on_redirect]).to be_falsey
|
481
466
|
end
|
482
467
|
end
|
483
468
|
|
484
469
|
describe ".follow_redirects" do
|
485
470
|
it "sets follow redirects to true by default" do
|
486
471
|
@klass.follow_redirects
|
487
|
-
@klass.default_options[:follow_redirects].
|
472
|
+
expect(@klass.default_options[:follow_redirects]).to be_truthy
|
488
473
|
end
|
489
474
|
|
490
475
|
it "sets the follow_redirects option to false" do
|
491
476
|
@klass.follow_redirects false
|
492
|
-
@klass.default_options[:follow_redirects].
|
477
|
+
expect(@klass.default_options[:follow_redirects]).to be_falsey
|
493
478
|
end
|
494
479
|
end
|
495
480
|
|
@@ -497,7 +482,7 @@ describe HTTParty do
|
|
497
482
|
it "sets the query_string_normalizer option" do
|
498
483
|
normalizer = proc {}
|
499
484
|
@klass.query_string_normalizer normalizer
|
500
|
-
@klass.default_options[:query_string_normalizer].
|
485
|
+
expect(@klass.default_options[:query_string_normalizer]).to eq(normalizer)
|
501
486
|
end
|
502
487
|
end
|
503
488
|
|
@@ -506,61 +491,61 @@ describe HTTParty do
|
|
506
491
|
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', format: :xml, no_follow: true)
|
507
492
|
@redirect = stub_response 'first redirect', 302
|
508
493
|
@redirect['location'] = 'http://foo.com/bar'
|
509
|
-
HTTParty::Request.
|
494
|
+
allow(HTTParty::Request).to receive_messages(new: @request)
|
510
495
|
end
|
511
496
|
|
512
497
|
it "should fail with redirected GET" do
|
513
|
-
|
498
|
+
expect do
|
514
499
|
@error = @klass.get('/foo', no_follow: true)
|
515
|
-
end.
|
500
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
516
501
|
end
|
517
502
|
|
518
503
|
it "should fail with redirected POST" do
|
519
|
-
|
504
|
+
expect do
|
520
505
|
@klass.post('/foo', no_follow: true)
|
521
|
-
end.
|
506
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
522
507
|
end
|
523
508
|
|
524
509
|
it "should fail with redirected PATCH" do
|
525
|
-
|
510
|
+
expect do
|
526
511
|
@klass.patch('/foo', no_follow: true)
|
527
|
-
end.
|
512
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
528
513
|
end
|
529
514
|
|
530
515
|
it "should fail with redirected DELETE" do
|
531
|
-
|
516
|
+
expect do
|
532
517
|
@klass.delete('/foo', no_follow: true)
|
533
|
-
end.
|
518
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
534
519
|
end
|
535
520
|
|
536
521
|
it "should fail with redirected MOVE" do
|
537
|
-
|
522
|
+
expect do
|
538
523
|
@klass.move('/foo', no_follow: true)
|
539
|
-
end.
|
524
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
540
525
|
end
|
541
526
|
|
542
527
|
it "should fail with redirected COPY" do
|
543
|
-
|
528
|
+
expect do
|
544
529
|
@klass.copy('/foo', no_follow: true)
|
545
|
-
end.
|
530
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
546
531
|
end
|
547
532
|
|
548
533
|
it "should fail with redirected PUT" do
|
549
|
-
|
534
|
+
expect do
|
550
535
|
@klass.put('/foo', no_follow: true)
|
551
|
-
end.
|
536
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
552
537
|
end
|
553
538
|
|
554
539
|
it "should fail with redirected HEAD" do
|
555
|
-
|
540
|
+
expect do
|
556
541
|
@klass.head('/foo', no_follow: true)
|
557
|
-
end.
|
542
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
558
543
|
end
|
559
544
|
|
560
545
|
it "should fail with redirected OPTIONS" do
|
561
|
-
|
546
|
+
expect do
|
562
547
|
@klass.options('/foo', no_follow: true)
|
563
|
-
end.
|
548
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
564
549
|
end
|
565
550
|
end
|
566
551
|
|
@@ -580,8 +565,8 @@ describe HTTParty do
|
|
580
565
|
end
|
581
566
|
|
582
567
|
it "should not run over each others options" do
|
583
|
-
@klass.default_options.
|
584
|
-
@additional_klass.default_options.
|
568
|
+
expect(@klass.default_options).to eq({ base_uri: 'http://first.com', default_params: { one: 1 } })
|
569
|
+
expect(@additional_klass.default_options).to eq({ base_uri: 'http://second.com', default_params: { two: 2 } })
|
585
570
|
end
|
586
571
|
end
|
587
572
|
|
@@ -602,68 +587,68 @@ describe HTTParty do
|
|
602
587
|
@child1.default_params joe: "alive"
|
603
588
|
@child2.default_params joe: "dead"
|
604
589
|
|
605
|
-
@child1.default_options.
|
606
|
-
@child2.default_options.
|
590
|
+
expect(@child1.default_options).to eq({ default_params: {joe: "alive"} })
|
591
|
+
expect(@child2.default_options).to eq({ default_params: {joe: "dead"} })
|
607
592
|
|
608
|
-
@parent.default_options.
|
593
|
+
expect(@parent.default_options).to eq({ })
|
609
594
|
end
|
610
595
|
|
611
596
|
it "inherits default_options from the superclass" do
|
612
597
|
@parent.basic_auth 'user', 'password'
|
613
|
-
@child1.default_options.
|
598
|
+
expect(@child1.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
|
614
599
|
@child1.basic_auth 'u', 'p' # modifying child1 has no effect on child2
|
615
|
-
@child2.default_options.
|
600
|
+
expect(@child2.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
|
616
601
|
end
|
617
602
|
|
618
603
|
it "doesn't modify the parent's default options" do
|
619
604
|
@parent.basic_auth 'user', 'password'
|
620
605
|
|
621
606
|
@child1.basic_auth 'u', 'p'
|
622
|
-
@child1.default_options.
|
607
|
+
expect(@child1.default_options).to eq({basic_auth: {username: 'u', password: 'p'}})
|
623
608
|
|
624
609
|
@child1.basic_auth 'email', 'token'
|
625
|
-
@child1.default_options.
|
610
|
+
expect(@child1.default_options).to eq({basic_auth: {username: 'email', password: 'token'}})
|
626
611
|
|
627
|
-
@parent.default_options.
|
612
|
+
expect(@parent.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
|
628
613
|
end
|
629
614
|
|
630
615
|
it "doesn't modify hashes in the parent's default options" do
|
631
616
|
@parent.headers 'Accept' => 'application/json'
|
632
617
|
@child1.headers 'Accept' => 'application/xml'
|
633
618
|
|
634
|
-
@parent.default_options[:headers].
|
635
|
-
@child1.default_options[:headers].
|
619
|
+
expect(@parent.default_options[:headers]).to eq({'Accept' => 'application/json'})
|
620
|
+
expect(@child1.default_options[:headers]).to eq({'Accept' => 'application/xml'})
|
636
621
|
end
|
637
622
|
|
638
623
|
it "works with lambda values" do
|
639
624
|
@child1.default_options[:imaginary_option] = lambda { "This is a new lambda "}
|
640
|
-
@child1.default_options[:imaginary_option].
|
625
|
+
expect(@child1.default_options[:imaginary_option]).to be_a Proc
|
641
626
|
end
|
642
627
|
|
643
628
|
it 'should dup the proc on the child class' do
|
644
629
|
imaginary_option = lambda { 2 * 3.14 }
|
645
630
|
@parent.default_options[:imaginary_option] = imaginary_option
|
646
|
-
@parent.default_options[:imaginary_option].call.
|
631
|
+
expect(@parent.default_options[:imaginary_option].call).to eq(imaginary_option.call)
|
647
632
|
@child1.default_options[:imaginary_option]
|
648
|
-
@child1.default_options[:imaginary_option].call.
|
649
|
-
@child1.default_options[:imaginary_option].
|
633
|
+
expect(@child1.default_options[:imaginary_option].call).to eq(imaginary_option.call)
|
634
|
+
expect(@child1.default_options[:imaginary_option]).not_to be_equal imaginary_option
|
650
635
|
end
|
651
636
|
|
652
637
|
it "inherits default_cookies from the parent class" do
|
653
638
|
@parent.cookies 'type' => 'chocolate_chip'
|
654
|
-
@child1.default_cookies.
|
639
|
+
expect(@child1.default_cookies).to eq({"type" => "chocolate_chip"})
|
655
640
|
@child1.cookies 'type' => 'snickerdoodle'
|
656
|
-
@child1.default_cookies.
|
657
|
-
@child2.default_cookies.
|
641
|
+
expect(@child1.default_cookies).to eq({"type" => "snickerdoodle"})
|
642
|
+
expect(@child2.default_cookies).to eq({"type" => "chocolate_chip"})
|
658
643
|
end
|
659
644
|
|
660
645
|
it "doesn't modify the parent's default cookies" do
|
661
646
|
@parent.cookies 'type' => 'chocolate_chip'
|
662
647
|
|
663
648
|
@child1.cookies 'type' => 'snickerdoodle'
|
664
|
-
@child1.default_cookies.
|
649
|
+
expect(@child1.default_cookies).to eq({"type" => "snickerdoodle"})
|
665
650
|
|
666
|
-
@parent.default_cookies.
|
651
|
+
expect(@parent.default_cookies).to eq({"type" => "chocolate_chip"})
|
667
652
|
end
|
668
653
|
end
|
669
654
|
|
@@ -680,40 +665,44 @@ describe HTTParty do
|
|
680
665
|
end
|
681
666
|
it "continues running the #inherited on the parent" do
|
682
667
|
child = Class.new(@parent)
|
683
|
-
child.instance_variable_get(:@grand_parent).
|
668
|
+
expect(child.instance_variable_get(:@grand_parent)).to be_truthy
|
684
669
|
end
|
685
670
|
end
|
686
671
|
|
687
672
|
describe "#get" do
|
688
673
|
it "should be able to get html" do
|
689
674
|
stub_http_response_with('google.html')
|
690
|
-
HTTParty.get('http://www.google.com').
|
675
|
+
expect(HTTParty.get('http://www.google.com').parsed_response).to eq(file_fixture('google.html'))
|
691
676
|
end
|
692
677
|
|
693
678
|
it "should be able to get chunked html" do
|
694
|
-
chunks =
|
679
|
+
chunks = %w(Chunk1 Chunk2 Chunk3 Chunk4)
|
695
680
|
stub_chunked_http_response_with(chunks)
|
696
681
|
|
697
|
-
|
698
|
-
|
699
|
-
|
682
|
+
expect(
|
683
|
+
HTTParty.get('http://www.google.com') do |fragment|
|
684
|
+
expect(chunks).to include(fragment)
|
685
|
+
end.parsed_response
|
686
|
+
).to eq(chunks.join)
|
700
687
|
end
|
701
688
|
|
702
689
|
it "should return an empty body if stream_body option is turned on" do
|
703
|
-
chunks =
|
690
|
+
chunks = %w(Chunk1 Chunk2 Chunk3 Chunk4)
|
704
691
|
options = {stream_body: true, format: 'html'}
|
705
692
|
stub_chunked_http_response_with(chunks, options)
|
706
693
|
|
707
|
-
|
708
|
-
|
709
|
-
|
694
|
+
expect(
|
695
|
+
HTTParty.get('http://www.google.com', options) do |fragment|
|
696
|
+
expect(chunks).to include(fragment)
|
697
|
+
end.parsed_response
|
698
|
+
).to eq(nil)
|
710
699
|
end
|
711
700
|
|
712
701
|
it "should be able parse response type json automatically" do
|
713
702
|
stub_http_response_with('twitter.json')
|
714
703
|
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
|
715
|
-
tweets.size.
|
716
|
-
tweets.first['user'].
|
704
|
+
expect(tweets.size).to eq(20)
|
705
|
+
expect(tweets.first['user']).to eq({
|
717
706
|
"name" => "Pyk",
|
718
707
|
"url" => nil,
|
719
708
|
"id" => "7694602",
|
@@ -723,14 +712,14 @@ describe HTTParty do
|
|
723
712
|
"followers_count" => 1,
|
724
713
|
"location" => "Opera Plaza, California",
|
725
714
|
"profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
|
726
|
-
}
|
715
|
+
})
|
727
716
|
end
|
728
717
|
|
729
718
|
it "should be able parse response type xml automatically" do
|
730
719
|
stub_http_response_with('twitter.xml')
|
731
720
|
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.xml')
|
732
|
-
tweets['statuses'].size.
|
733
|
-
tweets['statuses'].first['user'].
|
721
|
+
expect(tweets['statuses'].size).to eq(20)
|
722
|
+
expect(tweets['statuses'].first['user']).to eq({
|
734
723
|
"name" => "Magic 8 Bot",
|
735
724
|
"url" => nil,
|
736
725
|
"id" => "17656026",
|
@@ -740,54 +729,54 @@ describe HTTParty do
|
|
740
729
|
"followers_count" => "90",
|
741
730
|
"profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
|
742
731
|
"location" => nil
|
743
|
-
}
|
732
|
+
})
|
744
733
|
end
|
745
734
|
|
746
735
|
it "should be able parse response type csv automatically" do
|
747
736
|
stub_http_response_with('twitter.csv')
|
748
737
|
profile = HTTParty.get('http://twitter.com/statuses/profile.csv')
|
749
|
-
profile.size.
|
750
|
-
profile[0].
|
751
|
-
profile[1].
|
738
|
+
expect(profile.size).to eq(2)
|
739
|
+
expect(profile[0]).to eq(%w(name url id description protected screen_name followers_count profile_image_url location))
|
740
|
+
expect(profile[1]).to eq(["Magic 8 Bot", nil, "17656026", "ask me a question", "false", "magic8bot", "90", "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg", nil])
|
752
741
|
end
|
753
742
|
|
754
743
|
it "should not get undefined method add_node for nil class for the following xml" do
|
755
744
|
stub_http_response_with('undefined_method_add_node_for_nil.xml')
|
756
745
|
result = HTTParty.get('http://foobar.com')
|
757
|
-
result.
|
746
|
+
expect(result.parsed_response).to eq({"Entities" => {"href" => "https://s3-sandbox.parature.com/api/v1/5578/5633/Account", "results" => "0", "total" => "0", "page_size" => "25", "page" => "1"}})
|
758
747
|
end
|
759
748
|
|
760
749
|
it "should parse empty response fine" do
|
761
750
|
stub_http_response_with('empty.xml')
|
762
751
|
result = HTTParty.get('http://foobar.com')
|
763
|
-
result.
|
752
|
+
expect(result).to be_nil
|
764
753
|
end
|
765
754
|
|
766
755
|
it "should accept http URIs" do
|
767
756
|
stub_http_response_with('google.html')
|
768
|
-
|
757
|
+
expect do
|
769
758
|
HTTParty.get('http://google.com')
|
770
|
-
end.
|
759
|
+
end.not_to raise_error
|
771
760
|
end
|
772
761
|
|
773
762
|
it "should accept https URIs" do
|
774
763
|
stub_http_response_with('google.html')
|
775
|
-
|
764
|
+
expect do
|
776
765
|
HTTParty.get('https://google.com')
|
777
|
-
end.
|
766
|
+
end.not_to raise_error
|
778
767
|
end
|
779
768
|
|
780
769
|
it "should accept webcal URIs" do
|
781
770
|
stub_http_response_with('google.html')
|
782
|
-
|
771
|
+
expect do
|
783
772
|
HTTParty.get('webcal://google.com')
|
784
|
-
end.
|
773
|
+
end.not_to raise_error
|
785
774
|
end
|
786
775
|
|
787
776
|
it "should raise an InvalidURIError on URIs that can't be parsed at all" do
|
788
|
-
|
777
|
+
expect do
|
789
778
|
HTTParty.get("It's the one that says 'Bad URI'")
|
790
|
-
end.
|
779
|
+
end.to raise_error(URI::InvalidURIError)
|
791
780
|
end
|
792
781
|
end
|
793
782
|
end
|