cryx-cacheability 0.1.0 → 0.1.1
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/VERSION.yml +1 -1
- data/lib/cacheability/restclient.rb +16 -6
- data/spec/cacheability_spec.rb +7 -3
- data/spec/spec_helper.rb +1 -1
- metadata +4 -3
data/VERSION.yml
CHANGED
@@ -7,8 +7,12 @@ module RestClient
|
|
7
7
|
class MockHTTPResponse
|
8
8
|
attr_reader :code, :headers, :body
|
9
9
|
def initialize(rack_response)
|
10
|
-
@code, @headers,
|
10
|
+
@code, @headers, io = rack_response
|
11
|
+
@body = ""
|
12
|
+
io.each{ |block| @body << block }
|
13
|
+
io.close if io.respond_to?(:close)
|
11
14
|
end
|
15
|
+
|
12
16
|
def to_hash
|
13
17
|
@headers
|
14
18
|
end
|
@@ -37,13 +41,20 @@ module RestClient
|
|
37
41
|
"PATH_INFO" => path_info,
|
38
42
|
"QUERY_STRING" => uri.query,
|
39
43
|
"SERVER_NAME" => uri.host,
|
40
|
-
"SERVER_PORT" => uri.port.to_s
|
44
|
+
"SERVER_PORT" => uri.port.to_s,
|
45
|
+
"rack.version" => Rack::VERSION,
|
46
|
+
"rack.run_once" => false,
|
47
|
+
"rack.multithread" => true,
|
48
|
+
"rack.multiprocess" => true,
|
49
|
+
"rack.url_scheme" => uri.scheme,
|
50
|
+
"rack.input" => StringIO.new,
|
51
|
+
"rack.errors" => StringIO.new # Rack-Cache writes errors into this field
|
41
52
|
}
|
42
53
|
debeautify_headers(additional_headers).each do |key, value|
|
43
54
|
env.merge!("HTTP_"+key.to_s.gsub("-", "_").upcase => value)
|
44
55
|
end
|
45
56
|
response = MockHTTPResponse.new(cache.call(env))
|
46
|
-
RestClient::Response.new(response.body
|
57
|
+
RestClient::Response.new(response.body, response)
|
47
58
|
else
|
48
59
|
super(additional_headers)
|
49
60
|
end
|
@@ -66,9 +77,8 @@ module RestClient
|
|
66
77
|
response = get(debeautify_headers(http_headers), pass_through_cache=false)
|
67
78
|
response.headers.delete(:x_content_digest) # don't know why, but it seems to make the validation fail if kept...
|
68
79
|
[response.code, debeautify_headers( response.headers ), response.to_s]
|
69
|
-
rescue RestClient::NotModified
|
70
|
-
|
71
|
-
[304, {}, ""]
|
80
|
+
rescue RestClient::NotModified => e
|
81
|
+
[304, e.response.to_hash, ""]
|
72
82
|
end
|
73
83
|
end
|
74
84
|
end
|
data/spec/cacheability_spec.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
require 'restclient'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/cacheability/restclient'
|
4
|
+
|
3
5
|
|
4
6
|
describe RestClient::CacheableResource do
|
5
7
|
require File.dirname(__FILE__) + '/../lib/cacheability/restclient'
|
@@ -18,6 +20,8 @@ describe RestClient::CacheableResource do
|
|
18
20
|
describe "correctly instantiated" do
|
19
21
|
before do
|
20
22
|
@mock_cache = mock('rack-cache singleton')
|
23
|
+
@mock_rack_errors = mock('string io')
|
24
|
+
@mock_304_net_http_response = mock('http response', :code => 304, :body => "", :to_hash => {'header1' => 'value1'})
|
21
25
|
Rack::Cache.stub!(:new).and_return(@mock_cache)
|
22
26
|
@env = {
|
23
27
|
'REQUEST_METHOD' => 'GET',
|
@@ -31,7 +35,7 @@ describe RestClient::CacheableResource do
|
|
31
35
|
end
|
32
36
|
|
33
37
|
it "should pass through the cache" do
|
34
|
-
@resource.cache.should_receive(:call).with( hash_including( @env.merge({'HTTP_ADDITIONAL_HEADER' => 'whatever'}) ) )
|
38
|
+
@resource.cache.should_receive(:call).with( hash_including( @env.merge({'HTTP_ADDITIONAL_HEADER' => 'whatever'}) ) ).and_return([200, {}, "response body"])
|
35
39
|
@resource['/?q1=a&q2=b'].get(:additional_header => 'whatever')
|
36
40
|
end
|
37
41
|
|
@@ -48,9 +52,9 @@ describe RestClient::CacheableResource do
|
|
48
52
|
end
|
49
53
|
|
50
54
|
it "should return a 304 not modified response if the call to the backend returned a 304 not modified response" do
|
51
|
-
@resource.should_receive(:get).with({'ADDITIONAL-HEADER' => 'whatever'}, false).and_raise(RestClient::NotModified)
|
55
|
+
@resource.should_receive(:get).with({'ADDITIONAL-HEADER' => 'whatever'}, false).and_raise(RestClient::NotModified.new(@mock_304_net_http_response))
|
52
56
|
response = @resource.call(@env.merge({'HTTP_ADDITIONAL_HEADER' => 'whatever'}))
|
53
|
-
response.should == [304, {}, ""]
|
57
|
+
response.should == [304, {"header1"=>"value1"}, ""]
|
54
58
|
end
|
55
59
|
|
56
60
|
it "should render a RestClient::Response even when the data is coming from the cache" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cryx-cacheability
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Rohr
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
-
description:
|
25
|
+
description: A gem that makes client-side caching of HTTP requests a no-brainer. It is built upon the Rack:Cache gem from Ryan Tomayko.
|
26
26
|
email: cyril.rohr@irisa.fr
|
27
27
|
executables: []
|
28
28
|
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- lib/cacheability.rb
|
38
38
|
- spec/cacheability_spec.rb
|
39
39
|
- spec/spec_helper.rb
|
40
|
+
- spec/test.rb
|
40
41
|
has_rdoc: true
|
41
42
|
homepage: http://github.com/cryx/cacheability
|
42
43
|
post_install_message:
|