faraday-http-cache 2.0.0 → 2.1.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/LICENSE +0 -2
- data/README.md +14 -6
- data/lib/faraday/http_cache.rb +7 -14
- data/lib/faraday/http_cache/request.rb +1 -1
- data/lib/faraday/http_cache/response.rb +1 -1
- data/lib/faraday/http_cache/storage.rb +1 -1
- data/spec/http_cache_spec.rb +13 -5
- data/spec/response_spec.rb +9 -1
- data/spec/storage_spec.rb +1 -1
- data/spec/support/test_app.rb +4 -0
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2147bf25b6776f94d77ce49cd1147a790978446
|
4
|
+
data.tar.gz: cd45d9ddd630c3b627595e32826629a5c1e1d848
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 432baf735bfa450affe7fa9a88aff70667a8a37b93fcbcb490ccd75f8b69498459716f273af9b32093f6435680a4f20f9d2d1ef9212d91646eba2ef53ad73745
|
7
|
+
data.tar.gz: ceb797b25268e5ef954684f7332ed790c49db1ca0f9709be87e4722a94553d806787932d5021e4829b65952be15982338266b377b65432da441584255f8712e1
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Faraday Http Cache
|
2
2
|
|
3
|
-
[](https://travis-ci.org/sourcelevel/faraday-http-cache)
|
4
4
|
|
5
5
|
a [Faraday](https://github.com/lostisland/faraday) middleware that respects HTTP cache,
|
6
6
|
by checking expiration and validation of the stored responses.
|
@@ -53,9 +53,16 @@ This type of store **might not be persisted across multiple processes or connect
|
|
53
53
|
so it is probably not suitable for most production environments.
|
54
54
|
Make sure that you configure a store that is suitable for you.
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
The stdlib `JSON` module is used for serialization by default, which can struggle with unicode
|
57
|
+
characters in responses. For example, if your JSON returns `"name": "Raül"` then you might see
|
58
|
+
errors like:
|
59
|
+
|
60
|
+
```
|
61
|
+
Response could not be serialized: "\xC3" from ASCII-8BIT to UTF-8. Try using Marshal to serialize.
|
62
|
+
```
|
63
|
+
|
64
|
+
For full unicode support, or if you expect to be dealing with images, you can use
|
65
|
+
[Marshal][marshal] instead. Alternatively you could use another json library like `oj` or `yajl-ruby`.
|
59
66
|
|
60
67
|
```ruby
|
61
68
|
client = Faraday.new do |builder|
|
@@ -120,7 +127,7 @@ end
|
|
120
127
|
|
121
128
|
## See it live
|
122
129
|
|
123
|
-
You can clone this repository, install
|
130
|
+
You can clone this repository, install its dependencies with Bundler (run `bundle install`) and
|
124
131
|
execute the files under the `examples` directory to see a sample of the middleware usage.
|
125
132
|
|
126
133
|
## What gets cached?
|
@@ -153,6 +160,7 @@ client.get('http://site/api/some-private-resource') # => will be cached
|
|
153
160
|
|
154
161
|
## License
|
155
162
|
|
156
|
-
Copyright (c) 2012-
|
163
|
+
Copyright (c) 2012-2018 Plataformatec.
|
164
|
+
Copyright (c) 2019 SourceLevel and contributors.
|
157
165
|
|
158
166
|
[marshal]: http://www.ruby-doc.org/core-2.0/Marshal.html
|
data/lib/faraday/http_cache.rb
CHANGED
@@ -19,7 +19,7 @@ module Faraday
|
|
19
19
|
#
|
20
20
|
# # Using the middleware with a simple client:
|
21
21
|
# client = Faraday.new do |builder|
|
22
|
-
# builder.
|
22
|
+
# builder.use :http_cache, store: my_store_backend
|
23
23
|
# builder.adapter Faraday.default_adapter
|
24
24
|
# end
|
25
25
|
#
|
@@ -61,7 +61,7 @@ module Faraday
|
|
61
61
|
# The response was cached and the server has validated it with a 304 response.
|
62
62
|
:valid,
|
63
63
|
|
64
|
-
# The response was
|
64
|
+
# The response was cached but was not revalidated by the server.
|
65
65
|
:invalid,
|
66
66
|
|
67
67
|
# No response was found in the cache.
|
@@ -70,8 +70,8 @@ module Faraday
|
|
70
70
|
# The response can't be cached.
|
71
71
|
:uncacheable,
|
72
72
|
|
73
|
-
# The request
|
74
|
-
:
|
73
|
+
# The request was cached but need to be revalidated by the server.
|
74
|
+
:must_revalidate,
|
75
75
|
].freeze
|
76
76
|
|
77
77
|
# Public: Initializes a new HttpCache middleware.
|
@@ -132,15 +132,7 @@ module Faraday
|
|
132
132
|
response = nil
|
133
133
|
|
134
134
|
if @request.cacheable?
|
135
|
-
response =
|
136
|
-
trace :bypass
|
137
|
-
@app.call(env).on_complete do |fresh_env|
|
138
|
-
response = Response.new(create_response(fresh_env))
|
139
|
-
store(response)
|
140
|
-
end
|
141
|
-
else
|
142
|
-
process(env)
|
143
|
-
end
|
135
|
+
response = process(env)
|
144
136
|
else
|
145
137
|
trace :unacceptable
|
146
138
|
response = @app.call(env)
|
@@ -194,10 +186,11 @@ module Faraday
|
|
194
186
|
|
195
187
|
return fetch(env) if entry.nil?
|
196
188
|
|
197
|
-
if entry.fresh?
|
189
|
+
if entry.fresh? && !@request.no_cache?
|
198
190
|
response = entry.to_response(env)
|
199
191
|
trace :fresh
|
200
192
|
else
|
193
|
+
trace :must_revalidate
|
201
194
|
response = validate(entry, env)
|
202
195
|
end
|
203
196
|
|
@@ -50,7 +50,7 @@ module Faraday
|
|
50
50
|
#
|
51
51
|
# Returns true if the response is fresh, otherwise false.
|
52
52
|
def fresh?
|
53
|
-
!cache_control.
|
53
|
+
!cache_control.no_cache? && ttl && ttl > 0
|
54
54
|
end
|
55
55
|
|
56
56
|
# Internal: Checks if the Response returned a 'Not Modified' status.
|
@@ -56,7 +56,7 @@ module Faraday
|
|
56
56
|
entries << entry
|
57
57
|
|
58
58
|
cache.write(key, entries)
|
59
|
-
rescue Encoding::UndefinedConversionError => e
|
59
|
+
rescue ::Encoding::UndefinedConversionError => e
|
60
60
|
warn "Response could not be serialized: #{e.message}. Try using Marshal to serialize."
|
61
61
|
raise e
|
62
62
|
end
|
data/spec/http_cache_spec.rb
CHANGED
@@ -180,9 +180,12 @@ describe Faraday::HttpCache do
|
|
180
180
|
end
|
181
181
|
|
182
182
|
context 'when the request has a "no-cache" directive' do
|
183
|
-
it '
|
184
|
-
client.get('
|
185
|
-
expect(client.get('
|
183
|
+
it 'revalidates the cache' do
|
184
|
+
expect(client.get('etag').body).to eq('1')
|
185
|
+
expect(client.get('etag', nil, 'Cache-Control' => 'no-cache').body).to eq('1')
|
186
|
+
|
187
|
+
expect(client.get('get', nil).body).to eq('2')
|
188
|
+
expect(client.get('etag', nil, 'Cache-Control' => 'no-cache').body).to eq('3')
|
186
189
|
end
|
187
190
|
|
188
191
|
it 'caches the response' do
|
@@ -224,7 +227,7 @@ describe Faraday::HttpCache do
|
|
224
227
|
|
225
228
|
it 'logs that the request with "Last-Modified" was revalidated' do
|
226
229
|
client.get('timestamped')
|
227
|
-
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /timestamped] valid, store') }
|
230
|
+
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /timestamped] must_revalidate, valid, store') }
|
228
231
|
expect(client.get('timestamped').body).to eq('1')
|
229
232
|
end
|
230
233
|
|
@@ -235,7 +238,7 @@ describe Faraday::HttpCache do
|
|
235
238
|
|
236
239
|
it 'logs that the request with "ETag" was revalidated' do
|
237
240
|
client.get('etag')
|
238
|
-
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /etag] valid, store') }
|
241
|
+
expect(logger).to receive(:debug) { |&block| expect(block.call).to eq('HTTP Cache: [GET /etag] must_revalidate, valid, store') }
|
239
242
|
expect(client.get('etag').body).to eq('1')
|
240
243
|
end
|
241
244
|
|
@@ -274,6 +277,11 @@ describe Faraday::HttpCache do
|
|
274
277
|
expect(first_vary).not_to eql(second_vary)
|
275
278
|
end
|
276
279
|
|
280
|
+
it 'caches non-stale response with "must-revalidate" directive' do
|
281
|
+
client.get('must-revalidate')
|
282
|
+
expect(client.get('must-revalidate').body).to eq('1')
|
283
|
+
end
|
284
|
+
|
277
285
|
it 'raises an error when misconfigured' do
|
278
286
|
expect {
|
279
287
|
client = Faraday.new(url: ENV['FARADAY_SERVER']) do |stack|
|
data/spec/response_spec.rb
CHANGED
@@ -89,8 +89,16 @@ describe Faraday::HttpCache::Response do
|
|
89
89
|
expect(response).not_to be_fresh
|
90
90
|
end
|
91
91
|
|
92
|
-
it 'is
|
92
|
+
it 'is fresh if the response contains "must-revalidate" and is not stale' do
|
93
93
|
date = (Time.now - 200).httpdate
|
94
|
+
headers = { 'Cache-Control' => 'public, max-age=23880, must-revalidate, no-transform', 'Date' => date }
|
95
|
+
response = Faraday::HttpCache::Response.new(response_headers: headers)
|
96
|
+
|
97
|
+
expect(response).to be_fresh
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'is not fresh if Cache Control has "must-revalidate" and is stale' do
|
101
|
+
date = (Time.now - 500).httpdate
|
94
102
|
headers = { 'Cache-Control' => 'max-age=400, must-revalidate', 'Date' => date }
|
95
103
|
response = Faraday::HttpCache::Response.new(response_headers: headers)
|
96
104
|
|
data/spec/storage_spec.rb
CHANGED
@@ -55,7 +55,7 @@ describe Faraday::HttpCache::Storage do
|
|
55
55
|
|
56
56
|
expect {
|
57
57
|
storage.write(request, response)
|
58
|
-
}.to raise_error(Encoding::UndefinedConversionError)
|
58
|
+
}.to raise_error(::Encoding::UndefinedConversionError)
|
59
59
|
expect(logger).to have_received(:warn).with(
|
60
60
|
'Response could not be serialized: "\xE2" from ASCII-8BIT to UTF-8. Try using Marshal to serialize.'
|
61
61
|
)
|
data/spec/support/test_app.rb
CHANGED
@@ -88,6 +88,10 @@ class TestApp < Sinatra::Base
|
|
88
88
|
[200, { 'Date' => settings.yesterday, 'Expires' => settings.yesterday }, increment_counter]
|
89
89
|
end
|
90
90
|
|
91
|
+
get '/must-revalidate' do
|
92
|
+
[200, { 'Date' => Time.now.httpdate, 'Cache-Control' => 'public, max-age=23880, must-revalidate, no-transform' }, increment_counter]
|
93
|
+
end
|
94
|
+
|
91
95
|
get '/timestamped' do
|
92
96
|
settings.counter += 1
|
93
97
|
header = settings.counter > 2 ? '1' : '2'
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-http-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Mazza
|
8
|
+
- George Guimarães
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2020-04-06 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: faraday
|
@@ -26,7 +27,7 @@ dependencies:
|
|
26
27
|
version: '0.8'
|
27
28
|
description: Middleware to handle HTTP caching
|
28
29
|
email:
|
29
|
-
- opensource@
|
30
|
+
- opensource@sourcelevel.io
|
30
31
|
executables: []
|
31
32
|
extensions: []
|
32
33
|
extra_rdoc_files: []
|
@@ -52,7 +53,7 @@ files:
|
|
52
53
|
- spec/support/test_app.rb
|
53
54
|
- spec/support/test_server.rb
|
54
55
|
- spec/validation_spec.rb
|
55
|
-
homepage: https://github.com/
|
56
|
+
homepage: https://github.com/sourcelevel/faraday-http-cache
|
56
57
|
licenses:
|
57
58
|
- Apache 2.0
|
58
59
|
metadata: {}
|
@@ -72,21 +73,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
73
|
version: '0'
|
73
74
|
requirements: []
|
74
75
|
rubyforge_project:
|
75
|
-
rubygems_version: 2.
|
76
|
+
rubygems_version: 2.6.8
|
76
77
|
signing_key:
|
77
78
|
specification_version: 4
|
78
79
|
summary: A Faraday middleware that stores and validates cache expiration.
|
79
80
|
test_files:
|
80
|
-
- spec/binary_spec.rb
|
81
|
-
- spec/cache_control_spec.rb
|
82
|
-
- spec/http_cache_spec.rb
|
83
|
-
- spec/instrumentation_spec.rb
|
84
|
-
- spec/json_spec.rb
|
85
|
-
- spec/request_spec.rb
|
86
|
-
- spec/response_spec.rb
|
87
81
|
- spec/spec_helper.rb
|
82
|
+
- spec/validation_spec.rb
|
83
|
+
- spec/json_spec.rb
|
84
|
+
- spec/instrumentation_spec.rb
|
88
85
|
- spec/storage_spec.rb
|
89
|
-
- spec/
|
86
|
+
- spec/http_cache_spec.rb
|
87
|
+
- spec/binary_spec.rb
|
90
88
|
- spec/support/test_app.rb
|
89
|
+
- spec/support/empty.png
|
91
90
|
- spec/support/test_server.rb
|
92
|
-
- spec/
|
91
|
+
- spec/request_spec.rb
|
92
|
+
- spec/cache_control_spec.rb
|
93
|
+
- spec/response_spec.rb
|