resourceful 0.3.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +24 -28
- data/Rakefile +44 -14
- data/lib/resourceful.rb +11 -21
- data/lib/resourceful/authentication_manager.rb +3 -2
- data/lib/resourceful/cache_manager.rb +58 -1
- data/lib/resourceful/exceptions.rb +34 -0
- data/lib/resourceful/header.rb +95 -0
- data/lib/resourceful/http_accessor.rb +0 -2
- data/lib/resourceful/memcache_cache_manager.rb +3 -13
- data/lib/resourceful/net_http_adapter.rb +15 -5
- data/lib/resourceful/request.rb +180 -18
- data/lib/resourceful/resource.rb +38 -141
- data/lib/resourceful/response.rb +142 -95
- data/resourceful.gemspec +9 -7
- data/spec/acceptance/authorization_spec.rb +16 -0
- data/spec/acceptance/caching_spec.rb +192 -0
- data/spec/acceptance/header_spec.rb +24 -0
- data/spec/acceptance/redirecting_spec.rb +12 -0
- data/spec/acceptance/resource_spec.rb +84 -0
- data/spec/acceptance_shared_specs.rb +12 -17
- data/spec/{acceptance_spec.rb → old_acceptance_specs.rb} +27 -57
- data/spec/simple_sinatra_server.rb +74 -0
- data/spec/simple_sinatra_server_spec.rb +98 -0
- data/spec/spec_helper.rb +21 -7
- metadata +50 -42
- data/spec/resourceful/authentication_manager_spec.rb +0 -249
- data/spec/resourceful/cache_manager_spec.rb +0 -223
- data/spec/resourceful/header_spec.rb +0 -38
- data/spec/resourceful/http_accessor_spec.rb +0 -164
- data/spec/resourceful/memcache_cache_manager_spec.rb +0 -111
- data/spec/resourceful/net_http_adapter_spec.rb +0 -96
- data/spec/resourceful/options_interpreter_spec.rb +0 -102
- data/spec/resourceful/request_spec.rb +0 -186
- data/spec/resourceful/resource_spec.rb +0 -600
- data/spec/resourceful/response_spec.rb +0 -238
- data/spec/resourceful/stubbed_resource_proxy_spec.rb +0 -58
- data/spec/simple_http_server_shared_spec.rb +0 -162
- data/spec/simple_http_server_shared_spec_spec.rb +0 -212
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,28 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'pathname'
|
3
2
|
require 'spec'
|
4
|
-
require 'pp'
|
5
|
-
require 'facets'
|
6
3
|
|
7
|
-
$LOAD_PATH <<
|
8
|
-
require 'resourceful/util'
|
4
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
|
9
5
|
require 'resourceful'
|
10
|
-
require 'resourceful/http_accessor'
|
11
6
|
|
12
|
-
|
7
|
+
$LOAD_PATH << File.dirname(__FILE__) # ./spec
|
13
8
|
|
9
|
+
# Spawn the server in another process
|
10
|
+
|
11
|
+
@server = Thread.new do
|
12
|
+
|
13
|
+
require 'simple_sinatra_server'
|
14
|
+
Sinatra::Default.set(
|
15
|
+
:run => true,
|
16
|
+
:logging => false
|
17
|
+
)
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
# Kill the server process when rspec finishes
|
22
|
+
at_exit { @server.exit }
|
23
|
+
|
24
|
+
|
25
|
+
# Give the app a change to initialize
|
26
|
+
$stderr.puts "Waiting for thin to initialize..."
|
27
|
+
sleep 0.2
|
14
28
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resourceful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Sadauskas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-07-13 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -82,6 +82,16 @@ dependencies:
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: "0"
|
84
84
|
version:
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: sinatra
|
87
|
+
type: :development
|
88
|
+
version_requirement:
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: "0"
|
94
|
+
version:
|
85
95
|
description: An HTTP library for Ruby that takes advantage of everything HTTP has to offer.
|
86
96
|
email: psadauskas@gmail.com
|
87
97
|
executables: []
|
@@ -89,58 +99,56 @@ executables: []
|
|
89
99
|
extensions: []
|
90
100
|
|
91
101
|
extra_rdoc_files:
|
92
|
-
-
|
93
|
-
- lib/resourceful/
|
94
|
-
- lib/resourceful/
|
95
|
-
- lib/resourceful/resource.rb
|
96
|
-
- lib/resourceful/memcache_cache_manager.rb
|
102
|
+
- README.markdown
|
103
|
+
- lib/resourceful/cache_manager.rb
|
104
|
+
- lib/resourceful/exceptions.rb
|
97
105
|
- lib/resourceful/net_http_adapter.rb
|
98
|
-
- lib/resourceful/http_accessor.rb
|
99
106
|
- lib/resourceful/stubbed_resource_proxy.rb
|
100
107
|
- lib/resourceful/header.rb
|
101
|
-
- lib/resourceful/cache_manager.rb
|
102
|
-
- lib/resourceful/options_interpreter.rb
|
103
|
-
- lib/resourceful/response.rb
|
104
|
-
- lib/resourceful/request.rb
|
105
|
-
- README.markdown
|
106
|
-
files:
|
107
|
-
- lib/resourceful.rb
|
108
108
|
- lib/resourceful/authentication_manager.rb
|
109
|
-
- lib/resourceful/
|
109
|
+
- lib/resourceful/request.rb
|
110
110
|
- lib/resourceful/resource.rb
|
111
|
+
- lib/resourceful/response.rb
|
112
|
+
- lib/resourceful/util.rb
|
113
|
+
- lib/resourceful/http_accessor.rb
|
114
|
+
- lib/resourceful/options_interpreter.rb
|
111
115
|
- lib/resourceful/memcache_cache_manager.rb
|
116
|
+
- lib/resourceful.rb
|
117
|
+
files:
|
118
|
+
- Manifest
|
119
|
+
- spec/spec.opts
|
120
|
+
- spec/simple_sinatra_server_spec.rb
|
121
|
+
- spec/simple_sinatra_server.rb
|
122
|
+
- spec/acceptance/header_spec.rb
|
123
|
+
- spec/acceptance/caching_spec.rb
|
124
|
+
- spec/acceptance/resource_spec.rb
|
125
|
+
- spec/acceptance/redirecting_spec.rb
|
126
|
+
- spec/acceptance/authorization_spec.rb
|
127
|
+
- spec/acceptance_shared_specs.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/old_acceptance_specs.rb
|
130
|
+
- README.markdown
|
131
|
+
- resourceful.gemspec
|
132
|
+
- lib/resourceful/cache_manager.rb
|
133
|
+
- lib/resourceful/exceptions.rb
|
112
134
|
- lib/resourceful/net_http_adapter.rb
|
113
|
-
- lib/resourceful/http_accessor.rb
|
114
135
|
- lib/resourceful/stubbed_resource_proxy.rb
|
115
136
|
- lib/resourceful/header.rb
|
116
|
-
- lib/resourceful/
|
117
|
-
- lib/resourceful/options_interpreter.rb
|
118
|
-
- lib/resourceful/response.rb
|
137
|
+
- lib/resourceful/authentication_manager.rb
|
119
138
|
- lib/resourceful/request.rb
|
120
|
-
-
|
121
|
-
-
|
122
|
-
-
|
123
|
-
-
|
124
|
-
-
|
125
|
-
-
|
126
|
-
-
|
127
|
-
- spec/resourceful/memcache_cache_manager_spec.rb
|
128
|
-
- spec/resourceful/response_spec.rb
|
129
|
-
- spec/resourceful/options_interpreter_spec.rb
|
130
|
-
- spec/resourceful/http_accessor_spec.rb
|
131
|
-
- spec/resourceful/stubbed_resource_proxy_spec.rb
|
132
|
-
- spec/resourceful/request_spec.rb
|
133
|
-
- spec/resourceful/resource_spec.rb
|
134
|
-
- spec/resourceful/cache_manager_spec.rb
|
135
|
-
- spec/resourceful/net_http_adapter_spec.rb
|
136
|
-
- spec/simple_http_server_shared_spec.rb
|
137
|
-
- Manifest
|
139
|
+
- lib/resourceful/resource.rb
|
140
|
+
- lib/resourceful/response.rb
|
141
|
+
- lib/resourceful/util.rb
|
142
|
+
- lib/resourceful/http_accessor.rb
|
143
|
+
- lib/resourceful/options_interpreter.rb
|
144
|
+
- lib/resourceful/memcache_cache_manager.rb
|
145
|
+
- lib/resourceful.rb
|
138
146
|
- Rakefile
|
139
|
-
- README.markdown
|
140
147
|
- MIT-LICENSE
|
141
|
-
- resourceful.gemspec
|
142
148
|
has_rdoc: true
|
143
149
|
homepage: http://github.com/paul/resourceful
|
150
|
+
licenses: []
|
151
|
+
|
144
152
|
post_install_message:
|
145
153
|
rdoc_options:
|
146
154
|
- --line-numbers
|
@@ -166,9 +174,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
174
|
requirements: []
|
167
175
|
|
168
176
|
rubyforge_project: resourceful
|
169
|
-
rubygems_version: 1.3.
|
177
|
+
rubygems_version: 1.3.3
|
170
178
|
signing_key:
|
171
|
-
specification_version:
|
179
|
+
specification_version: 3
|
172
180
|
summary: An HTTP library for Ruby that takes advantage of everything HTTP has to offer.
|
173
181
|
test_files: []
|
174
182
|
|
@@ -1,249 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require Pathname(__FILE__).dirname + '../spec_helper'
|
3
|
-
|
4
|
-
require 'resourceful/authentication_manager'
|
5
|
-
|
6
|
-
describe Resourceful::AuthenticationManager do
|
7
|
-
|
8
|
-
before do
|
9
|
-
@authmgr = Resourceful::AuthenticationManager.new
|
10
|
-
|
11
|
-
@authenticator = mock('Authenticator')
|
12
|
-
end
|
13
|
-
|
14
|
-
[:add_auth_handler, :associate_auth_info, :add_credentials].each do |meth|
|
15
|
-
it "should have ##{meth}" do
|
16
|
-
@authmgr.should respond_to(meth)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'should add an authenticator to its list' do
|
21
|
-
@authmgr.add_auth_handler(@authenticator)
|
22
|
-
@authmgr.instance_variable_get("@authenticators").should include(@authenticator)
|
23
|
-
end
|
24
|
-
|
25
|
-
describe 'associating authenticators with challanges' do
|
26
|
-
before do
|
27
|
-
@authmgr.add_auth_handler(@authenticator)
|
28
|
-
@authenticator.stub!(:valid_for?).and_return(true)
|
29
|
-
@authenticator.stub!(:update_credentials)
|
30
|
-
@challenge = mock('Response')
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should check that an authenticator is valid for a challenge' do
|
34
|
-
@authenticator.should_receive(:valid_for?).with(@challenge).and_return(true)
|
35
|
-
@authmgr.associate_auth_info(@challenge)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should update the credentials of the authenticator if it is valid for the challenge' do
|
39
|
-
@authenticator.should_receive(:update_credentials).with(@challenge)
|
40
|
-
@authmgr.associate_auth_info(@challenge)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'should not update the credentials of the authenticator if it is not valid for the challenge' do
|
44
|
-
@authenticator.stub!(:valid_for?).and_return(false)
|
45
|
-
@authenticator.should_not_receive(:update_credentials).with(@challenge)
|
46
|
-
@authmgr.associate_auth_info(@challenge)
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
describe 'adding credentials to a request' do
|
52
|
-
before do
|
53
|
-
@authmgr.add_auth_handler(@authenticator)
|
54
|
-
@authenticator.stub!(:can_handle?).and_return(true)
|
55
|
-
@authenticator.stub!(:add_credentials_to)
|
56
|
-
@request = mock('Request')
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should find an authenticator that can handle the request' do
|
60
|
-
@authenticator.should_receive(:can_handle?).with(@request).and_return(true)
|
61
|
-
@authmgr.add_credentials(@request)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should add the authenticators credentials to the request' do
|
65
|
-
@authenticator.should_receive(:add_credentials_to).with(@request)
|
66
|
-
@authmgr.add_credentials(@request)
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'should not add the authenticators credentials to the request if it cant handle it' do
|
70
|
-
@authenticator.should_receive(:can_handle?).with(@request).and_return(false)
|
71
|
-
@authenticator.should_not_receive(:add_credentials_to).with(@request)
|
72
|
-
@authmgr.add_credentials(@request)
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
describe Resourceful::BasicAuthenticator do
|
80
|
-
before do
|
81
|
-
@auth = Resourceful::BasicAuthenticator.new('Test Auth', 'admin', 'secret')
|
82
|
-
end
|
83
|
-
|
84
|
-
{:realm => 'Test Auth', :username => 'admin', :password => 'secret'}.each do |meth,val|
|
85
|
-
it "should initialize with a #{meth}" do
|
86
|
-
@auth.instance_variable_get("@#{meth}").should == val
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "Updating from a challenge response" do
|
91
|
-
before do
|
92
|
-
@header = {'WWW-Authenticate' => ['Basic realm="Test Auth"']}
|
93
|
-
@chal = mock('response', :header => @header, :uri => 'http://example.com/foo/bar')
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'should be valid for a challenge response with scheme "Basic" and the same realm' do
|
97
|
-
@auth.valid_for?(@chal).should be_true
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'should be valid for a challenge response with multiple schemes including matchin "Basic" challenge' do
|
101
|
-
@header = {'WWW-Authenticate' => ['Digest some other stuff', 'Basic realm="Test Auth"', 'Weird scheme']}
|
102
|
-
|
103
|
-
@auth.valid_for?(@chal).should be_true
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'should not be sensitive to case variances in the scheme' do
|
107
|
-
@header['WWW-Authenticate'] = ['bAsIc realm="Test Auth"']
|
108
|
-
@auth.valid_for?(@chal).should be_true
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'should not be sensitive to case variances in the realm directive' do
|
112
|
-
@header['WWW-Authenticate'] = ['Basic rEaLm="Test Auth"']
|
113
|
-
@auth.valid_for?(@chal).should be_true
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'should not be sensitive to case variances in the realm value' do
|
117
|
-
@header['WWW-Authenticate'] = ['Basic realm="test auth"']
|
118
|
-
@auth.valid_for?(@chal).should be_true
|
119
|
-
end
|
120
|
-
|
121
|
-
it 'should not be valid if the scheme is not "Basic"' do
|
122
|
-
@header['WWW-Authenticate'] = ["Digest"]
|
123
|
-
@auth.valid_for?(@chal).should be_false
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'should not be valid if the realm does not match' do
|
127
|
-
@header['WWW-Authenticate'] = ['Basic realm="not test auth"']
|
128
|
-
@auth.valid_for?(@chal).should be_false
|
129
|
-
end
|
130
|
-
|
131
|
-
it 'should not be valid if the header is unreadable' do
|
132
|
-
@header['WWW-Authenticate'] = nil
|
133
|
-
@auth.valid_for?(@chal).should be_false
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'should set the valid domain from the host part of the challenge uri' do
|
137
|
-
@auth.update_credentials(@chal)
|
138
|
-
@auth.instance_variable_get("@domain").should == 'example.com'
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
describe 'updating a request with credentials' do
|
144
|
-
before do
|
145
|
-
@auth.instance_variable_set("@domain", 'example.com')
|
146
|
-
@header = {}
|
147
|
-
@req = mock('request', :uri => 'http://example.com/bar/foo', :header => @header)
|
148
|
-
end
|
149
|
-
|
150
|
-
it 'should be able to handle a request for the matching domain' do
|
151
|
-
@auth.can_handle?(@req).should be_true
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'should add credentials to a request' do
|
155
|
-
@header.should_receive(:[]=).with('Authorization', 'Basic YWRtaW46c2VjcmV0')
|
156
|
-
@auth.add_credentials_to(@req)
|
157
|
-
end
|
158
|
-
|
159
|
-
it 'should build the credentials string for the header' do
|
160
|
-
@auth.credentials.should == 'Basic YWRtaW46c2VjcmV0'
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
describe Resourceful::DigestAuthenticator do
|
167
|
-
|
168
|
-
before do
|
169
|
-
@header = {'WWW-Authenticate' => ['Digest realm="Test Auth"']}
|
170
|
-
@chal = mock('response', :header => @header, :uri => 'http://example.com/foo/bar')
|
171
|
-
|
172
|
-
@req_header = {}
|
173
|
-
@req = mock('request', :header => @req_header,
|
174
|
-
:uri => 'http://example.com',
|
175
|
-
:method => 'GET')
|
176
|
-
|
177
|
-
@auth = Resourceful::DigestAuthenticator.new('Test Auth', 'admin', 'secret')
|
178
|
-
end
|
179
|
-
|
180
|
-
{:realm => 'Test Auth', :username => 'admin', :password => 'secret'}.each do |meth,val|
|
181
|
-
it "should initialize with a #{meth}" do
|
182
|
-
@auth.instance_variable_get("@#{meth}").should == val
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
describe "Updating credentials from a challenge response" do
|
187
|
-
|
188
|
-
it "should set the domain from the host part of the challenge response uri" do
|
189
|
-
@auth.update_credentials(@chal)
|
190
|
-
@auth.domain.should == 'example.com'
|
191
|
-
end
|
192
|
-
|
193
|
-
it "should create an HTTPAuth Digest Challenge from the challenge response WWW-Authenticate header" do
|
194
|
-
HTTPAuth::Digest::Challenge.should_receive(:from_header).with(@header['WWW-Authenticate'].first)
|
195
|
-
@auth.update_credentials(@chal)
|
196
|
-
end
|
197
|
-
|
198
|
-
end
|
199
|
-
|
200
|
-
describe "Validating a challenge" do
|
201
|
-
it 'should be valid for a challenge response with scheme "Digest" and the same realm' do
|
202
|
-
@auth.valid_for?(@chal).should be_true
|
203
|
-
end
|
204
|
-
|
205
|
-
it 'should not be valid if the scheme is not "Digest"' do
|
206
|
-
@header['WWW-Authenticate'] = ["Basic"]
|
207
|
-
@auth.valid_for?(@chal).should be_false
|
208
|
-
end
|
209
|
-
|
210
|
-
it 'should not be valid if the realm does not match' do
|
211
|
-
@header['WWW-Authenticate'] = ['Digest realm="not test auth"']
|
212
|
-
@auth.valid_for?(@chal).should be_false
|
213
|
-
end
|
214
|
-
|
215
|
-
it 'should not be valid if the header is unreadable' do
|
216
|
-
@header['WWW-Authenticate'] = nil
|
217
|
-
@auth.valid_for?(@chal).should be_false
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
it "should be able to handle requests to the same domain" do
|
222
|
-
@auth.instance_variable_set("@domain", 'example.com')
|
223
|
-
@auth.can_handle?(@req).should be_true
|
224
|
-
end
|
225
|
-
|
226
|
-
it "should not handle requests to a different domain" do
|
227
|
-
@auth.instance_variable_set("@domain", 'example2.com')
|
228
|
-
@auth.can_handle?(@req).should be_false
|
229
|
-
end
|
230
|
-
|
231
|
-
it "should add credentials to a request" do
|
232
|
-
@auth.update_credentials(@chal)
|
233
|
-
@auth.add_credentials_to(@req)
|
234
|
-
@req_header.should have_key('Authorization')
|
235
|
-
@req_header['Authorization'].should_not be_blank
|
236
|
-
end
|
237
|
-
|
238
|
-
it "should have HTTPAuth::Digest generate the Authorization header" do
|
239
|
-
@auth.update_credentials(@chal)
|
240
|
-
cred = mock('digest_credentials', :to_header => nil)
|
241
|
-
|
242
|
-
HTTPAuth::Digest::Credentials.should_receive(:from_challenge).with(
|
243
|
-
@auth.challenge, :username => 'admin', :password => 'secret', :method => 'GET', :uri => ''
|
244
|
-
).and_return(cred)
|
245
|
-
|
246
|
-
cred = @auth.credentials_for(@req)
|
247
|
-
end
|
248
|
-
|
249
|
-
end
|
@@ -1,223 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require Pathname(__FILE__).dirname + '../spec_helper'
|
3
|
-
|
4
|
-
require 'resourceful/cache_manager'
|
5
|
-
|
6
|
-
describe Resourceful::AbstractCacheManager do
|
7
|
-
before do
|
8
|
-
@cm = Resourceful::InMemoryCacheManager.new #cheat, because I cant new a real one.
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'should not be initializable' do
|
12
|
-
lambda { Resourceful::CacheManager.new }.should raise_error
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'should have a lookup method' do
|
16
|
-
@cm.should respond_to(:lookup)
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should have a store method' do
|
20
|
-
@cm.should respond_to(:store)
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should have a invalidate method' do
|
24
|
-
@cm.should respond_to(:invalidate)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe Resourceful::NullCacheManager do
|
29
|
-
before do
|
30
|
-
@ncm = Resourceful::NullCacheManager.new
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should not find anything' do
|
34
|
-
@ncm.lookup(:stuff).should be_nil
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should not store anything' do
|
38
|
-
@ncm.should respond_to(:store)
|
39
|
-
|
40
|
-
lambda { @ncm.store(:foo, :bar) }.should_not raise_error
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
describe Resourceful::InMemoryCacheManager do
|
46
|
-
before do
|
47
|
-
@request = mock('request', :resource => mock('resource'),
|
48
|
-
:request_time => Time.utc(2008,5,22,15,00),
|
49
|
-
:uri => 'uri')
|
50
|
-
@response = mock('response', :header => {}, :cachable? => true)
|
51
|
-
|
52
|
-
@entry = mock('cache entry', :response => @response, :valid_for? => true)
|
53
|
-
Resourceful::CacheEntry.stub!(:new).and_return(@entry)
|
54
|
-
|
55
|
-
@imcm = Resourceful::InMemoryCacheManager.new
|
56
|
-
end
|
57
|
-
|
58
|
-
describe 'finding' do
|
59
|
-
before do
|
60
|
-
@response.stub!(:authoritative=)
|
61
|
-
@imcm.instance_variable_set("@collection", {'uri' => {@request => @response}})
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should lookup the response by request' do
|
65
|
-
@imcm.lookup(@request).should == @response
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'should set the response to non-authoritative' do
|
69
|
-
@response.should_receive(:authoritative=).with(false)
|
70
|
-
@imcm.lookup(@request)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe 'saving' do
|
75
|
-
it 'should make a new cache entry' do
|
76
|
-
Resourceful::CacheEntry.should_receive(:new).with(
|
77
|
-
@request,
|
78
|
-
@response
|
79
|
-
)
|
80
|
-
|
81
|
-
@imcm.store(@request, @response)
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'should store the response entity by request' do
|
85
|
-
@imcm.store(@request, @response)
|
86
|
-
col = @imcm.instance_variable_get("@collection")
|
87
|
-
col['uri'][@request].should == @response
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'should check if the response is cachable' do
|
91
|
-
@response.should_receive(:cachable?).and_return(true)
|
92
|
-
@imcm.store(@request, @response)
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'should not store an entry if the response is not cachable' do
|
96
|
-
@response.should_receive(:cachable?).and_return(false)
|
97
|
-
@imcm.store(@request, @response)
|
98
|
-
col = @imcm.instance_variable_get("@collection")
|
99
|
-
col['uri'][@request].should be_nil
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
describe 'invalidating' do
|
104
|
-
it 'should remove an entry from the cache by uri' do
|
105
|
-
@imcm.store(@request, @response)
|
106
|
-
@imcm.invalidate('uri')
|
107
|
-
col = @imcm.instance_variable_get("@collection")
|
108
|
-
col.should_not have_key('uri')
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
describe Resourceful::CacheEntryCollection do
|
115
|
-
before do
|
116
|
-
@request = mock('request', :uri => 'this', :request_time => Time.now, :header => {})
|
117
|
-
@valid_resp = stub('valid_resp', :authoritative= => nil, :header => {})
|
118
|
-
|
119
|
-
@entry_valid = mock('entry', :valid_for? => true, :response => @valid_resp)
|
120
|
-
@entry_invalid = mock('entry', :valid_for? => false, :response => stub('invalid_resp'))
|
121
|
-
|
122
|
-
@collection = Resourceful::CacheEntryCollection.new
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'should find the right entry for a request' do
|
126
|
-
@collection.instance_variable_set('@entries', [@entry_valid, @entry_invalid])
|
127
|
-
@entry_valid.should_receive(:valid_for?).with(@request).and_return(true)
|
128
|
-
@collection[@request].should == @valid_resp
|
129
|
-
end
|
130
|
-
|
131
|
-
it 'should be nil if no matching entry was found' do
|
132
|
-
@collection.instance_variable_set('@entries', [@entry_invalid])
|
133
|
-
@entry_invalid.should_receive(:valid_for?).with(@request).and_return(false)
|
134
|
-
@collection[@request].should == nil
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'should store an entry' do
|
138
|
-
@collection[@request] = @valid_resp
|
139
|
-
@collection.instance_variable_get("@entries").should have(1).items
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'should replace an existing entry if the existing entry matches the request' do
|
143
|
-
new_resp = stub('new_resp', :authoritative= => nil, :header => {})
|
144
|
-
|
145
|
-
@collection[@request] = @valid_resp
|
146
|
-
@collection[@request] = new_resp
|
147
|
-
|
148
|
-
@collection.instance_variable_get("@entries").map{|it| it.response}.should include(new_resp)
|
149
|
-
@collection.instance_variable_get("@entries").map{|it| it.response}.should_not include(@valid_resp)
|
150
|
-
end
|
151
|
-
|
152
|
-
end
|
153
|
-
|
154
|
-
describe Resourceful::CacheEntry do
|
155
|
-
before do
|
156
|
-
@entry = Resourceful::CacheEntry.new(
|
157
|
-
mock('original_request', :header => {'Accept' => 'text/plain'} ,
|
158
|
-
:request_time => Time.utc(2008,5,16,0,0,0), :uri => 'http://foo.invalid'),
|
159
|
-
mock('response', :header => {'Vary' => 'Accept'})
|
160
|
-
)
|
161
|
-
|
162
|
-
@request = mock('request', :uri => 'http://foo.invalid')
|
163
|
-
end
|
164
|
-
|
165
|
-
describe "#valid_for?(a_request)" do
|
166
|
-
it "should true for request to URI w/ matching header " do
|
167
|
-
@entry.valid_for?(mock("new_request",
|
168
|
-
:uri => 'http://foo.invalid',
|
169
|
-
:header => {'Accept' => 'text/plain'})).should be_true
|
170
|
-
end
|
171
|
-
|
172
|
-
it "should false for requests against different URIs even if headers match" do
|
173
|
-
@entry.valid_for?(mock("new_request", :uri => 'http://bar.invalid',
|
174
|
-
:header => {'Accept' => 'text/plain'})).should be_false
|
175
|
-
end
|
176
|
-
|
177
|
-
it "should false for requests where headers don't match" do
|
178
|
-
@entry.valid_for?(mock("new_request", :uri => 'http://foo.invalid',
|
179
|
-
:header => {'Accept' => 'application/octet-stream'})).should be_false
|
180
|
-
end
|
181
|
-
|
182
|
-
it "should be false if request has a varying header and the original request was missing that header" do
|
183
|
-
entry = Resourceful::CacheEntry.new(
|
184
|
-
mock('original_request', :header => {},
|
185
|
-
:request_time => Time.utc(2008,5,16,0,0,0), :uri => 'http://foo.invalid'),
|
186
|
-
mock('response', :header => {'Vary' => 'Accept'}))
|
187
|
-
|
188
|
-
entry.valid_for?(mock("new_request", :uri => 'http://foo.invalid',
|
189
|
-
:header => {'Accept' => 'text/plain'})).should be_false
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
describe '#select_request_headers' do
|
194
|
-
before do
|
195
|
-
@req_header = mock('header', :[] => nil)
|
196
|
-
@request = mock('request', :header => @req_header)
|
197
|
-
|
198
|
-
@resp_header = mock('header', :[] => nil)
|
199
|
-
@response = mock('response', :header => @resp_header)
|
200
|
-
end
|
201
|
-
|
202
|
-
it 'should select the request headers from the Vary header' do
|
203
|
-
@resp_header.should_receive(:[]).with('Vary')
|
204
|
-
@entry.select_request_headers(@request, @response)
|
205
|
-
end
|
206
|
-
|
207
|
-
it 'should pull the values from the request that match keys in the vary header' do
|
208
|
-
@resp_header.should_receive(:[]).with('Vary').twice.and_return(['foo', 'bar'])
|
209
|
-
@req_header.should_receive(:[]).with('foo').and_return('oof')
|
210
|
-
@req_header.should_receive(:[]).with('bar').and_return('rab')
|
211
|
-
|
212
|
-
header = @entry.select_request_headers(@request, @response)
|
213
|
-
header['foo'].should == 'oof'
|
214
|
-
header['bar'].should == 'rab'
|
215
|
-
end
|
216
|
-
|
217
|
-
it 'should return a new Header object' do
|
218
|
-
@entry.select_request_headers(@request, @response).should be_kind_of(Resourceful::Header)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
|