faraday-http-cache 0.2.1 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +32 -8
- data/lib/faraday/http_cache.rb +85 -20
- data/lib/faraday/http_cache/response.rb +5 -4
- data/lib/faraday/http_cache/storage.rb +16 -7
- data/spec/binary_spec.rb +18 -0
- data/spec/cache_control_spec.rb +43 -43
- data/spec/json_spec.rb +6 -8
- data/spec/middleware_spec.rb +100 -73
- data/spec/response_spec.rb +89 -89
- data/spec/spec_helper.rb +3 -0
- data/spec/storage_spec.rb +38 -19
- data/spec/support/empty.png +0 -0
- data/spec/support/test_app.rb +24 -13
- data/spec/support/test_server.rb +5 -5
- metadata +9 -4
data/spec/spec_helper.rb
CHANGED
@@ -21,8 +21,11 @@ RSpec.configure do |config|
|
|
21
21
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
22
22
|
config.run_all_when_everything_filtered = true
|
23
23
|
config.filter_run :focus
|
24
|
+
config.order = 'random'
|
24
25
|
|
25
26
|
config.after(:suite) do
|
26
27
|
server.stop
|
27
28
|
end
|
28
29
|
end
|
30
|
+
|
31
|
+
ActiveSupport::Deprecation.silenced = true
|
data/spec/storage_spec.rb
CHANGED
@@ -2,40 +2,59 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Faraday::HttpCache::Storage do
|
4
4
|
let(:request) do
|
5
|
-
{ :
|
5
|
+
{ method: :get, request_headers: {}, url: URI.parse('http://foo.bar/') }
|
6
6
|
end
|
7
7
|
|
8
|
-
let(:response) { double(:
|
8
|
+
let(:response) { double(serializable_hash: {}) }
|
9
9
|
|
10
10
|
let(:cache) { ActiveSupport::Cache.lookup_store }
|
11
11
|
|
12
|
-
|
12
|
+
let(:storage) { Faraday::HttpCache::Storage.new(store: cache) }
|
13
|
+
subject { storage }
|
13
14
|
|
14
15
|
describe 'Cache configuration' do
|
15
16
|
it 'lookups a ActiveSupport cache store' do
|
16
|
-
ActiveSupport::Cache.
|
17
|
-
Faraday::HttpCache::Storage.new(:file_store, '/tmp')
|
17
|
+
expect(ActiveSupport::Cache).to receive(:lookup_store).with(:file_store, ['/tmp'])
|
18
|
+
Faraday::HttpCache::Storage.new({ store: :file_store, store_options: ['/tmp'] })
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
21
22
|
describe 'storing responses' do
|
22
|
-
it 'writes the response json to the underlying cache using a digest as the key' do
|
23
|
-
json = MultiJson.dump(response.serializable_hash)
|
24
23
|
|
25
|
-
|
26
|
-
|
24
|
+
shared_examples 'serialization' do
|
25
|
+
it 'writes the response json to the underlying cache using a digest as the key' do
|
26
|
+
expect(cache).to receive(:write).with(cache_key, serialized)
|
27
|
+
subject.write(request, response)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with default serializer' do
|
32
|
+
let(:serialized) { MultiJson.dump(response.serializable_hash) }
|
33
|
+
let(:cache_key) { '503ac9f7180ca1cdec49e8eb73a9cc0b47c27325' }
|
34
|
+
it_behaves_like 'serialization'
|
27
35
|
end
|
36
|
+
|
37
|
+
context 'with Marshal serializer' do
|
38
|
+
let(:storage) { Faraday::HttpCache::Storage.new store: cache, serializer: Marshal }
|
39
|
+
let(:serialized) { Marshal.dump(response.serializable_hash) }
|
40
|
+
let(:cache_key) do
|
41
|
+
array = request.stringify_keys.to_a.sort
|
42
|
+
Digest::SHA1.hexdigest(Marshal.dump(array))
|
43
|
+
end
|
44
|
+
it_behaves_like 'serialization'
|
45
|
+
end
|
46
|
+
|
28
47
|
end
|
29
48
|
|
30
49
|
describe 'reading responses' do
|
31
|
-
it
|
32
|
-
subject.read(request).
|
50
|
+
it 'returns nil if the response is not cached' do
|
51
|
+
expect(subject.read(request)).to be_nil
|
33
52
|
end
|
34
53
|
|
35
54
|
it 'decodes a stored response' do
|
36
55
|
subject.write(request, response)
|
37
56
|
|
38
|
-
subject.read(request).
|
57
|
+
expect(subject.read(request)).to be_a(Faraday::HttpCache::Response)
|
39
58
|
end
|
40
59
|
end
|
41
60
|
|
@@ -48,13 +67,13 @@ describe Faraday::HttpCache::Storage do
|
|
48
67
|
'Expires' => 37.seconds.from_now.httpdate,
|
49
68
|
'Last-Modified' => 300.seconds.ago.httpdate
|
50
69
|
}
|
51
|
-
response = Faraday::HttpCache::Response.new(:
|
52
|
-
response.
|
70
|
+
response = Faraday::HttpCache::Response.new(response_headers: headers)
|
71
|
+
expect(response).to be_fresh
|
53
72
|
subject.write(request, response)
|
54
73
|
|
55
74
|
cached_response = subject.read(request)
|
56
|
-
cached_response.max_age.
|
57
|
-
cached_response.
|
75
|
+
expect(cached_response.max_age).to eq(34)
|
76
|
+
expect(cached_response).not_to be_fresh
|
58
77
|
end
|
59
78
|
|
60
79
|
it 'is fresh until cached and that 1 second elapses then the response is no longer fresh' do
|
@@ -62,13 +81,13 @@ describe Faraday::HttpCache::Storage do
|
|
62
81
|
'Date' => 39.seconds.ago.httpdate,
|
63
82
|
'Expires' => 40.seconds.from_now.httpdate,
|
64
83
|
}
|
65
|
-
response = Faraday::HttpCache::Response.new(:
|
66
|
-
response.
|
84
|
+
response = Faraday::HttpCache::Response.new(response_headers: headers)
|
85
|
+
expect(response).to be_fresh
|
67
86
|
subject.write(request, response)
|
68
87
|
|
69
88
|
sleep(1)
|
70
89
|
cached_response = subject.read(request)
|
71
|
-
cached_response.
|
90
|
+
expect(cached_response).not_to be_fresh
|
72
91
|
end
|
73
92
|
end
|
74
93
|
|
Binary file
|
data/spec/support/test_app.rb
CHANGED
@@ -11,7 +11,7 @@ class TestApp < Sinatra::Base
|
|
11
11
|
set :yesterday, 1.day.ago.httpdate
|
12
12
|
|
13
13
|
get '/ping' do
|
14
|
-
|
14
|
+
'PONG'
|
15
15
|
end
|
16
16
|
|
17
17
|
get '/clear' do
|
@@ -21,36 +21,42 @@ class TestApp < Sinatra::Base
|
|
21
21
|
end
|
22
22
|
|
23
23
|
get '/json' do
|
24
|
-
json = MultiJson.encode(:
|
24
|
+
json = MultiJson.encode(count: increment_counter.to_i)
|
25
25
|
[200, { 'Cache-Control' => 'max-age=400', 'Content-Type' => 'application/json' }, json]
|
26
26
|
end
|
27
27
|
|
28
|
+
get '/image' do
|
29
|
+
image = File.expand_path('../empty.png', __FILE__)
|
30
|
+
data = IO.binread(image)
|
31
|
+
[200, { 'Cache-Control' => 'max-age=400', 'Content-Type' => 'image/png' }, data]
|
32
|
+
end
|
33
|
+
|
28
34
|
post '/post' do
|
29
|
-
[200, { 'Cache-Control' => 'max-age=400' },
|
35
|
+
[200, { 'Cache-Control' => 'max-age=400' }, increment_counter]
|
30
36
|
end
|
31
37
|
|
32
38
|
get '/broken' do
|
33
|
-
[500, { 'Cache-Control' => 'max-age=400' },
|
39
|
+
[500, { 'Cache-Control' => 'max-age=400' }, increment_counter]
|
34
40
|
end
|
35
41
|
|
36
42
|
get '/get' do
|
37
|
-
[200, { 'Cache-Control' => 'max-age=200' },
|
43
|
+
[200, { 'Cache-Control' => 'max-age=200' }, increment_counter]
|
38
44
|
end
|
39
45
|
|
40
46
|
get '/private' do
|
41
|
-
[200, { 'Cache-Control' => 'private' },
|
47
|
+
[200, { 'Cache-Control' => 'private' }, increment_counter]
|
42
48
|
end
|
43
49
|
|
44
50
|
get '/dontstore' do
|
45
|
-
[200, { 'Cache-Control' => 'no-store' },
|
51
|
+
[200, { 'Cache-Control' => 'no-store' }, increment_counter]
|
46
52
|
end
|
47
53
|
|
48
54
|
get '/expires' do
|
49
|
-
[200, { 'Expires' => (Time.now + 10).httpdate },
|
55
|
+
[200, { 'Expires' => (Time.now + 10).httpdate }, increment_counter]
|
50
56
|
end
|
51
57
|
|
52
58
|
get '/yesterday' do
|
53
|
-
[200, { 'Date' => settings.yesterday, 'Expires' => settings.yesterday },
|
59
|
+
[200, { 'Date' => settings.yesterday, 'Expires' => settings.yesterday }, increment_counter]
|
54
60
|
end
|
55
61
|
|
56
62
|
get '/timestamped' do
|
@@ -58,9 +64,9 @@ class TestApp < Sinatra::Base
|
|
58
64
|
header = settings.counter > 2 ? '1' : '2'
|
59
65
|
|
60
66
|
if env['HTTP_IF_MODIFIED_SINCE'] == header
|
61
|
-
[304, {},
|
67
|
+
[304, {}, '']
|
62
68
|
else
|
63
|
-
[200, { 'Last-Modified' => header },
|
69
|
+
[200, { 'Last-Modified' => header }, increment_counter]
|
64
70
|
end
|
65
71
|
end
|
66
72
|
|
@@ -71,7 +77,12 @@ class TestApp < Sinatra::Base
|
|
71
77
|
if env['HTTP_IF_NONE_MATCH'] == tag
|
72
78
|
[304, { 'ETag' => tag, 'Cache-Control' => 'max-age=200', 'Date' => Time.now.httpdate, 'Expires' => (Time.now + 200).httpdate, 'Vary' => '*' }, ""]
|
73
79
|
else
|
74
|
-
[200, { 'ETag' => tag, 'Cache-Control' => 'max-age=0', 'Date' => settings.yesterday, 'Expires' => Time.now.httpdate, 'Vary' => 'Accept' },
|
80
|
+
[200, { 'ETag' => tag, 'Cache-Control' => 'max-age=0', 'Date' => settings.yesterday, 'Expires' => Time.now.httpdate, 'Vary' => 'Accept' }, increment_counter]
|
75
81
|
end
|
76
82
|
end
|
77
|
-
|
83
|
+
|
84
|
+
# Increments the 'requests' counter to act as a newly processed response.
|
85
|
+
def increment_counter
|
86
|
+
(settings.requests += 1).to_s
|
87
|
+
end
|
88
|
+
end
|
data/spec/support/test_server.rb
CHANGED
@@ -26,9 +26,9 @@ class TestServer
|
|
26
26
|
log = File.open('log/test.log', 'w+')
|
27
27
|
log.sync = true
|
28
28
|
webrick_opts = {
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
29
|
+
Port: @port,
|
30
|
+
Logger: WEBrick::Log::new(log),
|
31
|
+
AccessLog: [[log, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']]
|
32
32
|
}
|
33
33
|
Rack::Handler::WEBrick.run(TestApp, webrick_opts)
|
34
34
|
end
|
@@ -51,7 +51,7 @@ class TestServer
|
|
51
51
|
begin
|
52
52
|
server_pings += 1
|
53
53
|
sleep 0.05
|
54
|
-
abort
|
54
|
+
abort 'test server did not managed to start' if server_pings >= 50
|
55
55
|
end until responsive.call('/ping')
|
56
56
|
end
|
57
57
|
|
@@ -61,4 +61,4 @@ class TestServer
|
|
61
61
|
ensure
|
62
62
|
server.close if server
|
63
63
|
end
|
64
|
-
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-http-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Mazza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -66,16 +66,19 @@ files:
|
|
66
66
|
- lib/faraday/http_cache/storage.rb
|
67
67
|
- lib/faraday/http_cache.rb
|
68
68
|
- lib/faraday-http-cache.rb
|
69
|
+
- spec/binary_spec.rb
|
69
70
|
- spec/cache_control_spec.rb
|
70
71
|
- spec/json_spec.rb
|
71
72
|
- spec/middleware_spec.rb
|
72
73
|
- spec/response_spec.rb
|
73
74
|
- spec/spec_helper.rb
|
74
75
|
- spec/storage_spec.rb
|
76
|
+
- spec/support/empty.png
|
75
77
|
- spec/support/test_app.rb
|
76
78
|
- spec/support/test_server.rb
|
77
79
|
homepage: https://github.com/plataformatec/faraday-http-cache
|
78
|
-
licenses:
|
80
|
+
licenses:
|
81
|
+
- Apache 2.0
|
79
82
|
metadata: {}
|
80
83
|
post_install_message:
|
81
84
|
rdoc_options: []
|
@@ -93,16 +96,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
96
|
version: '0'
|
94
97
|
requirements: []
|
95
98
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.0.
|
99
|
+
rubygems_version: 2.0.3
|
97
100
|
signing_key:
|
98
101
|
specification_version: 4
|
99
102
|
summary: A Faraday middleware that stores and validates cache expiration.
|
100
103
|
test_files:
|
104
|
+
- spec/binary_spec.rb
|
101
105
|
- spec/cache_control_spec.rb
|
102
106
|
- spec/json_spec.rb
|
103
107
|
- spec/middleware_spec.rb
|
104
108
|
- spec/response_spec.rb
|
105
109
|
- spec/spec_helper.rb
|
106
110
|
- spec/storage_spec.rb
|
111
|
+
- spec/support/empty.png
|
107
112
|
- spec/support/test_app.rb
|
108
113
|
- spec/support/test_server.rb
|