alexvollmer-httparty 0.2.6 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History +34 -0
- data/Manifest +25 -2
- data/README +2 -2
- data/Rakefile +6 -2
- data/bin/httparty +39 -44
- data/cucumber.yml +1 -0
- data/examples/basic.rb +6 -1
- data/examples/delicious.rb +1 -0
- data/examples/rubyurl.rb +1 -1
- data/features/basic_authentication.feature +20 -0
- data/features/command_line.feature +7 -0
- data/features/deals_with_http_error_codes.feature +26 -0
- data/features/handles_multiple_formats.feature +34 -0
- data/features/steps/env.rb +15 -0
- data/features/steps/httparty_response_steps.rb +26 -0
- data/features/steps/httparty_steps.rb +15 -0
- data/features/steps/mongrel_helper.rb +55 -0
- data/features/steps/remote_service_steps.rb +47 -0
- data/features/supports_redirection.feature +22 -0
- data/httparty.gemspec +4 -7
- data/lib/core_extensions.rb +48 -222
- data/lib/httparty/cookie_hash.rb +9 -0
- data/lib/httparty/exceptions.rb +3 -0
- data/lib/httparty/module_inheritable_attributes.rb +25 -0
- data/lib/httparty/parsers/json.rb +74 -0
- data/lib/httparty/parsers/xml.rb +209 -0
- data/lib/httparty/parsers.rb +4 -0
- data/lib/httparty/request.rb +63 -76
- data/lib/httparty/response.rb +17 -0
- data/lib/httparty/version.rb +2 -2
- data/lib/httparty.rb +108 -19
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
- data/spec/hash_spec.rb +49 -0
- data/spec/httparty/cookie_hash_spec.rb +38 -0
- data/spec/httparty/parsers/json_spec.rb +42 -0
- data/spec/httparty/parsers/xml_spec.rb +445 -0
- data/spec/httparty/request_spec.rb +219 -80
- data/spec/httparty/response_spec.rb +53 -0
- data/spec/httparty_spec.rb +125 -64
- data/spec/spec_helper.rb +5 -8
- data/spec/string_spec.rb +27 -0
- metadata +34 -14
- data/lib/module_level_inheritable_attributes.rb +0 -25
- data/spec/as_buggery_spec.rb +0 -16
@@ -1,6 +1,20 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
2
|
|
3
3
|
describe HTTParty::Request do
|
4
|
+
def stub_response(body, code = 200)
|
5
|
+
unless @http
|
6
|
+
@http = Net::HTTP.new('localhost', 80)
|
7
|
+
@request.stub!(:http).and_return(@http)
|
8
|
+
@request.stub!(:uri).and_return(URI.parse("http://foo.com/foobar"))
|
9
|
+
end
|
10
|
+
|
11
|
+
response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body)
|
12
|
+
response.stub!(:body).and_return(body)
|
13
|
+
|
14
|
+
@http.stub!(:request).and_return(response)
|
15
|
+
response
|
16
|
+
end
|
17
|
+
|
4
18
|
before do
|
5
19
|
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :format => :xml)
|
6
20
|
end
|
@@ -13,14 +27,98 @@ describe HTTParty::Request do
|
|
13
27
|
|
14
28
|
describe 'http' do
|
15
29
|
it "should use ssl for port 443" do
|
16
|
-
|
30
|
+
request = HTTParty::Request.new(Net::HTTP::Get, 'https://api.foo.com/v1:443')
|
31
|
+
request.send(:http).use_ssl?.should == true
|
17
32
|
end
|
18
33
|
|
19
34
|
it 'should not use ssl for port 80' do
|
20
|
-
|
35
|
+
request = HTTParty::Request.new(Net::HTTP::Get, 'http://foobar.com')
|
36
|
+
@request.send(:http).use_ssl?.should == false
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should use basic auth when configured" do
|
40
|
+
@request.options[:basic_auth] = {:username => 'foobar', :password => 'secret'}
|
41
|
+
@request.send(:setup_raw_request)
|
42
|
+
@request.instance_variable_get(:@raw_request)['authorization'].should_not be_nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#format_from_mimetype' do
|
47
|
+
it 'should handle text/xml' do
|
48
|
+
["text/xml", "text/xml; charset=iso8859-1"].each do |ct|
|
49
|
+
@request.send(:format_from_mimetype, ct).should == :xml
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should handle application/xml' do
|
54
|
+
["application/xml", "application/xml; charset=iso8859-1"].each do |ct|
|
55
|
+
@request.send(:format_from_mimetype, ct).should == :xml
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should handle text/json' do
|
60
|
+
["text/json", "text/json; charset=iso8859-1"].each do |ct|
|
61
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should handle application/json' do
|
66
|
+
["application/json", "application/json; charset=iso8859-1"].each do |ct|
|
67
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should handle text/javascript' do
|
72
|
+
["text/javascript", "text/javascript; charset=iso8859-1"].each do |ct|
|
73
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should handle application/javascript' do
|
78
|
+
["application/javascript", "application/javascript; charset=iso8859-1"].each do |ct|
|
79
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
80
|
+
end
|
21
81
|
end
|
22
82
|
end
|
23
83
|
|
84
|
+
describe '#format_from_mimetype' do
|
85
|
+
it 'should handle text/xml' do
|
86
|
+
["text/xml", "text/xml; charset=iso8859-1"].each do |ct|
|
87
|
+
@request.send(:format_from_mimetype, ct).should == :xml
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should handle application/xml' do
|
92
|
+
["application/xml", "application/xml; charset=iso8859-1"].each do |ct|
|
93
|
+
@request.send(:format_from_mimetype, ct).should == :xml
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should handle text/json' do
|
98
|
+
["text/json", "text/json; charset=iso8859-1"].each do |ct|
|
99
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should handle application/json' do
|
104
|
+
["application/json", "application/json; charset=iso8859-1"].each do |ct|
|
105
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should handle text/javascript' do
|
110
|
+
["text/javascript", "text/javascript; charset=iso8859-1"].each do |ct|
|
111
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should handle application/javascript' do
|
116
|
+
["application/javascript", "application/javascript; charset=iso8859-1"].each do |ct|
|
117
|
+
@request.send(:format_from_mimetype, ct).should == :json
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
24
122
|
describe 'parsing responses' do
|
25
123
|
it 'should handle xml automatically' do
|
26
124
|
xml = %q[<books><book><id>1234</id><name>Foo Bar!</name></book></books>]
|
@@ -33,112 +131,153 @@ describe HTTParty::Request do
|
|
33
131
|
@request.options[:format] = :json
|
34
132
|
@request.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
35
133
|
end
|
134
|
+
|
135
|
+
it 'should handle yaml automatically' do
|
136
|
+
yaml = "books: \n book: \n name: Foo Bar!\n id: \"1234\"\n"
|
137
|
+
@request.options[:format] = :yaml
|
138
|
+
@request.send(:parse_response, yaml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should include any HTTP headers in the returned response" do
|
142
|
+
@request.options[:format] = :html
|
143
|
+
response = stub_response "Content"
|
144
|
+
response.initialize_http_header("key" => "value")
|
145
|
+
|
146
|
+
@request.perform.headers.should == { "key" => ["value"] }
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'with non-200 responses' do
|
150
|
+
|
151
|
+
it 'should return a valid object for 4xx response' do
|
152
|
+
stub_response '<foo><bar>yes</bar></foo>', 401
|
153
|
+
resp = @request.perform
|
154
|
+
resp.code.should == 401
|
155
|
+
resp.body.should == "<foo><bar>yes</bar></foo>"
|
156
|
+
resp['foo']['bar'].should == "yes"
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should return a valid object for 5xx response' do
|
160
|
+
stub_response '<foo><bar>error</bar></foo>', 500
|
161
|
+
resp = @request.perform
|
162
|
+
resp.code.should == 500
|
163
|
+
resp.body.should == "<foo><bar>error</bar></foo>"
|
164
|
+
resp['foo']['bar'].should == "error"
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
36
168
|
end
|
37
169
|
|
38
170
|
it "should not attempt to parse empty responses" do
|
39
|
-
|
40
|
-
@request.stub!(:http).and_return(http)
|
41
|
-
response = Net::HTTPNoContent.new("1.1", 204, "No content for you")
|
42
|
-
response.stub!(:body).and_return(nil)
|
43
|
-
http.stub!(:request).and_return(response)
|
171
|
+
stub_response "", 204
|
44
172
|
|
45
173
|
@request.options[:format] = :xml
|
46
174
|
@request.perform.should be_nil
|
47
|
-
|
48
|
-
response.stub!(:body).and_return("")
|
49
|
-
@request.perform.should be_nil
|
50
175
|
end
|
51
176
|
|
52
177
|
it "should not fail for missing mime type" do
|
53
|
-
|
54
|
-
@request.stub!(:http).and_return(http)
|
55
|
-
|
56
|
-
response = Net::HTTPOK.new("1.1", 200, "Content for you")
|
57
|
-
response.stub!(:[]).with('content-type').and_return(nil)
|
58
|
-
response.stub!(:body).and_return('Content for you')
|
59
|
-
|
60
|
-
http.stub!(:request).and_return(response)
|
61
|
-
|
178
|
+
stub_response "Content for you"
|
62
179
|
@request.options[:format] = :html
|
63
|
-
@request.perform.should == 'Content for you'
|
180
|
+
@request.perform.body.should == 'Content for you'
|
64
181
|
end
|
65
182
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
@request.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}, '_body' => xml}
|
72
|
-
end
|
183
|
+
it "should drop query parameters when following redirects" do
|
184
|
+
request = HTTParty::Request.new(Net::HTTP::Post,
|
185
|
+
'http://api.foo.com/foo',
|
186
|
+
:query => { :bar => 1 },
|
187
|
+
:format => :xml)
|
73
188
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
189
|
+
http = Net::HTTP.new('localhost', 80)
|
190
|
+
request.stub!(:http).and_return(http)
|
191
|
+
req1 = Net::HTTP::Post.new('/foo')
|
192
|
+
req1.should_receive(:set_form_data).once.with({ :bar => 1 })
|
193
|
+
req1.should_receive(:initialize_http_header).once.with(nil)
|
194
|
+
Net::HTTP::Post.should_receive(:new).once.with("/foo").and_return(req1)
|
195
|
+
redirect = stub_response('', 302)
|
196
|
+
redirect['location'] = 'http://api.foo.com/foo/1'
|
197
|
+
http.should_receive(:request).
|
198
|
+
once.
|
199
|
+
with(an_instance_of(Net::HTTP::Post)).
|
200
|
+
and_return(redirect)
|
79
201
|
|
80
|
-
|
202
|
+
req2 = Net::HTTP::Get.new('/foo/1')
|
203
|
+
Net::HTTP::Get.should_receive(:new).once.with("/foo/1").and_return(req2)
|
204
|
+
ok = stub_response('<hash><foo>bar</foo></hash>', 200)
|
205
|
+
http.should_receive(:request).
|
206
|
+
once.
|
207
|
+
with(an_instance_of(Net::HTTP::Get)).
|
208
|
+
and_return(ok)
|
209
|
+
|
210
|
+
request.perform.should == { "hash" => { "foo" => "bar" }}
|
81
211
|
end
|
82
212
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
@redirect['location'] = '/foo'
|
90
|
-
end
|
213
|
+
it "should not follow redirects after POST if no_follow is set" do
|
214
|
+
request = HTTParty::Request.new(Net::HTTP::Post,
|
215
|
+
'http://api.foo.com/foo',
|
216
|
+
:query => { :bar => 1 },
|
217
|
+
:no_follow => true,
|
218
|
+
:format => :xml)
|
91
219
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
220
|
+
http = Net::HTTP.new('localhost', 80)
|
221
|
+
request.stub!(:http).and_return(http)
|
222
|
+
req1 = Net::HTTP::Post.new('/foo')
|
223
|
+
req1.should_receive(:set_form_data).once.with({ :bar => 1 })
|
224
|
+
req1.should_receive(:initialize_http_header).once.with(nil)
|
225
|
+
Net::HTTP::Post.should_receive(:new).once.with("/foo").and_return(req1)
|
226
|
+
redirect = stub_response('', 302)
|
227
|
+
redirect['location'] = 'http://api.foo.com/foo/1'
|
228
|
+
http.should_receive(:request).
|
229
|
+
once.
|
230
|
+
with(an_instance_of(Net::HTTP::Post)).
|
231
|
+
and_return(redirect)
|
98
232
|
|
99
|
-
|
100
|
-
@http.stub!(:request).and_return(@redirect)
|
101
|
-
end
|
233
|
+
Net::HTTP::Get.should_not_receive(:new).with("/foo/1")
|
102
234
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
235
|
+
lambda {
|
236
|
+
request.perform.should be_empty
|
237
|
+
}.should raise_error(HTTParty::RedirectionTooDeep)
|
238
|
+
end
|
107
239
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
240
|
+
describe "a request that redirects" do
|
241
|
+
before(:each) do
|
242
|
+
@redirect = stub_response("", 302)
|
243
|
+
@redirect['location'] = '/foo'
|
112
244
|
|
113
|
-
|
114
|
-
setup_successful_redirect
|
115
|
-
@request.perform.should == {"hash" => {"foo" => "bar"}}
|
245
|
+
@ok = stub_response('<hash><foo>bar</foo></hash>', 200)
|
116
246
|
end
|
117
247
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
248
|
+
describe "once" do
|
249
|
+
before(:each) do
|
250
|
+
@http.stub!(:request).and_return(@redirect, @ok)
|
251
|
+
end
|
123
252
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
253
|
+
it "should be handled by GET transparently" do
|
254
|
+
@request.perform.should == {"hash" => {"foo" => "bar"}}
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should be handled by POST transparently" do
|
258
|
+
@request.http_method = Net::HTTP::Post
|
259
|
+
@request.perform.should == {"hash" => {"foo" => "bar"}}
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should be handled by DELETE transparently" do
|
263
|
+
@request.http_method = Net::HTTP::Delete
|
264
|
+
@request.perform.should == {"hash" => {"foo" => "bar"}}
|
265
|
+
end
|
129
266
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
267
|
+
it "should be handled by PUT transparently" do
|
268
|
+
@request.http_method = Net::HTTP::Put
|
269
|
+
@request.perform.should == {"hash" => {"foo" => "bar"}}
|
270
|
+
end
|
134
271
|
end
|
135
272
|
|
136
|
-
|
137
|
-
|
273
|
+
describe "infinitely" do
|
274
|
+
before(:each) do
|
275
|
+
@http.stub!(:request).and_return(@redirect)
|
276
|
+
end
|
138
277
|
|
139
|
-
|
140
|
-
@request.perform
|
141
|
-
end
|
278
|
+
it "should raise an exception" do
|
279
|
+
lambda { @request.perform }.should raise_error(HTTParty::RedirectionTooDeep)
|
280
|
+
end
|
142
281
|
end
|
143
282
|
end
|
144
283
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe HTTParty::Response do
|
4
|
+
describe "initialization" do
|
5
|
+
before do
|
6
|
+
@response_object = {'foo' => 'bar'}
|
7
|
+
@body = "{foo:'bar'}"
|
8
|
+
@code = 200
|
9
|
+
@response = HTTParty::Response.new(@response_object, @body, @code)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should set delegate" do
|
13
|
+
@response.delegate.should == @response_object
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should set body" do
|
17
|
+
@response.body.should == @body
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should set code" do
|
21
|
+
@response.code.should == @code
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be able to set headers during initialization" do
|
26
|
+
response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, {'foo' => 'bar'})
|
27
|
+
response.headers.should == {'foo' => 'bar'}
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should send missing methods to delegate" do
|
31
|
+
response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200)
|
32
|
+
response['foo'].should == 'bar'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be able to iterate delegate if it is array" do
|
36
|
+
response = HTTParty::Response.new([{'foo' => 'bar'}, {'foo' => 'baz'}], "[{foo:'bar'}, {foo:'baz'}]", 200)
|
37
|
+
response.size.should == 2
|
38
|
+
lambda {
|
39
|
+
response.each { |item| }
|
40
|
+
}.should_not raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
xit "should allow hashes to be accessed with dot notation" do
|
44
|
+
response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200)
|
45
|
+
response.foo.should == 'bar'
|
46
|
+
end
|
47
|
+
|
48
|
+
xit "should allow nested hashes to be accessed with dot notation" do
|
49
|
+
response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200)
|
50
|
+
response.foo.should == {'bar' => 'baz'}
|
51
|
+
response.foo.bar.should == 'baz'
|
52
|
+
end
|
53
|
+
end
|
data/spec/httparty_spec.rb
CHANGED
@@ -1,36 +1,25 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
2
|
|
3
|
-
class Foo
|
4
|
-
include HTTParty
|
5
|
-
base_uri 'api.foo.com/v1'
|
6
|
-
end
|
7
|
-
|
8
|
-
class GRest
|
9
|
-
include HTTParty
|
10
|
-
base_uri "grest.com"
|
11
|
-
default_params :one => 'two'
|
12
|
-
end
|
13
|
-
|
14
|
-
class HRest
|
15
|
-
include HTTParty
|
16
|
-
base_uri "hrest.com"
|
17
|
-
default_params :two => 'three'
|
18
|
-
end
|
19
|
-
|
20
3
|
describe HTTParty do
|
21
|
-
|
4
|
+
before(:each) do
|
5
|
+
@klass = Class.new do
|
6
|
+
include HTTParty
|
7
|
+
base_uri "http://api.foo.com"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
22
11
|
describe "base uri" do
|
23
|
-
before do
|
24
|
-
|
12
|
+
before(:each) do
|
13
|
+
@klass.base_uri('api.foo.com/v1')
|
25
14
|
end
|
26
15
|
|
27
16
|
it "should have reader" do
|
28
|
-
|
17
|
+
@klass.base_uri.should == 'http://api.foo.com/v1'
|
29
18
|
end
|
30
19
|
|
31
20
|
it 'should have writer' do
|
32
|
-
|
33
|
-
|
21
|
+
@klass.base_uri('http://api.foobar.com')
|
22
|
+
@klass.base_uri.should == 'http://api.foobar.com'
|
34
23
|
end
|
35
24
|
end
|
36
25
|
|
@@ -53,91 +42,149 @@ describe HTTParty do
|
|
53
42
|
|
54
43
|
describe "headers" do
|
55
44
|
it "should default to empty hash" do
|
56
|
-
|
45
|
+
@klass.headers.should == {}
|
57
46
|
end
|
58
47
|
|
59
48
|
it "should be able to be updated" do
|
60
49
|
init_headers = {:foo => 'bar', :baz => 'spax'}
|
61
|
-
|
62
|
-
|
50
|
+
@klass.headers init_headers
|
51
|
+
@klass.headers.should == init_headers
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "cookies" do
|
56
|
+
def expect_cookie_header(s)
|
57
|
+
HTTParty::Request.should_receive(:new) \
|
58
|
+
.with(anything, anything, hash_including({ :headers => { "cookie" => s } })) \
|
59
|
+
.and_return(mock("mock response", :perform => nil))
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not be in the headers by default" do
|
63
|
+
HTTParty::Request.stub!(:new).and_return(stub(nil, :perform => nil))
|
64
|
+
@klass.get("")
|
65
|
+
@klass.headers.keys.should_not include("cookie")
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should raise an ArgumentError if passed a non-Hash" do
|
69
|
+
lambda do
|
70
|
+
@klass.cookies("nonsense")
|
71
|
+
end.should raise_error(ArgumentError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should allow a cookie to be specified with a one-off request" do
|
75
|
+
expect_cookie_header "type=snickerdoodle"
|
76
|
+
@klass.get("", :cookies => { :type => "snickerdoodle" })
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "when a cookie is set at the class level" do
|
80
|
+
before(:each) do
|
81
|
+
@klass.cookies({ :type => "snickerdoodle" })
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should include that cookie in the request" do
|
85
|
+
expect_cookie_header "type=snickerdoodle"
|
86
|
+
@klass.get("")
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should allow the class defaults to be overridden" do
|
90
|
+
expect_cookie_header "type=chocolate_chip"
|
91
|
+
|
92
|
+
@klass.get("", :cookies => { :type => "chocolate_chip" })
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "in a class with multiple methods that use different cookies" do
|
97
|
+
before(:each) do
|
98
|
+
@klass.instance_eval do
|
99
|
+
def first_method
|
100
|
+
get("first_method", :cookies => { :first_method_cookie => "foo" })
|
101
|
+
end
|
102
|
+
|
103
|
+
def second_method
|
104
|
+
get("second_method", :cookies => { :second_method_cookie => "foo" })
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should not allow cookies used in one method to carry over into other methods" do
|
110
|
+
expect_cookie_header "first_method_cookie=foo"
|
111
|
+
@klass.first_method
|
112
|
+
|
113
|
+
expect_cookie_header "second_method_cookie=foo"
|
114
|
+
@klass.second_method
|
115
|
+
end
|
63
116
|
end
|
64
117
|
end
|
65
118
|
|
66
119
|
describe "default params" do
|
67
120
|
it "should default to empty hash" do
|
68
|
-
|
121
|
+
@klass.default_params.should == {}
|
69
122
|
end
|
70
123
|
|
71
124
|
it "should be able to be updated" do
|
72
125
|
new_defaults = {:foo => 'bar', :baz => 'spax'}
|
73
|
-
|
74
|
-
|
126
|
+
@klass.default_params new_defaults
|
127
|
+
@klass.default_params.should == new_defaults
|
75
128
|
end
|
76
129
|
end
|
77
130
|
|
78
131
|
describe "basic http authentication" do
|
79
132
|
it "should work" do
|
80
|
-
|
81
|
-
|
133
|
+
@klass.basic_auth 'foobar', 'secret'
|
134
|
+
@klass.default_options[:basic_auth].should == {:username => 'foobar', :password => 'secret'}
|
82
135
|
end
|
83
136
|
end
|
84
137
|
|
85
138
|
describe "format" do
|
86
139
|
it "should allow xml" do
|
87
|
-
|
88
|
-
|
140
|
+
@klass.format :xml
|
141
|
+
@klass.default_options[:format].should == :xml
|
89
142
|
end
|
90
143
|
|
91
144
|
it "should allow json" do
|
92
|
-
|
93
|
-
|
145
|
+
@klass.format :json
|
146
|
+
@klass.default_options[:format].should == :json
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should allow yaml" do
|
150
|
+
@klass.format :yaml
|
151
|
+
@klass.default_options[:format].should == :yaml
|
94
152
|
end
|
95
153
|
|
96
154
|
it 'should not allow funky format' do
|
97
155
|
lambda do
|
98
|
-
|
156
|
+
@klass.format :foobar
|
99
157
|
end.should raise_error(HTTParty::UnsupportedFormat)
|
100
158
|
end
|
101
159
|
end
|
102
160
|
|
103
|
-
describe "with
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
it "should fail with redirected POST" do
|
112
|
-
lambda do
|
113
|
-
Foo.post('/foo', :no_follow => true)
|
114
|
-
end.should raise_error(HTTParty::RedirectionTooDeep)
|
115
|
-
end
|
161
|
+
describe "with multiple class definitions" do
|
162
|
+
before(:each) do
|
163
|
+
@klass.instance_eval do
|
164
|
+
base_uri "http://first.com"
|
165
|
+
default_params :one => 1
|
166
|
+
end
|
116
167
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
168
|
+
@additional_klass = Class.new
|
169
|
+
@additional_klass.instance_eval do
|
170
|
+
include HTTParty
|
171
|
+
base_uri "http://second.com"
|
172
|
+
default_params :two => 2
|
173
|
+
end
|
121
174
|
end
|
122
175
|
|
123
|
-
it "should fail with redirected PUT" do
|
124
|
-
lambda do
|
125
|
-
Foo.put('/foo', :no_follow => true)
|
126
|
-
end.should raise_error(HTTParty::RedirectionTooDeep)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
describe "with multiple class definitions" do
|
131
176
|
it "should not run over each others options" do
|
132
|
-
|
133
|
-
|
177
|
+
@klass.default_options.should == { :base_uri => 'http://first.com', :default_params => { :one => 1 } }
|
178
|
+
@additional_klass.default_options.should == { :base_uri => 'http://second.com', :default_params => { :two => 2 } }
|
134
179
|
end
|
135
180
|
end
|
136
181
|
|
137
182
|
describe "#get" do
|
138
183
|
it "should be able to get html" do
|
139
184
|
stub_http_response_with('google.html')
|
140
|
-
HTTParty.get('http://www.google.com')
|
185
|
+
resp = HTTParty.get('http://www.google.com')
|
186
|
+
resp.should == file_fixture('google.html')
|
187
|
+
resp.body.should == file_fixture('google.html')
|
141
188
|
end
|
142
189
|
|
143
190
|
it "should be able parse response type json automatically" do
|
@@ -155,6 +202,7 @@ describe HTTParty do
|
|
155
202
|
"location" => "Opera Plaza, California",
|
156
203
|
"profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
|
157
204
|
}
|
205
|
+
tweets.body.should == file_fixture('twitter.json')
|
158
206
|
end
|
159
207
|
|
160
208
|
it "should be able parse response type xml automatically" do
|
@@ -172,6 +220,19 @@ describe HTTParty do
|
|
172
220
|
"profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
|
173
221
|
"location" => nil
|
174
222
|
}
|
223
|
+
tweets.body.should == file_fixture('twitter.xml')
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should not get undefined method add_node for nil class for the following xml" do
|
227
|
+
stub_http_response_with('undefined_method_add_node_for_nil.xml')
|
228
|
+
result = HTTParty.get('http://foobar.com')
|
229
|
+
result.should == {"Entities"=>{"href"=>"https://s3-sandbox.parature.com/api/v1/5578/5633/Account", "results"=>"0", "total"=>"0", "page_size"=>"25", "page"=>"1"}}
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should parse empty response fine" do
|
233
|
+
stub_http_response_with('empty.xml')
|
234
|
+
result = HTTParty.get('http://foobar.com')
|
235
|
+
result.should == nil
|
175
236
|
end
|
176
237
|
end
|
177
238
|
end
|