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