weary 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +95 -13
- data/Rakefile +5 -29
- data/VERSION +1 -1
- data/examples/batch.rb +20 -0
- data/examples/repo.rb +7 -11
- data/examples/status.rb +10 -2
- data/lib/weary.rb +40 -7
- data/lib/weary/base.rb +1 -2
- data/lib/weary/batch.rb +37 -0
- data/lib/weary/exceptions.rb +1 -1
- data/lib/weary/httpverb.rb +2 -2
- data/lib/weary/request.rb +116 -33
- data/lib/weary/resource.rb +1 -1
- data/lib/weary/response.rb +19 -23
- data/spec/spec_helper.rb +7 -18
- data/spec/weary/base_spec.rb +10 -11
- data/spec/weary/batch_spec.rb +71 -0
- data/spec/weary/httpverb_spec.rb +5 -0
- data/spec/weary/request_spec.rb +326 -33
- data/spec/weary/resource_spec.rb +2 -2
- data/spec/weary/response_spec.rb +137 -41
- data/spec/weary_spec.rb +54 -0
- data/weary.gemspec +14 -3
- metadata +28 -3
data/lib/weary/resource.rb
CHANGED
@@ -79,7 +79,7 @@ module Weary
|
|
79
79
|
parameters = setup_parameters(params, defaults)
|
80
80
|
request_opts = setup_options(parameters, credentials)
|
81
81
|
uri.query = request_opts[:query].to_params if request_opts[:query]
|
82
|
-
|
82
|
+
Request.new(uri.normalize.to_s, @via, request_opts)
|
83
83
|
end
|
84
84
|
|
85
85
|
# Setup the parameters to make the Request with
|
data/lib/weary/response.rb
CHANGED
@@ -1,35 +1,33 @@
|
|
1
1
|
module Weary
|
2
2
|
class Response
|
3
3
|
|
4
|
-
attr_reader :raw, :
|
5
|
-
alias mime_type content_type
|
4
|
+
attr_reader :raw, :requester, :code, :message, :header, :content_type, :cookie, :body, :url
|
6
5
|
|
7
|
-
def initialize(http_response,
|
8
|
-
raise ArgumentError, "Must be a Net::HTTPResponse" unless http_response.is_a?(Net::HTTPResponse)
|
6
|
+
def initialize(http_response, requester)
|
9
7
|
@raw = http_response
|
10
|
-
@
|
8
|
+
@requester = requester
|
9
|
+
@url = requester.uri
|
11
10
|
@code = http_response.code.to_i
|
12
11
|
@message = http_response.message
|
13
12
|
@header = http_response.to_hash
|
14
13
|
@content_type = http_response.content_type
|
15
14
|
@cookie = http_response['Set-Cookie']
|
16
15
|
@body = http_response.body
|
17
|
-
self.format = http_response.content_type
|
18
16
|
end
|
19
17
|
|
20
18
|
# Is this an HTTP redirect?
|
21
19
|
def redirected?
|
22
|
-
|
20
|
+
raw.is_a?(Net::HTTPRedirection)
|
23
21
|
end
|
24
22
|
|
25
23
|
# Was this Request successful?
|
26
24
|
def success?
|
27
|
-
(200..299).include?(
|
25
|
+
(200..299).include?(code)
|
28
26
|
end
|
29
27
|
|
30
28
|
# Returns a symbol corresponding to the Response's Content Type
|
31
|
-
def format
|
32
|
-
@format
|
29
|
+
def format
|
30
|
+
@format ||= case content_type
|
33
31
|
when *ContentTypes[:json]
|
34
32
|
:json
|
35
33
|
when *ContentTypes[:xml]
|
@@ -38,35 +36,33 @@ module Weary
|
|
38
36
|
:html
|
39
37
|
when *ContentTypes[:yaml]
|
40
38
|
:yaml
|
41
|
-
when *ContentTypes[:plain]
|
42
|
-
:plain
|
43
39
|
else
|
44
|
-
|
40
|
+
:plain
|
45
41
|
end
|
46
42
|
end
|
47
43
|
|
48
44
|
# Follow the Redirect
|
49
45
|
def follow_redirect
|
50
46
|
if redirected?
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
47
|
+
new_request = requester.dup
|
48
|
+
new_request.uri = @raw['location']
|
49
|
+
new_request.perform
|
50
|
+
end
|
55
51
|
end
|
56
52
|
|
57
53
|
# Parse the body with Crack parsers (if XML/HTML) or Yaml parser
|
58
54
|
def parse
|
59
|
-
raise StandardError, "The Response has no body. #{
|
55
|
+
raise StandardError, "The Response has no body. #{requester.via.to_s.upcase} request sent." unless body
|
60
56
|
handle_errors
|
61
|
-
case
|
57
|
+
case format
|
62
58
|
when :xml, :html
|
63
|
-
Crack::XML.parse
|
59
|
+
Crack::XML.parse body
|
64
60
|
when :json
|
65
|
-
Crack::JSON.parse
|
61
|
+
Crack::JSON.parse body
|
66
62
|
when :yaml
|
67
|
-
YAML::load
|
63
|
+
YAML::load body
|
68
64
|
else
|
69
|
-
|
65
|
+
body
|
70
66
|
end
|
71
67
|
end
|
72
68
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
|
3
3
|
require 'rubygems'
|
4
|
-
require 'spec'
|
5
4
|
require 'weary'
|
5
|
+
require 'spec'
|
6
|
+
require 'fakeweb'
|
6
7
|
|
7
8
|
def get_fixture(filename)
|
8
9
|
open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read
|
9
10
|
end
|
10
11
|
|
11
|
-
def
|
12
|
-
|
13
|
-
message = case code
|
12
|
+
def http_status_message(code)
|
13
|
+
message = case code.to_i
|
14
14
|
when 200
|
15
15
|
"OK"
|
16
16
|
when 301
|
@@ -29,6 +29,8 @@ def mock_response(request_method = :get, code=200, header={}, body=nil)
|
|
29
29
|
"Method Not Allowed"
|
30
30
|
when 409
|
31
31
|
"Conflict"
|
32
|
+
when 418
|
33
|
+
"I'm a teapot"
|
32
34
|
when 422
|
33
35
|
"Unprocessable Entity"
|
34
36
|
when 401...500
|
@@ -38,18 +40,5 @@ def mock_response(request_method = :get, code=200, header={}, body=nil)
|
|
38
40
|
else
|
39
41
|
"Unknown"
|
40
42
|
end
|
41
|
-
|
42
|
-
response.initialize_http_header(header)
|
43
|
-
response.stub!(:body).and_return(body)
|
44
|
-
response.stub!(:message).and_return(message)
|
45
|
-
|
46
|
-
response
|
47
|
-
end
|
48
|
-
|
49
|
-
def mock_request(url, method = :get, options={}, mock_header={}, mock_body=nil)
|
50
|
-
request = Weary::Request.new(url, method, options)
|
51
|
-
http_response = mock_response(method, 200, mock_header, mock_body)
|
52
|
-
request.stub!(:perform).and_return Weary::Response.new(http_response, method)
|
53
|
-
|
54
|
-
request
|
43
|
+
[code.to_s,message]
|
55
44
|
end
|
data/spec/weary/base_spec.rb
CHANGED
@@ -203,7 +203,7 @@ describe Weary::Base do
|
|
203
203
|
cred = {:username => 'mwunsch', :password => 'secret'}
|
204
204
|
lambda { n.authentication_test }.should raise_error
|
205
205
|
n.credentials cred[:username], cred[:password]
|
206
|
-
n.authentication_test.
|
206
|
+
n.authentication_test.credentials.should == cred
|
207
207
|
end
|
208
208
|
|
209
209
|
it 'passes in default parameters if defined' do
|
@@ -211,14 +211,14 @@ describe Weary::Base do
|
|
211
211
|
defaults = {:id => 1234, :message => "Hello world"}
|
212
212
|
lambda { n.params_test }.should raise_error
|
213
213
|
n.defaults = defaults
|
214
|
-
n.params_test.
|
214
|
+
n.params_test.with.should == defaults.to_params
|
215
215
|
end
|
216
216
|
|
217
217
|
it 'accepts parameters when given' do
|
218
218
|
n = @methtest.new
|
219
219
|
req = n.params_test :id => 1234, :message => "Hello world", :foo => "Bar"
|
220
|
-
req.
|
221
|
-
req.
|
220
|
+
req.with.should == {:id => 1234, :message => "Hello world"}.to_params
|
221
|
+
req.with.include?("foo").should == false
|
222
222
|
end
|
223
223
|
|
224
224
|
|
@@ -253,7 +253,7 @@ describe Weary::Base do
|
|
253
253
|
|
254
254
|
obj.credentials "username", "password"
|
255
255
|
obj.instance_variable_get(:@credentials).should == {:username => "username", :password => "password"}
|
256
|
-
obj.important(:id => 1234).
|
256
|
+
obj.important(:id => 1234).credentials.should == obj.instance_variable_get(:@credentials)
|
257
257
|
end
|
258
258
|
|
259
259
|
it 'credentials can be an OAuth access token' do
|
@@ -263,8 +263,7 @@ describe Weary::Base do
|
|
263
263
|
|
264
264
|
obj.credentials oauth_token
|
265
265
|
obj.instance_variable_get(:@credentials).class.should == OAuth::AccessToken
|
266
|
-
obj.important(:id => 1234).
|
267
|
-
obj.important(:id => 1234).options.has_key?(:basic_auth).should == false
|
266
|
+
obj.important(:id => 1234).credentials.should == oauth_token
|
268
267
|
end
|
269
268
|
|
270
269
|
it 'can set defaults to pass into requests' do
|
@@ -272,7 +271,7 @@ describe Weary::Base do
|
|
272
271
|
|
273
272
|
obj.defaults = {:user => "mwunsch", :message => "hello world"}
|
274
273
|
obj.defaults.should == {:user => "mwunsch", :message => "hello world"}
|
275
|
-
obj.thing(:id => 1234).
|
274
|
+
obj.thing(:id => 1234).uri.query.should == {:user => "mwunsch", :message => "hello world", :id => 1234}.to_params
|
276
275
|
end
|
277
276
|
|
278
277
|
it 'has a list of resources' do
|
@@ -297,8 +296,8 @@ describe Weary::Base do
|
|
297
296
|
obj1.resources[:thing].follows = false
|
298
297
|
obj1.rebuild_method(obj1.resources[:thing])
|
299
298
|
|
300
|
-
obj1.thing(:id => 1234).
|
301
|
-
@klass.new.thing(:id => 1234).
|
299
|
+
obj1.thing(:id => 1234).follows?.should == false
|
300
|
+
@klass.new.thing(:id => 1234).follows?.should == true
|
302
301
|
end
|
303
302
|
|
304
303
|
it 'can modify a resource without modifying the resources of its class' do
|
@@ -314,7 +313,7 @@ describe Weary::Base do
|
|
314
313
|
|
315
314
|
obj.credentials "username", "password"
|
316
315
|
obj.modify_resource(:important) {|r| r.follows = false }
|
317
|
-
obj.important(:id => 1234).
|
316
|
+
obj.important(:id => 1234).follows?.should == false
|
318
317
|
end
|
319
318
|
|
320
319
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
describe Weary::Batch do
|
4
|
+
after do
|
5
|
+
FakeWeb.clean_registry
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'is a group of requests' do
|
9
|
+
requests = [ Weary.get('http://twitter.com'),
|
10
|
+
Weary.get('http://github.com'),
|
11
|
+
Weary.get('http://vimeo.com'),
|
12
|
+
Weary.get('http://tumblr.com')]
|
13
|
+
|
14
|
+
test = Weary::Batch.new(requests)
|
15
|
+
test.requests.should == requests
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'contains a pool of threads, left empty until processing' do
|
19
|
+
requests = [ Weary.get('http://twitter.com'),
|
20
|
+
Weary.get('http://github.com'),
|
21
|
+
Weary.get('http://vimeo.com'),
|
22
|
+
Weary.get('http://tumblr.com')]
|
23
|
+
|
24
|
+
test = Weary::Batch.new(requests)
|
25
|
+
test.pool.blank?.should == true
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'performs requests in parallel' do
|
29
|
+
resources = %w[http://twitter.com http://github.com http://vimeo.com http://tumblr.com]
|
30
|
+
resources.each {|url| FakeWeb.register_uri :get, url, :body => 'Hello world' }
|
31
|
+
|
32
|
+
requests = [ Weary.get('http://twitter.com'),
|
33
|
+
Weary.get('http://github.com'),
|
34
|
+
Weary.get('http://vimeo.com'),
|
35
|
+
Weary.get('http://tumblr.com')]
|
36
|
+
|
37
|
+
test = Weary::Batch.new(requests)
|
38
|
+
responses = test.perform
|
39
|
+
responses.size.should == 4
|
40
|
+
responses.each {|response| response.code.should == 200 }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'Callbacks' do
|
44
|
+
before do
|
45
|
+
resources = %w[http://twitter.com http://github.com http://vimeo.com http://tumblr.com]
|
46
|
+
resources.each {|url| FakeWeb.register_uri :get, url, :body => 'Hello world' }
|
47
|
+
@requests = [ Weary.get('http://twitter.com'),
|
48
|
+
Weary.get('http://github.com'),
|
49
|
+
Weary.get('http://vimeo.com'),
|
50
|
+
Weary.get('http://tumblr.com')]
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'stores the on_complete callback' do
|
54
|
+
test = Weary::Batch.new(@requests)
|
55
|
+
test.on_complete do
|
56
|
+
'hello'
|
57
|
+
end
|
58
|
+
test.on_complete.call.should == 'hello'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'stores the before_send callback' do
|
62
|
+
test = Weary::Batch.new(@requests)
|
63
|
+
test.before_send do
|
64
|
+
'hello'
|
65
|
+
end
|
66
|
+
test.before_send.call.should == 'hello'
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
data/spec/weary/httpverb_spec.rb
CHANGED
data/spec/weary/request_spec.rb
CHANGED
@@ -2,50 +2,343 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
|
2
2
|
|
3
3
|
describe Weary::Request do
|
4
4
|
|
5
|
-
it
|
5
|
+
it 'creates a Net/HTTP connection' do
|
6
6
|
test = Weary::Request.new("http://google.com")
|
7
|
-
test.
|
7
|
+
test.http.class.should == Net::HTTP
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
11
|
-
test = Weary::Request.new("http://google.com"
|
12
|
-
test.
|
10
|
+
it 'maps to a Net/HTTPRequest class' do
|
11
|
+
test = Weary::Request.new("http://google.com")
|
12
|
+
test.request_preparation.class.should == Net::HTTP::Get
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
describe 'Request' do
|
16
|
+
it 'prepares a Net/HTTP request' do
|
17
|
+
test = Weary::Request.new("http://google.com")
|
18
|
+
test.request.class.should == Net::HTTP::Get
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'prepares a body for POST' do
|
22
|
+
test = Weary::Request.new("http://foo.bar", :post)
|
23
|
+
test.with = {:name => "markwunsch"}
|
24
|
+
req = test.request
|
25
|
+
req.class.should == Net::HTTP::Post
|
26
|
+
req.body.should == test.with
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets up headers' do
|
30
|
+
test = Weary::Request.new("http://foo.bar")
|
31
|
+
test.headers = {"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]}
|
32
|
+
req = test.request
|
33
|
+
req['User-Agent'].should == Weary::UserAgents["Safari 4.0.2 - Mac"]
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'has an authorization header when basic auth is used' do
|
37
|
+
test = Weary::Request.new("http://foo.bar")
|
38
|
+
test.credentials = {:username => "mark", :password => "secret"}
|
39
|
+
req = test.request
|
40
|
+
req.key?('Authorization').should == true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "prepares an oauth scheme if a token is provided" do
|
44
|
+
consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
|
45
|
+
token = OAuth::AccessToken.new(consumer, "token", "secret")
|
46
|
+
test = Weary::Request.new("http://foo.bar", :post)
|
47
|
+
test.credentials = token
|
48
|
+
test.request.oauth_helper.options[:token].should == token
|
49
|
+
end
|
18
50
|
end
|
19
51
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
52
|
+
describe 'Options' do
|
53
|
+
it 'sets the credentials to basic authentication' do
|
54
|
+
basic_auth = {:username => 'mark', :password => 'secret'}
|
55
|
+
test = Weary::Request.new("http://foo.bar", :get, {:basic_auth => basic_auth})
|
56
|
+
test.credentials.should == basic_auth
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'sets the credentials to an oauth token' do
|
60
|
+
consumer = OAuth::Consumer.new("consumer_token","consumer_secret",{:site => 'http://foo.bar'})
|
61
|
+
token = OAuth::AccessToken.new(consumer, "token", "secret")
|
62
|
+
test = Weary::Request.new("http://foo.bar", :post, {:oauth => token})
|
63
|
+
test.credentials.should == token
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'sets the body params' do
|
67
|
+
body = {:options => "something"}
|
68
|
+
test = Weary::Request.new("http://foo.bar", :post, {:body => body})
|
69
|
+
test.with.should == body.to_params
|
70
|
+
test2 = Weary::Request.new("http://foo.bar", :post, {:body => body.to_params})
|
71
|
+
test2.with.should == body.to_params
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'sets header values' do
|
75
|
+
head = {"User-Agent" => Weary::UserAgents["Safari 4.0.2 - Mac"]}
|
76
|
+
test = Weary::Request.new("http://foo.bar", :get, {:headers => head})
|
77
|
+
test.headers.should == head
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'sets a following value for redirection' do
|
81
|
+
test = Weary::Request.new("http://foo.bar", :get, {:no_follow => true})
|
82
|
+
test.follows?.should == false
|
83
|
+
test = Weary::Request.new("http://foo.bar", :get, {:no_follow => false})
|
84
|
+
test.follows?.should == true
|
85
|
+
test = Weary::Request.new("http://foo.bar", :get)
|
86
|
+
test.follows?.should == true
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'uses the #with hash to create a URI query string if the method is a GET' do
|
90
|
+
test = Weary::Request.new("http://foo.bar/path/to/something")
|
91
|
+
test.with = {:name => "markwunsch", :title => "awesome"}
|
92
|
+
test.uri.query.should == test.with
|
93
|
+
end
|
28
94
|
end
|
95
|
+
|
96
|
+
describe 'Perform' do
|
97
|
+
# These tests reveal tight coupling with Response API, which may or may not be a good thing
|
98
|
+
|
99
|
+
after do
|
100
|
+
FakeWeb.clean_registry
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'performs the request and gets back a response' do
|
104
|
+
hello = "Hello from FakeWeb"
|
105
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => hello)
|
106
|
+
|
107
|
+
test = Weary::Request.new("http://markwunsch.com")
|
108
|
+
response = test.perform
|
109
|
+
response.class.should == Weary::Response
|
110
|
+
response.body.should == hello
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'follows redirection' do
|
114
|
+
hello = "Hello from FakeWeb"
|
115
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(301), :Location => 'http://redirected.com')
|
116
|
+
FakeWeb.register_uri(:get, "http://redirected.com", :body => hello)
|
117
|
+
|
118
|
+
test = Weary::Request.new("http://markwunsch.com")
|
119
|
+
response = test.perform
|
120
|
+
response.body.should == hello
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'will not follow redirection if disabled' do
|
124
|
+
hello = "Hello from FakeWeb"
|
125
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(301), :Location => 'http://redirected.com')
|
126
|
+
FakeWeb.register_uri(:get, "http://redirected.com", :body => hello)
|
127
|
+
|
128
|
+
test = Weary::Request.new("http://markwunsch.com", :get, :no_follow => true)
|
129
|
+
response = test.perform
|
130
|
+
response.code.should == 301
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'passes the response into a callback' do
|
134
|
+
hello = "Hello from FakeWeb"
|
135
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => hello)
|
136
|
+
response_body = ""
|
137
|
+
|
138
|
+
test = Weary::Request.new("http://markwunsch.com")
|
139
|
+
test.perform do |response|
|
140
|
+
response_body = response.body
|
141
|
+
end
|
142
|
+
|
143
|
+
response_body.should == hello
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'performs the callback even when redirected' do
|
147
|
+
hello = "Hello from FakeWeb"
|
148
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(301), :Location => 'http://redirected.com')
|
149
|
+
FakeWeb.register_uri(:get, "http://redirected.com", :body => hello)
|
150
|
+
|
151
|
+
response_body = ""
|
152
|
+
|
153
|
+
test = Weary::Request.new("http://markwunsch.com")
|
154
|
+
test.perform do |response|
|
155
|
+
response_body = response.body
|
156
|
+
end
|
157
|
+
|
158
|
+
response_body.should == hello
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'authorizes with basic authentication' do
|
162
|
+
message = 'You are authorized to do that.'
|
163
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(401))
|
164
|
+
FakeWeb.register_uri(:get, "http://mark:secret@markwunsch.com", :body => message)
|
165
|
+
|
166
|
+
test = Weary::Request.new("http://markwunsch.com")
|
167
|
+
response = test.perform
|
168
|
+
response.code.should == 401
|
169
|
+
response.body.should_not == message
|
170
|
+
test.credentials = {:username => 'mark', :password => 'secret'}
|
171
|
+
response = test.perform
|
172
|
+
response.code.should == 200
|
173
|
+
response.body.should == message
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'still authorizes correctly if redirected' do
|
177
|
+
message = 'You are authorized to do that.'
|
178
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(401))
|
179
|
+
FakeWeb.register_uri(:get, "http://mark:secret@markwunsch.com", :status => http_status_message(301), :Location => 'http://markwunsch.net')
|
180
|
+
FakeWeb.register_uri(:get, "http://markwunsch.net", :status => http_status_message(401))
|
181
|
+
FakeWeb.register_uri(:get, "http://mark:secret@markwunsch.net", :body => message)
|
182
|
+
|
183
|
+
test = Weary::Request.new("http://markwunsch.com")
|
184
|
+
test.credentials = {:username => 'mark', :password => 'secret'}
|
185
|
+
response = test.perform
|
186
|
+
response.code.should == 200
|
187
|
+
response.body.should == message
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'converts parameters to url query strings' do
|
191
|
+
params = {:id => 'mark', :message => 'hello'}
|
192
|
+
message = "Using FakeWeb with params of #{params.to_params}"
|
193
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(403))
|
194
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com?#{params.to_params}", :body => message)
|
195
|
+
|
196
|
+
test = Weary::Request.new("http://markwunsch.com")
|
197
|
+
test.with = params
|
198
|
+
response = test.perform
|
199
|
+
response.body.should == message
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'sends query strings correctly when redirected' do
|
203
|
+
params = {:id => 'mark', :message => 'hello'}
|
204
|
+
message = "Using FakeWeb with params of #{params.to_params}"
|
205
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(403))
|
206
|
+
FakeWeb.register_uri(:get, "http://markwunsch.net", :status => http_status_message(403))
|
207
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com?#{params.to_params}", :status => http_status_message(301), :Location => 'http://markwunsch.net')
|
208
|
+
FakeWeb.register_uri(:get, "http://markwunsch.net?#{params.to_params}", :body => message)
|
209
|
+
|
210
|
+
test = Weary::Request.new("http://markwunsch.com")
|
211
|
+
test.with = params
|
212
|
+
response = test.perform
|
213
|
+
response.code.should == 200
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'converts parameters to request body on post' do
|
217
|
+
params = {:id => 'mark', :message => 'hello'}
|
218
|
+
message = "Using FakeWeb with params of #{params.to_params}"
|
219
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(403))
|
220
|
+
FakeWeb.register_uri(:post, "http://markwunsch.com", :body => message)
|
221
|
+
|
222
|
+
test = Weary::Request.new("http://markwunsch.com")
|
223
|
+
test.via = :post
|
224
|
+
test.with = params
|
225
|
+
response = test.perform
|
226
|
+
response.code.should == 200
|
227
|
+
response.body.should == message
|
228
|
+
|
229
|
+
# No way of testing Request bodies with FakeWeb as of 1.2.7
|
230
|
+
end
|
29
231
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
232
|
+
describe 'Non-Blocking, Threaded' do
|
233
|
+
# Not exactly sure the best way to test these
|
234
|
+
|
235
|
+
it 'creates a new thread to perform the request' do
|
236
|
+
hello = "Hello from FakeWeb"
|
237
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => hello)
|
238
|
+
|
239
|
+
test = Weary::Request.new("http://markwunsch.com")
|
240
|
+
test.perform!.value.body.should == hello
|
241
|
+
end
|
242
|
+
|
243
|
+
it "sets its callback" do
|
244
|
+
msg = "You did it!"
|
245
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => msg)
|
246
|
+
test = Weary::Request.new("http://markwunsch.com")
|
247
|
+
body = ""
|
248
|
+
|
249
|
+
thread = test.perform! do |r|
|
250
|
+
body = r.body
|
251
|
+
end
|
252
|
+
body = thread.value.body
|
253
|
+
body.should == msg
|
254
|
+
end
|
255
|
+
end
|
41
256
|
end
|
42
257
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
258
|
+
describe 'Callbacks' do
|
259
|
+
after do
|
260
|
+
FakeWeb.clean_registry
|
261
|
+
end
|
262
|
+
|
263
|
+
describe 'on_complete' do
|
264
|
+
it 'stores the callback' do
|
265
|
+
test = Weary::Request.new("http://markwunsch.com")
|
266
|
+
test.on_complete do
|
267
|
+
'hello'
|
268
|
+
end
|
269
|
+
test.on_complete.call.should == 'hello'
|
270
|
+
end
|
271
|
+
|
272
|
+
it 'accepts a block, and the block becomes the callback' do
|
273
|
+
msg = "You did it!"
|
274
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => msg)
|
275
|
+
test = Weary::Request.new("http://markwunsch.com")
|
276
|
+
body = ""
|
277
|
+
|
278
|
+
test.on_complete do |response|
|
279
|
+
body = response.body
|
280
|
+
end
|
281
|
+
|
282
|
+
test.perform
|
283
|
+
body.should == msg
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'is overriden when a block is passed to the perform method' do
|
287
|
+
msg = "You did it!"
|
288
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => msg)
|
289
|
+
test = Weary::Request.new("http://markwunsch.com")
|
290
|
+
body = ""
|
291
|
+
|
292
|
+
test.on_complete do |response|
|
293
|
+
body = response.body
|
294
|
+
end
|
295
|
+
test.perform
|
296
|
+
body.should == msg
|
297
|
+
|
298
|
+
test.perform do |response|
|
299
|
+
body = 'Now it is different'
|
300
|
+
end
|
301
|
+
|
302
|
+
body.should == 'Now it is different'
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
describe 'before_send' do
|
307
|
+
it 'stores the callback' do
|
308
|
+
test = Weary::Request.new("http://markwunsch.com")
|
309
|
+
test.before_send do
|
310
|
+
'hello'
|
311
|
+
end
|
312
|
+
test.before_send.call.should == 'hello'
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'accepts a block, and the block becomes the callback' do
|
316
|
+
msg = "You did it!"
|
317
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :body => msg)
|
318
|
+
test = Weary::Request.new("http://markwunsch.com")
|
319
|
+
body = ""
|
320
|
+
|
321
|
+
test.before_send do
|
322
|
+
body = msg
|
323
|
+
end
|
324
|
+
body.should_not == msg
|
325
|
+
test.perform
|
326
|
+
body.should == msg
|
327
|
+
end
|
328
|
+
|
329
|
+
it 'takes the Request as an argument, so it can be manipulate before sending' do
|
330
|
+
hello = "Hello from FakeWeb"
|
331
|
+
FakeWeb.register_uri(:get, "http://markwunsch.com", :status => http_status_message(301), :Location => 'http://redirected.com')
|
332
|
+
FakeWeb.register_uri(:get, "http://redirected.com", :body => hello)
|
333
|
+
|
334
|
+
test = Weary::Request.new("http://markwunsch.com")
|
335
|
+
test.before_send do |req|
|
336
|
+
req.follows = false
|
337
|
+
end
|
338
|
+
|
339
|
+
test.perform.code.should == 301
|
340
|
+
end
|
341
|
+
end
|
49
342
|
end
|
50
343
|
|
51
344
|
end
|