httparty 0.13.0 → 0.14.0
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/.travis.yml +4 -2
- data/CONTRIBUTING.md +23 -0
- data/Gemfile +8 -3
- data/Guardfile +3 -3
- data/History +106 -11
- data/README.md +19 -20
- data/Rakefile +5 -7
- data/bin/httparty +18 -14
- data/docs/README.md +100 -0
- data/examples/README.md +67 -0
- data/examples/aaws.rb +5 -5
- data/examples/basic.rb +6 -10
- data/examples/crack.rb +2 -2
- data/examples/custom_parsers.rb +1 -4
- data/examples/delicious.rb +8 -8
- data/examples/google.rb +2 -2
- data/examples/headers_and_user_agents.rb +1 -1
- data/examples/logging.rb +36 -0
- data/examples/nokogiri_html_parser.rb +0 -3
- data/examples/rescue_json.rb +17 -0
- data/examples/rubyurl.rb +3 -3
- data/examples/stackexchange.rb +24 -0
- data/examples/tripit_sign_in.rb +20 -9
- data/examples/twitter.rb +7 -7
- data/examples/whoismyrep.rb +1 -1
- data/features/command_line.feature +90 -2
- data/features/digest_authentication.feature +10 -0
- data/features/steps/env.rb +16 -11
- data/features/steps/httparty_response_steps.rb +18 -14
- data/features/steps/httparty_steps.rb +10 -2
- data/features/steps/mongrel_helper.rb +35 -2
- data/features/steps/remote_service_steps.rb +26 -8
- data/features/supports_read_timeout_option.feature +13 -0
- data/httparty.gemspec +6 -5
- data/lib/httparty/connection_adapter.rb +36 -13
- data/lib/httparty/cookie_hash.rb +3 -4
- data/lib/httparty/exceptions.rb +4 -1
- data/lib/httparty/hash_conversions.rb +17 -15
- data/lib/httparty/logger/{apache_logger.rb → apache_formatter.rb} +3 -3
- data/lib/httparty/logger/curl_formatter.rb +91 -0
- data/lib/httparty/logger/logger.rb +18 -10
- data/lib/httparty/module_inheritable_attributes.rb +1 -1
- data/lib/httparty/net_digest_auth.rb +69 -18
- data/lib/httparty/parser.rb +4 -2
- data/lib/httparty/request.rb +105 -48
- data/lib/httparty/response.rb +31 -6
- data/lib/httparty/version.rb +1 -1
- data/lib/httparty.rb +132 -72
- data/spec/httparty/connection_adapter_spec.rb +285 -88
- data/spec/httparty/cookie_hash_spec.rb +46 -29
- data/spec/httparty/exception_spec.rb +29 -7
- data/spec/httparty/hash_conversions_spec.rb +49 -0
- data/spec/httparty/logger/apache_formatter_spec.rb +41 -0
- data/spec/httparty/logger/curl_formatter_spec.rb +119 -0
- data/spec/httparty/logger/logger_spec.rb +23 -7
- data/spec/httparty/net_digest_auth_spec.rb +118 -30
- data/spec/httparty/parser_spec.rb +43 -35
- data/spec/httparty/request_spec.rb +734 -182
- data/spec/httparty/response_spec.rb +139 -69
- data/spec/httparty/ssl_spec.rb +22 -22
- data/spec/httparty_spec.rb +307 -199
- data/spec/spec_helper.rb +34 -12
- data/spec/support/ssl_test_helper.rb +6 -6
- data/spec/support/ssl_test_server.rb +21 -21
- data/spec/support/stub_response.rb +20 -14
- data/website/index.html +3 -3
- metadata +30 -33
- data/lib/httparty/core_extensions.rb +0 -32
- data/lib/httparty/logger/curl_logger.rb +0 -48
- data/spec/httparty/logger/apache_logger_spec.rb +0 -26
- data/spec/httparty/logger/curl_logger_spec.rb +0 -18
- data/spec/spec.opts +0 -2
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,139 +72,146 @@ 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.
|
143
|
-
.with(anything, anything, hash_including({ :
|
144
|
-
.and_return(
|
126
|
+
def expect_headers(header = {})
|
127
|
+
expect(HTTParty::Request).to receive(:new) \
|
128
|
+
.with(anything, anything, hash_including({ headers: header })) \
|
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
|
-
init_headers = {:
|
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
|
158
|
-
expect_headers(:
|
159
|
-
@klass.headers(:
|
143
|
+
expect_headers(foo: 'bar')
|
144
|
+
@klass.headers(foo: 'bar')
|
160
145
|
@klass.get('')
|
161
146
|
end
|
162
147
|
|
163
148
|
it "merges class headers with request headers" do
|
164
|
-
expect_headers(:
|
165
|
-
@klass.headers(:
|
166
|
-
@klass.get('', :
|
149
|
+
expect_headers(baz: 'spax', foo: 'bar')
|
150
|
+
@klass.headers(foo: 'bar')
|
151
|
+
@klass.get('', headers: {baz: 'spax'})
|
167
152
|
end
|
168
153
|
|
169
154
|
it 'overrides class headers with request headers' do
|
170
|
-
expect_headers(:
|
171
|
-
@klass.headers(:
|
172
|
-
@klass.get('', :
|
155
|
+
expect_headers(baz: 'spax', foo: 'baz')
|
156
|
+
@klass.headers(foo: 'bar')
|
157
|
+
@klass.get('', headers: {baz: 'spax', foo: 'baz'})
|
173
158
|
end
|
174
159
|
|
175
160
|
context "with cookies" do
|
176
161
|
it 'utilizes the class-level cookies' do
|
177
|
-
expect_headers(:
|
178
|
-
@klass.headers(:
|
179
|
-
@klass.cookies(:
|
162
|
+
expect_headers(foo: 'bar', 'cookie' => 'type=snickerdoodle')
|
163
|
+
@klass.headers(foo: 'bar')
|
164
|
+
@klass.cookies(type: 'snickerdoodle')
|
180
165
|
@klass.get('')
|
181
166
|
end
|
182
167
|
|
183
168
|
it 'adds cookies to the headers' do
|
184
|
-
expect_headers(:
|
185
|
-
@klass.headers(:
|
186
|
-
@klass.get('', :
|
169
|
+
expect_headers(foo: 'bar', 'cookie' => 'type=snickerdoodle')
|
170
|
+
@klass.headers(foo: 'bar')
|
171
|
+
@klass.get('', cookies: {type: 'snickerdoodle'})
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'doesnt modify default_options' do
|
175
|
+
expect(@klass.headers).to eq({})
|
176
|
+
expect_headers('cookie' => 'type=snickerdoodle')
|
177
|
+
@klass.get('', cookies: {type: 'snickerdoodle'})
|
178
|
+
expect(@klass.default_options[:headers]).to eq({})
|
187
179
|
end
|
188
180
|
|
189
181
|
it 'adds optional cookies to the optional headers' do
|
190
|
-
expect_headers(:
|
191
|
-
@klass.get('', :
|
182
|
+
expect_headers(baz: 'spax', 'cookie' => 'type=snickerdoodle')
|
183
|
+
@klass.get('', cookies: {type: 'snickerdoodle'}, headers: {baz: 'spax'})
|
192
184
|
end
|
193
185
|
end
|
194
186
|
end
|
195
187
|
|
196
188
|
describe "cookies" do
|
197
189
|
def expect_cookie_header(s)
|
198
|
-
HTTParty::Request.
|
199
|
-
.with(anything, anything, hash_including({ :
|
200
|
-
.and_return(
|
190
|
+
expect(HTTParty::Request).to receive(:new) \
|
191
|
+
.with(anything, anything, hash_including({ headers: { "cookie" => s } })) \
|
192
|
+
.and_return(double("mock response", perform: nil))
|
201
193
|
end
|
202
194
|
|
203
195
|
it "should not be in the headers by default" do
|
204
|
-
HTTParty::Request.
|
196
|
+
allow(HTTParty::Request).to receive(:new).and_return(double(nil, perform: nil))
|
205
197
|
@klass.get("")
|
206
|
-
@klass.headers.keys.
|
198
|
+
expect(@klass.headers.keys).not_to include("cookie")
|
207
199
|
end
|
208
200
|
|
209
201
|
it "should raise an ArgumentError if passed a non-Hash" do
|
210
|
-
|
202
|
+
expect do
|
211
203
|
@klass.cookies("nonsense")
|
212
|
-
end.
|
204
|
+
end.to raise_error(ArgumentError)
|
213
205
|
end
|
214
206
|
|
215
207
|
it "should allow a cookie to be specified with a one-off request" do
|
216
208
|
expect_cookie_header "type=snickerdoodle"
|
217
|
-
@klass.get("", :
|
209
|
+
@klass.get("", cookies: { type: "snickerdoodle" })
|
218
210
|
end
|
219
211
|
|
220
212
|
describe "when a cookie is set at the class level" do
|
221
213
|
before(:each) do
|
222
|
-
@klass.cookies({ :
|
214
|
+
@klass.cookies({ type: "snickerdoodle" })
|
223
215
|
end
|
224
216
|
|
225
217
|
it "should include that cookie in the request" do
|
@@ -237,7 +229,7 @@ describe HTTParty do
|
|
237
229
|
it "should allow the class defaults to be overridden" do
|
238
230
|
expect_cookie_header "type=chocolate_chip"
|
239
231
|
|
240
|
-
@klass.get("", :
|
232
|
+
@klass.get("", cookies: { type: "chocolate_chip" })
|
241
233
|
end
|
242
234
|
end
|
243
235
|
|
@@ -245,11 +237,11 @@ describe HTTParty do
|
|
245
237
|
before(:each) do
|
246
238
|
@klass.instance_eval do
|
247
239
|
def first_method
|
248
|
-
get("first_method", :
|
240
|
+
get("first_method", cookies: { first_method_cookie: "foo" })
|
249
241
|
end
|
250
242
|
|
251
243
|
def second_method
|
252
|
-
get("second_method", :
|
244
|
+
get("second_method", cookies: { second_method_cookie: "foo" })
|
253
245
|
end
|
254
246
|
end
|
255
247
|
end
|
@@ -266,79 +258,79 @@ describe HTTParty do
|
|
266
258
|
|
267
259
|
describe "default params" do
|
268
260
|
it "should default to empty hash" do
|
269
|
-
@klass.default_params.
|
261
|
+
expect(@klass.default_params).to eq({})
|
270
262
|
end
|
271
263
|
|
272
264
|
it "should be able to be updated" do
|
273
|
-
new_defaults = {:
|
265
|
+
new_defaults = {foo: 'bar', baz: 'spax'}
|
274
266
|
@klass.default_params new_defaults
|
275
|
-
@klass.default_params.
|
267
|
+
expect(@klass.default_params).to eq(new_defaults)
|
276
268
|
end
|
277
269
|
end
|
278
270
|
|
279
271
|
describe "default timeout" do
|
280
272
|
it "should default to nil" do
|
281
|
-
@klass.default_options[:timeout].
|
273
|
+
expect(@klass.default_options[:timeout]).to eq(nil)
|
282
274
|
end
|
283
275
|
|
284
276
|
it "should support updating" do
|
285
277
|
@klass.default_timeout 10
|
286
|
-
@klass.default_options[:timeout].
|
278
|
+
expect(@klass.default_options[:timeout]).to eq(10)
|
287
279
|
end
|
288
280
|
|
289
281
|
it "should support floats" do
|
290
282
|
@klass.default_timeout 0.5
|
291
|
-
@klass.default_options[:timeout].
|
283
|
+
expect(@klass.default_options[:timeout]).to eq(0.5)
|
292
284
|
end
|
293
285
|
end
|
294
286
|
|
295
287
|
describe "debug_output" do
|
296
288
|
it "stores the given stream as a default_option" do
|
297
289
|
@klass.debug_output $stdout
|
298
|
-
@klass.default_options[:debug_output].
|
290
|
+
expect(@klass.default_options[:debug_output]).to eq($stdout)
|
299
291
|
end
|
300
292
|
|
301
293
|
it "stores the $stderr stream by default" do
|
302
294
|
@klass.debug_output
|
303
|
-
@klass.default_options[:debug_output].
|
295
|
+
expect(@klass.default_options[:debug_output]).to eq($stderr)
|
304
296
|
end
|
305
297
|
end
|
306
298
|
|
307
299
|
describe "basic http authentication" do
|
308
300
|
it "should work" do
|
309
301
|
@klass.basic_auth 'foobar', 'secret'
|
310
|
-
@klass.default_options[:basic_auth].
|
302
|
+
expect(@klass.default_options[:basic_auth]).to eq({username: 'foobar', password: 'secret'})
|
311
303
|
end
|
312
304
|
end
|
313
305
|
|
314
306
|
describe "digest http authentication" do
|
315
307
|
it "should work" do
|
316
308
|
@klass.digest_auth 'foobar', 'secret'
|
317
|
-
@klass.default_options[:digest_auth].
|
309
|
+
expect(@klass.default_options[:digest_auth]).to eq({username: 'foobar', password: 'secret'})
|
318
310
|
end
|
319
311
|
end
|
320
312
|
|
321
313
|
describe "parser" do
|
322
314
|
class CustomParser
|
323
315
|
def self.parse(body)
|
324
|
-
|
316
|
+
{sexy: true}
|
325
317
|
end
|
326
318
|
end
|
327
319
|
|
328
320
|
let(:parser) do
|
329
|
-
|
321
|
+
proc { |data, format| CustomParser.parse(data) }
|
330
322
|
end
|
331
323
|
|
332
324
|
it "should set parser options" do
|
333
325
|
@klass.parser parser
|
334
|
-
@klass.default_options[:parser].
|
326
|
+
expect(@klass.default_options[:parser]).to eq(parser)
|
335
327
|
end
|
336
328
|
|
337
329
|
it "should be able parse response with custom parser" do
|
338
330
|
@klass.parser parser
|
339
|
-
FakeWeb.register_uri(:get, 'http://twitter.com/statuses/public_timeline.xml', :
|
331
|
+
FakeWeb.register_uri(:get, 'http://twitter.com/statuses/public_timeline.xml', body: 'tweets')
|
340
332
|
custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml')
|
341
|
-
custom_parsed_response[:sexy].
|
333
|
+
expect(custom_parsed_response[:sexy]).to eq(true)
|
342
334
|
end
|
343
335
|
|
344
336
|
it "raises UnsupportedFormat when the parser cannot handle the format" do
|
@@ -355,122 +347,173 @@ describe HTTParty do
|
|
355
347
|
expect do
|
356
348
|
@klass.format :json
|
357
349
|
@klass.parser lambda {|body, format|}
|
358
|
-
end.to_not raise_error
|
350
|
+
end.to_not raise_error
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
describe "uri_adapter" do
|
355
|
+
|
356
|
+
require 'forwardable'
|
357
|
+
class CustomURIAdaptor
|
358
|
+
extend Forwardable
|
359
|
+
def_delegators :@uri, :userinfo, :relative?, :query, :query=, :scheme, :path, :host, :port
|
360
|
+
|
361
|
+
def initialize uri
|
362
|
+
@uri = uri
|
363
|
+
end
|
364
|
+
|
365
|
+
def self.parse uri
|
366
|
+
new URI.parse uri
|
367
|
+
end
|
359
368
|
end
|
369
|
+
|
370
|
+
let(:uri_adapter) { CustomURIAdaptor }
|
371
|
+
|
372
|
+
it "should set the uri_adapter" do
|
373
|
+
@klass.uri_adapter uri_adapter
|
374
|
+
expect(@klass.default_options[:uri_adapter]).to be uri_adapter
|
375
|
+
end
|
376
|
+
|
377
|
+
it "should raise an ArgumentError if uri_adapter doesn't implement parse method" do
|
378
|
+
expect do
|
379
|
+
@klass.uri_adapter double()
|
380
|
+
end.to raise_error(ArgumentError)
|
381
|
+
end
|
382
|
+
|
383
|
+
|
384
|
+
it "should process a request with a uri instance parsed from the uri_adapter" do
|
385
|
+
uri = 'http://foo.com/bar'
|
386
|
+
FakeWeb.register_uri(:get, uri, body: 'stuff')
|
387
|
+
@klass.uri_adapter uri_adapter
|
388
|
+
expect(@klass.get(uri).parsed_response).to eq('stuff')
|
389
|
+
end
|
390
|
+
|
360
391
|
end
|
361
392
|
|
362
393
|
describe "connection_adapter" do
|
363
394
|
let(:uri) { 'http://google.com/api.json' }
|
364
|
-
let(:connection_adapter) {
|
395
|
+
let(:connection_adapter) { double('CustomConnectionAdapter') }
|
365
396
|
|
366
397
|
it "should set the connection_adapter" do
|
367
398
|
@klass.connection_adapter connection_adapter
|
368
|
-
@klass.default_options[:connection_adapter].
|
399
|
+
expect(@klass.default_options[:connection_adapter]).to be connection_adapter
|
369
400
|
end
|
370
401
|
|
371
402
|
it "should set the connection_adapter_options when provided" do
|
372
|
-
options = {:
|
403
|
+
options = {foo: :bar}
|
373
404
|
@klass.connection_adapter connection_adapter, options
|
374
|
-
@klass.default_options[:connection_adapter_options].
|
405
|
+
expect(@klass.default_options[:connection_adapter_options]).to be options
|
375
406
|
end
|
376
407
|
|
377
408
|
it "should not set the connection_adapter_options when not provided" do
|
378
409
|
@klass.connection_adapter connection_adapter
|
379
|
-
@klass.default_options[:connection_adapter_options].
|
410
|
+
expect(@klass.default_options[:connection_adapter_options]).to be_nil
|
380
411
|
end
|
381
412
|
|
382
413
|
it "should process a request with a connection from the adapter" do
|
383
|
-
connection_adapter_options = {:
|
384
|
-
connection_adapter.
|
385
|
-
o[:connection_adapter_options].
|
386
|
-
HTTParty::ConnectionAdapter.call(u,o)
|
387
|
-
|
388
|
-
FakeWeb.register_uri(:get, uri, :
|
414
|
+
connection_adapter_options = {foo: :bar}
|
415
|
+
expect(connection_adapter).to receive(:call) { |u, o|
|
416
|
+
expect(o[:connection_adapter_options]).to eq(connection_adapter_options)
|
417
|
+
HTTParty::ConnectionAdapter.call(u, o)
|
418
|
+
}.with(URI.parse(uri), kind_of(Hash))
|
419
|
+
FakeWeb.register_uri(:get, uri, body: 'stuff')
|
389
420
|
@klass.connection_adapter connection_adapter, connection_adapter_options
|
390
|
-
@klass.get(uri).
|
421
|
+
expect(@klass.get(uri).parsed_response).to eq('stuff')
|
391
422
|
end
|
392
423
|
end
|
393
424
|
|
394
425
|
describe "format" do
|
395
426
|
it "should allow xml" do
|
396
427
|
@klass.format :xml
|
397
|
-
@klass.default_options[:format].
|
428
|
+
expect(@klass.default_options[:format]).to eq(:xml)
|
398
429
|
end
|
399
430
|
|
400
431
|
it "should allow csv" do
|
401
432
|
@klass.format :csv
|
402
|
-
@klass.default_options[:format].
|
433
|
+
expect(@klass.default_options[:format]).to eq(:csv)
|
403
434
|
end
|
404
435
|
|
405
436
|
it "should allow json" do
|
406
437
|
@klass.format :json
|
407
|
-
@klass.default_options[:format].
|
438
|
+
expect(@klass.default_options[:format]).to eq(:json)
|
408
439
|
end
|
409
440
|
|
410
441
|
it "should allow plain" do
|
411
442
|
@klass.format :plain
|
412
|
-
@klass.default_options[:format].
|
443
|
+
expect(@klass.default_options[:format]).to eq(:plain)
|
413
444
|
end
|
414
445
|
|
415
446
|
it 'should not allow funky format' do
|
416
|
-
|
447
|
+
expect do
|
417
448
|
@klass.format :foobar
|
418
|
-
end.
|
449
|
+
end.to raise_error(HTTParty::UnsupportedFormat)
|
419
450
|
end
|
420
451
|
|
421
452
|
it 'should only print each format once with an exception' do
|
422
|
-
|
453
|
+
expect do
|
423
454
|
@klass.format :foobar
|
424
|
-
end.
|
455
|
+
end.to raise_error(HTTParty::UnsupportedFormat, "':foobar' Must be one of: csv, html, json, plain, xml")
|
425
456
|
end
|
426
457
|
|
427
458
|
it 'sets the default parser' do
|
428
|
-
@klass.default_options[:parser].
|
459
|
+
expect(@klass.default_options[:parser]).to be_nil
|
429
460
|
@klass.format :json
|
430
|
-
@klass.default_options[:parser].
|
461
|
+
expect(@klass.default_options[:parser]).to eq(HTTParty::Parser)
|
431
462
|
end
|
432
463
|
|
433
464
|
it 'does not reset parser to the default parser' do
|
434
465
|
my_parser = lambda {}
|
435
466
|
@klass.parser my_parser
|
436
467
|
@klass.format :json
|
437
|
-
@klass.parser.
|
468
|
+
expect(@klass.parser).to eq(my_parser)
|
438
469
|
end
|
439
470
|
end
|
440
471
|
|
441
472
|
describe "#no_follow" do
|
442
473
|
it "sets no_follow to false by default" do
|
443
474
|
@klass.no_follow
|
444
|
-
@klass.default_options[:no_follow].
|
475
|
+
expect(@klass.default_options[:no_follow]).to be_falsey
|
445
476
|
end
|
446
477
|
|
447
478
|
it "sets the no_follow option to true" do
|
448
479
|
@klass.no_follow true
|
449
|
-
@klass.default_options[:no_follow].
|
480
|
+
expect(@klass.default_options[:no_follow]).to be_truthy
|
450
481
|
end
|
451
482
|
end
|
452
483
|
|
453
484
|
describe "#maintain_method_across_redirects" do
|
454
485
|
it "sets maintain_method_across_redirects to true by default" do
|
455
486
|
@klass.maintain_method_across_redirects
|
456
|
-
@klass.default_options[:maintain_method_across_redirects].
|
487
|
+
expect(@klass.default_options[:maintain_method_across_redirects]).to be_truthy
|
457
488
|
end
|
458
489
|
|
459
490
|
it "sets the maintain_method_across_redirects option to false" do
|
460
491
|
@klass.maintain_method_across_redirects false
|
461
|
-
@klass.default_options[:maintain_method_across_redirects].
|
492
|
+
expect(@klass.default_options[:maintain_method_across_redirects]).to be_falsey
|
493
|
+
end
|
494
|
+
end
|
495
|
+
|
496
|
+
describe "#resend_on_redirect" do
|
497
|
+
it "sets resend_on_redirect to true by default" do
|
498
|
+
@klass.resend_on_redirect
|
499
|
+
expect(@klass.default_options[:resend_on_redirect]).to be_truthy
|
500
|
+
end
|
501
|
+
|
502
|
+
it "sets resend_on_redirect option to false" do
|
503
|
+
@klass.resend_on_redirect false
|
504
|
+
expect(@klass.default_options[:resend_on_redirect]).to be_falsey
|
462
505
|
end
|
463
506
|
end
|
464
507
|
|
465
508
|
describe ".follow_redirects" do
|
466
509
|
it "sets follow redirects to true by default" do
|
467
510
|
@klass.follow_redirects
|
468
|
-
@klass.default_options[:follow_redirects].
|
511
|
+
expect(@klass.default_options[:follow_redirects]).to be_truthy
|
469
512
|
end
|
470
513
|
|
471
514
|
it "sets the follow_redirects option to false" do
|
472
515
|
@klass.follow_redirects false
|
473
|
-
@klass.default_options[:follow_redirects].
|
516
|
+
expect(@klass.default_options[:follow_redirects]).to be_falsey
|
474
517
|
end
|
475
518
|
end
|
476
519
|
|
@@ -478,70 +521,119 @@ describe HTTParty do
|
|
478
521
|
it "sets the query_string_normalizer option" do
|
479
522
|
normalizer = proc {}
|
480
523
|
@klass.query_string_normalizer normalizer
|
481
|
-
@klass.default_options[:query_string_normalizer].
|
524
|
+
expect(@klass.default_options[:query_string_normalizer]).to eq(normalizer)
|
525
|
+
end
|
526
|
+
end
|
527
|
+
|
528
|
+
describe ".raise_on" do
|
529
|
+
context 'when parameters is an array' do
|
530
|
+
it 'sets raise_on option' do
|
531
|
+
@klass.raise_on [500, 404]
|
532
|
+
expect(@klass.default_options[:raise_on]).to contain_exactly(404, 500)
|
533
|
+
end
|
534
|
+
end
|
535
|
+
|
536
|
+
context 'when parameters is a fixnum' do
|
537
|
+
it 'sets raise_on option' do
|
538
|
+
@klass.raise_on 404
|
539
|
+
expect(@klass.default_options[:raise_on]).to contain_exactly(404)
|
540
|
+
end
|
482
541
|
end
|
483
542
|
end
|
484
543
|
|
485
544
|
describe "with explicit override of automatic redirect handling" do
|
486
545
|
before do
|
487
|
-
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :
|
546
|
+
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', format: :xml, no_follow: true)
|
488
547
|
@redirect = stub_response 'first redirect', 302
|
489
548
|
@redirect['location'] = 'http://foo.com/bar'
|
490
|
-
HTTParty::Request.
|
549
|
+
allow(HTTParty::Request).to receive_messages(new: @request)
|
491
550
|
end
|
492
551
|
|
493
552
|
it "should fail with redirected GET" do
|
494
|
-
|
495
|
-
@error = @klass.get('/foo', :
|
496
|
-
end.
|
553
|
+
expect do
|
554
|
+
@error = @klass.get('/foo', no_follow: true)
|
555
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
497
556
|
end
|
498
557
|
|
499
558
|
it "should fail with redirected POST" do
|
500
|
-
|
501
|
-
@klass.post('/foo', :
|
502
|
-
end.
|
559
|
+
expect do
|
560
|
+
@klass.post('/foo', no_follow: true)
|
561
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
503
562
|
end
|
504
563
|
|
505
564
|
it "should fail with redirected PATCH" do
|
506
|
-
|
507
|
-
@klass.patch('/foo', :
|
508
|
-
end.
|
565
|
+
expect do
|
566
|
+
@klass.patch('/foo', no_follow: true)
|
567
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
509
568
|
end
|
510
569
|
|
511
570
|
it "should fail with redirected DELETE" do
|
512
|
-
|
513
|
-
@klass.delete('/foo', :
|
514
|
-
end.
|
571
|
+
expect do
|
572
|
+
@klass.delete('/foo', no_follow: true)
|
573
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
515
574
|
end
|
516
575
|
|
517
576
|
it "should fail with redirected MOVE" do
|
518
|
-
|
519
|
-
@klass.move('/foo', :
|
520
|
-
end.
|
577
|
+
expect do
|
578
|
+
@klass.move('/foo', no_follow: true)
|
579
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
521
580
|
end
|
522
581
|
|
523
582
|
it "should fail with redirected COPY" do
|
524
|
-
|
525
|
-
@klass.copy('/foo', :
|
526
|
-
end.
|
583
|
+
expect do
|
584
|
+
@klass.copy('/foo', no_follow: true)
|
585
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
527
586
|
end
|
528
587
|
|
529
588
|
it "should fail with redirected PUT" do
|
530
|
-
|
531
|
-
@klass.put('/foo', :
|
532
|
-
end.
|
589
|
+
expect do
|
590
|
+
@klass.put('/foo', no_follow: true)
|
591
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
533
592
|
end
|
534
593
|
|
535
594
|
it "should fail with redirected HEAD" do
|
536
|
-
|
537
|
-
@klass.head('/foo', :
|
538
|
-
end.
|
595
|
+
expect do
|
596
|
+
@klass.head('/foo', no_follow: true)
|
597
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
539
598
|
end
|
540
599
|
|
541
600
|
it "should fail with redirected OPTIONS" do
|
542
|
-
|
543
|
-
@klass.options('/foo', :
|
544
|
-
end.
|
601
|
+
expect do
|
602
|
+
@klass.options('/foo', no_follow: true)
|
603
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
604
|
+
end
|
605
|
+
|
606
|
+
it "should fail with redirected MKCOL" do
|
607
|
+
expect do
|
608
|
+
@klass.mkcol('/foo', no_follow: true)
|
609
|
+
end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
|
610
|
+
end
|
611
|
+
end
|
612
|
+
|
613
|
+
describe "head requests should follow redirects requesting HEAD only" do
|
614
|
+
before do
|
615
|
+
allow(HTTParty::Request).to receive(:new).
|
616
|
+
and_return(double("mock response", perform: nil))
|
617
|
+
end
|
618
|
+
|
619
|
+
it "should remain HEAD request across redirects, unless specified otherwise" do
|
620
|
+
expect(@klass).to receive(:ensure_method_maintained_across_redirects).with({})
|
621
|
+
@klass.head('/foo')
|
622
|
+
end
|
623
|
+
|
624
|
+
end
|
625
|
+
|
626
|
+
describe "#ensure_method_maintained_across_redirects" do
|
627
|
+
it "should set maintain_method_across_redirects option if unspecified" do
|
628
|
+
options = {}
|
629
|
+
@klass.send(:ensure_method_maintained_across_redirects, options)
|
630
|
+
expect(options[:maintain_method_across_redirects]).to be_truthy
|
631
|
+
end
|
632
|
+
|
633
|
+
it "should not set maintain_method_across_redirects option if value is present" do
|
634
|
+
options = { maintain_method_across_redirects: false }
|
635
|
+
@klass.send(:ensure_method_maintained_across_redirects, options)
|
636
|
+
expect(options[:maintain_method_across_redirects]).to be_falsey
|
545
637
|
end
|
546
638
|
end
|
547
639
|
|
@@ -549,20 +641,20 @@ describe HTTParty do
|
|
549
641
|
before(:each) do
|
550
642
|
@klass.instance_eval do
|
551
643
|
base_uri "http://first.com"
|
552
|
-
default_params :
|
644
|
+
default_params one: 1
|
553
645
|
end
|
554
646
|
|
555
647
|
@additional_klass = Class.new
|
556
648
|
@additional_klass.instance_eval do
|
557
649
|
include HTTParty
|
558
650
|
base_uri "http://second.com"
|
559
|
-
default_params :
|
651
|
+
default_params two: 2
|
560
652
|
end
|
561
653
|
end
|
562
654
|
|
563
655
|
it "should not run over each others options" do
|
564
|
-
@klass.default_options.
|
565
|
-
@additional_klass.default_options.
|
656
|
+
expect(@klass.default_options).to eq({ base_uri: 'http://first.com', default_params: { one: 1 } })
|
657
|
+
expect(@additional_klass.default_options).to eq({ base_uri: 'http://second.com', default_params: { two: 2 } })
|
566
658
|
end
|
567
659
|
end
|
568
660
|
|
@@ -580,71 +672,71 @@ describe HTTParty do
|
|
580
672
|
end
|
581
673
|
|
582
674
|
it "does not modify each others inherited attributes" do
|
583
|
-
@child1.default_params :
|
584
|
-
@child2.default_params :
|
675
|
+
@child1.default_params joe: "alive"
|
676
|
+
@child2.default_params joe: "dead"
|
585
677
|
|
586
|
-
@child1.default_options.
|
587
|
-
@child2.default_options.
|
678
|
+
expect(@child1.default_options).to eq({ default_params: {joe: "alive"} })
|
679
|
+
expect(@child2.default_options).to eq({ default_params: {joe: "dead"} })
|
588
680
|
|
589
|
-
@parent.default_options.
|
681
|
+
expect(@parent.default_options).to eq({ })
|
590
682
|
end
|
591
683
|
|
592
684
|
it "inherits default_options from the superclass" do
|
593
685
|
@parent.basic_auth 'user', 'password'
|
594
|
-
@child1.default_options.
|
686
|
+
expect(@child1.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
|
595
687
|
@child1.basic_auth 'u', 'p' # modifying child1 has no effect on child2
|
596
|
-
@child2.default_options.
|
688
|
+
expect(@child2.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
|
597
689
|
end
|
598
690
|
|
599
691
|
it "doesn't modify the parent's default options" do
|
600
692
|
@parent.basic_auth 'user', 'password'
|
601
693
|
|
602
694
|
@child1.basic_auth 'u', 'p'
|
603
|
-
@child1.default_options.
|
695
|
+
expect(@child1.default_options).to eq({basic_auth: {username: 'u', password: 'p'}})
|
604
696
|
|
605
697
|
@child1.basic_auth 'email', 'token'
|
606
|
-
@child1.default_options.
|
698
|
+
expect(@child1.default_options).to eq({basic_auth: {username: 'email', password: 'token'}})
|
607
699
|
|
608
|
-
@parent.default_options.
|
700
|
+
expect(@parent.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
|
609
701
|
end
|
610
702
|
|
611
703
|
it "doesn't modify hashes in the parent's default options" do
|
612
704
|
@parent.headers 'Accept' => 'application/json'
|
613
705
|
@child1.headers 'Accept' => 'application/xml'
|
614
706
|
|
615
|
-
@parent.default_options[:headers].
|
616
|
-
@child1.default_options[:headers].
|
707
|
+
expect(@parent.default_options[:headers]).to eq({'Accept' => 'application/json'})
|
708
|
+
expect(@child1.default_options[:headers]).to eq({'Accept' => 'application/xml'})
|
617
709
|
end
|
618
710
|
|
619
711
|
it "works with lambda values" do
|
620
712
|
@child1.default_options[:imaginary_option] = lambda { "This is a new lambda "}
|
621
|
-
@child1.default_options[:imaginary_option].
|
713
|
+
expect(@child1.default_options[:imaginary_option]).to be_a Proc
|
622
714
|
end
|
623
715
|
|
624
716
|
it 'should dup the proc on the child class' do
|
625
717
|
imaginary_option = lambda { 2 * 3.14 }
|
626
718
|
@parent.default_options[:imaginary_option] = imaginary_option
|
627
|
-
@parent.default_options[:imaginary_option].call.
|
719
|
+
expect(@parent.default_options[:imaginary_option].call).to eq(imaginary_option.call)
|
628
720
|
@child1.default_options[:imaginary_option]
|
629
|
-
@child1.default_options[:imaginary_option].call.
|
630
|
-
@child1.default_options[:imaginary_option].
|
721
|
+
expect(@child1.default_options[:imaginary_option].call).to eq(imaginary_option.call)
|
722
|
+
expect(@child1.default_options[:imaginary_option]).not_to be_equal imaginary_option
|
631
723
|
end
|
632
724
|
|
633
725
|
it "inherits default_cookies from the parent class" do
|
634
726
|
@parent.cookies 'type' => 'chocolate_chip'
|
635
|
-
@child1.default_cookies.
|
727
|
+
expect(@child1.default_cookies).to eq({"type" => "chocolate_chip"})
|
636
728
|
@child1.cookies 'type' => 'snickerdoodle'
|
637
|
-
@child1.default_cookies.
|
638
|
-
@child2.default_cookies.
|
729
|
+
expect(@child1.default_cookies).to eq({"type" => "snickerdoodle"})
|
730
|
+
expect(@child2.default_cookies).to eq({"type" => "chocolate_chip"})
|
639
731
|
end
|
640
732
|
|
641
733
|
it "doesn't modify the parent's default cookies" do
|
642
734
|
@parent.cookies 'type' => 'chocolate_chip'
|
643
735
|
|
644
736
|
@child1.cookies 'type' => 'snickerdoodle'
|
645
|
-
@child1.default_cookies.
|
737
|
+
expect(@child1.default_cookies).to eq({"type" => "snickerdoodle"})
|
646
738
|
|
647
|
-
@parent.default_cookies.
|
739
|
+
expect(@parent.default_cookies).to eq({"type" => "chocolate_chip"})
|
648
740
|
end
|
649
741
|
end
|
650
742
|
|
@@ -661,30 +753,44 @@ describe HTTParty do
|
|
661
753
|
end
|
662
754
|
it "continues running the #inherited on the parent" do
|
663
755
|
child = Class.new(@parent)
|
664
|
-
child.instance_variable_get(:@grand_parent).
|
756
|
+
expect(child.instance_variable_get(:@grand_parent)).to be_truthy
|
665
757
|
end
|
666
758
|
end
|
667
759
|
|
668
760
|
describe "#get" do
|
669
761
|
it "should be able to get html" do
|
670
762
|
stub_http_response_with('google.html')
|
671
|
-
HTTParty.get('http://www.google.com').
|
763
|
+
expect(HTTParty.get('http://www.google.com').parsed_response).to eq(file_fixture('google.html'))
|
672
764
|
end
|
673
765
|
|
674
766
|
it "should be able to get chunked html" do
|
675
|
-
chunks =
|
767
|
+
chunks = %w(Chunk1 Chunk2 Chunk3 Chunk4)
|
676
768
|
stub_chunked_http_response_with(chunks)
|
677
769
|
|
678
|
-
|
679
|
-
|
680
|
-
|
770
|
+
expect(
|
771
|
+
HTTParty.get('http://www.google.com') do |fragment|
|
772
|
+
expect(chunks).to include(fragment)
|
773
|
+
end.parsed_response
|
774
|
+
).to eq(chunks.join)
|
775
|
+
end
|
776
|
+
|
777
|
+
it "should return an empty body if stream_body option is turned on" do
|
778
|
+
chunks = %w(Chunk1 Chunk2 Chunk3 Chunk4)
|
779
|
+
options = {stream_body: true, format: 'html'}
|
780
|
+
stub_chunked_http_response_with(chunks, options)
|
781
|
+
|
782
|
+
expect(
|
783
|
+
HTTParty.get('http://www.google.com', options) do |fragment|
|
784
|
+
expect(chunks).to include(fragment)
|
785
|
+
end.parsed_response
|
786
|
+
).to eq(nil)
|
681
787
|
end
|
682
788
|
|
683
789
|
it "should be able parse response type json automatically" do
|
684
790
|
stub_http_response_with('twitter.json')
|
685
791
|
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
|
686
|
-
tweets.size.
|
687
|
-
tweets.first['user'].
|
792
|
+
expect(tweets.size).to eq(20)
|
793
|
+
expect(tweets.first['user']).to eq({
|
688
794
|
"name" => "Pyk",
|
689
795
|
"url" => nil,
|
690
796
|
"id" => "7694602",
|
@@ -694,14 +800,14 @@ describe HTTParty do
|
|
694
800
|
"followers_count" => 1,
|
695
801
|
"location" => "Opera Plaza, California",
|
696
802
|
"profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
|
697
|
-
}
|
803
|
+
})
|
698
804
|
end
|
699
805
|
|
700
806
|
it "should be able parse response type xml automatically" do
|
701
807
|
stub_http_response_with('twitter.xml')
|
702
808
|
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.xml')
|
703
|
-
tweets['statuses'].size.
|
704
|
-
tweets['statuses'].first['user'].
|
809
|
+
expect(tweets['statuses'].size).to eq(20)
|
810
|
+
expect(tweets['statuses'].first['user']).to eq({
|
705
811
|
"name" => "Magic 8 Bot",
|
706
812
|
"url" => nil,
|
707
813
|
"id" => "17656026",
|
@@ -711,54 +817,56 @@ describe HTTParty do
|
|
711
817
|
"followers_count" => "90",
|
712
818
|
"profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
|
713
819
|
"location" => nil
|
714
|
-
}
|
820
|
+
})
|
715
821
|
end
|
716
822
|
|
717
823
|
it "should be able parse response type csv automatically" do
|
718
824
|
stub_http_response_with('twitter.csv')
|
719
825
|
profile = HTTParty.get('http://twitter.com/statuses/profile.csv')
|
720
|
-
profile.size.
|
721
|
-
profile[0].
|
722
|
-
profile[1].
|
826
|
+
expect(profile.size).to eq(2)
|
827
|
+
expect(profile[0]).to eq(%w(name url id description protected screen_name followers_count profile_image_url location))
|
828
|
+
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])
|
723
829
|
end
|
724
830
|
|
725
831
|
it "should not get undefined method add_node for nil class for the following xml" do
|
726
832
|
stub_http_response_with('undefined_method_add_node_for_nil.xml')
|
727
833
|
result = HTTParty.get('http://foobar.com')
|
728
|
-
result.
|
834
|
+
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"}})
|
729
835
|
end
|
730
836
|
|
731
837
|
it "should parse empty response fine" do
|
732
838
|
stub_http_response_with('empty.xml')
|
733
839
|
result = HTTParty.get('http://foobar.com')
|
734
|
-
result.
|
840
|
+
expect(result).to be_nil
|
735
841
|
end
|
736
842
|
|
737
843
|
it "should accept http URIs" do
|
738
844
|
stub_http_response_with('google.html')
|
739
|
-
|
845
|
+
expect do
|
740
846
|
HTTParty.get('http://google.com')
|
741
|
-
end.
|
847
|
+
end.not_to raise_error
|
742
848
|
end
|
743
849
|
|
744
850
|
it "should accept https URIs" do
|
745
851
|
stub_http_response_with('google.html')
|
746
|
-
|
852
|
+
expect do
|
747
853
|
HTTParty.get('https://google.com')
|
748
|
-
end.
|
854
|
+
end.not_to raise_error
|
749
855
|
end
|
750
856
|
|
751
857
|
it "should accept webcal URIs" do
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
858
|
+
uri = 'http://google.com/'
|
859
|
+
FakeWeb.register_uri(:get, uri, body: 'stuff')
|
860
|
+
uri = 'webcal://google.com/'
|
861
|
+
expect do
|
862
|
+
HTTParty.get(uri)
|
863
|
+
end.not_to raise_error
|
756
864
|
end
|
757
865
|
|
758
866
|
it "should raise an InvalidURIError on URIs that can't be parsed at all" do
|
759
|
-
|
867
|
+
expect do
|
760
868
|
HTTParty.get("It's the one that says 'Bad URI'")
|
761
|
-
end.
|
869
|
+
end.to raise_error(URI::InvalidURIError)
|
762
870
|
end
|
763
871
|
end
|
764
872
|
end
|