faraday-http-cache 2.8.0 → 3.0.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/lib/faraday/http_cache/cache_control.rb +19 -8
- data/lib/faraday/http_cache/version.rb +1 -1
- data/lib/faraday/http_cache.rb +5 -3
- data/spec/cache_control_spec.rb +27 -0
- data/spec/http_cache_spec.rb +27 -0
- metadata +4 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8308a71aed66c20d85c6bc345b40c36f63d6362b562e2ce21ba11c002c25929a
|
|
4
|
+
data.tar.gz: 707c84e7c6b8d0f54f7f55142649db9623f0031197d0fe08f195ab96d9e2fe49
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7cfea8ce8b26348fd53e4295aed88e46f3ac5f628d6b2ee65e0c939a034c789009e9bd403c1037a8c86b4c6f7f39fa3555d4acafc27db4c4a7219abb2c335917
|
|
7
|
+
data.tar.gz: 037b527d2ba46be6c9f961ba417fcc585f9bda4daf21eb01fd820f4c902b597a2f32df90863dc213ed0916fa1788a142a6b106d46c2bb5afd2314fb75767b6a2
|
|
@@ -34,9 +34,9 @@ module Faraday
|
|
|
34
34
|
|
|
35
35
|
# Internal: Gets the 'max-age' directive as an Integer.
|
|
36
36
|
#
|
|
37
|
-
# Returns nil if the 'max-age' directive isn't present.
|
|
37
|
+
# Returns nil if the 'max-age' directive isn't present or has no value.
|
|
38
38
|
def max_age
|
|
39
|
-
|
|
39
|
+
integer_directive('max-age')
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# Internal: Gets the 'max-age' directive as an Integer.
|
|
@@ -45,16 +45,16 @@ module Faraday
|
|
|
45
45
|
# if present to account for having to remove static age header when caching responses
|
|
46
46
|
def normalize_max_ages(age)
|
|
47
47
|
if age > 0
|
|
48
|
-
@directives['max-age'] =
|
|
49
|
-
@directives['s-maxage'] =
|
|
48
|
+
@directives['max-age'] = max_age - age if max_age
|
|
49
|
+
@directives['s-maxage'] = shared_max_age - age if shared_max_age
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
# Internal: Gets the 's-maxage' directive as an Integer.
|
|
54
54
|
#
|
|
55
|
-
# Returns nil if the 's-maxage' directive isn't present.
|
|
55
|
+
# Returns nil if the 's-maxage' directive isn't present or has no value.
|
|
56
56
|
def shared_max_age
|
|
57
|
-
|
|
57
|
+
integer_directive('s-maxage')
|
|
58
58
|
end
|
|
59
59
|
alias s_maxage shared_max_age
|
|
60
60
|
|
|
@@ -70,9 +70,10 @@ module Faraday
|
|
|
70
70
|
|
|
71
71
|
# Internal: Gets the 'stale-while-revalidate' directive as an Integer.
|
|
72
72
|
#
|
|
73
|
-
# Returns nil if the 'stale-while-revalidate' directive isn't present
|
|
73
|
+
# Returns nil if the 'stale-while-revalidate' directive isn't present or
|
|
74
|
+
# has no value.
|
|
74
75
|
def stale_while_revalidate
|
|
75
|
-
|
|
76
|
+
integer_directive('stale-while-revalidate')
|
|
76
77
|
end
|
|
77
78
|
|
|
78
79
|
# Internal: Gets the String representation for the cache directives.
|
|
@@ -98,6 +99,16 @@ module Faraday
|
|
|
98
99
|
|
|
99
100
|
private
|
|
100
101
|
|
|
102
|
+
# Internal: Reads a directive whose value must be an integer.
|
|
103
|
+
# A directive given without a value (a bare 'max-age') is parsed as
|
|
104
|
+
# true; treat it as absent instead of calling to_i on it.
|
|
105
|
+
#
|
|
106
|
+
# Returns the Integer value, or nil.
|
|
107
|
+
def integer_directive(name)
|
|
108
|
+
value = @directives[name]
|
|
109
|
+
value.to_i unless value.nil? || value == true
|
|
110
|
+
end
|
|
111
|
+
|
|
101
112
|
# Internal: Parses the Cache Control string to a Hash.
|
|
102
113
|
# Existing whitespace will be removed and the string is split on commas.
|
|
103
114
|
# For each part everything before a '=' will be treated as the key
|
data/lib/faraday/http_cache.rb
CHANGED
|
@@ -314,9 +314,11 @@ module Faraday
|
|
|
314
314
|
end
|
|
315
315
|
|
|
316
316
|
def delete(request, response)
|
|
317
|
-
|
|
318
|
-
headers
|
|
319
|
-
|
|
317
|
+
# A response cut short by a timeout or a reset can arrive without
|
|
318
|
+
# headers; there is still an entry to invalidate for the request URL.
|
|
319
|
+
headers = response.headers || {}
|
|
320
|
+
%w[Location Content-Location].each do |header|
|
|
321
|
+
url = headers[header]
|
|
320
322
|
@strategy.delete(url) if url
|
|
321
323
|
end
|
|
322
324
|
|
data/spec/cache_control_spec.rb
CHANGED
|
@@ -52,6 +52,23 @@ describe Faraday::HttpCache::CacheControl do
|
|
|
52
52
|
expect(cache_control.shared_max_age).to eq(600)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
+
it 'responds to #max_age with nil when the max-age directive has no value' do
|
|
56
|
+
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age')
|
|
57
|
+
expect(cache_control.max_age).to be_nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'responds to #shared_max_age with nil when the s-maxage directive has no value' do
|
|
61
|
+
cache_control = Faraday::HttpCache::CacheControl.new('public, s-maxage')
|
|
62
|
+
expect(cache_control.shared_max_age).to be_nil
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it 'normalizes max ages without raising when a directive has no value' do
|
|
66
|
+
cache_control = Faraday::HttpCache::CacheControl.new('max-age, s-maxage=600')
|
|
67
|
+
cache_control.normalize_max_ages(100)
|
|
68
|
+
expect(cache_control.max_age).to be_nil
|
|
69
|
+
expect(cache_control.shared_max_age).to eq(500)
|
|
70
|
+
end
|
|
71
|
+
|
|
55
72
|
it 'responds to #shared_max_age with nil when no s-maxage directive present' do
|
|
56
73
|
cache_control = Faraday::HttpCache::CacheControl.new('public')
|
|
57
74
|
expect(cache_control.shared_max_age).to be_nil
|
|
@@ -116,4 +133,14 @@ describe Faraday::HttpCache::CacheControl do
|
|
|
116
133
|
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60')
|
|
117
134
|
expect(cache_control.stale_while_revalidate).to be_nil
|
|
118
135
|
end
|
|
136
|
+
|
|
137
|
+
it 'responds to #stale_while_revalidate with 0 when directive is invalid true string value' do
|
|
138
|
+
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60, stale-while-revalidate=true')
|
|
139
|
+
expect(cache_control.stale_while_revalidate).to eq(0)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it 'responds to #stale_while_revalidate with nil when directive has no integer assignment' do
|
|
143
|
+
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60, stale-while-revalidate')
|
|
144
|
+
expect(cache_control.stale_while_revalidate).to be_nil
|
|
145
|
+
end
|
|
119
146
|
end
|
data/spec/http_cache_spec.rb
CHANGED
|
@@ -96,6 +96,33 @@ describe Faraday::HttpCache do
|
|
|
96
96
|
client.get('broken')
|
|
97
97
|
end
|
|
98
98
|
|
|
99
|
+
it 'still expires the request URL when the response has no headers' do
|
|
100
|
+
store = Faraday::HttpCache::MemoryStore.new
|
|
101
|
+
cached = Faraday.new(url: ENV['FARADAY_SERVER']) do |stack|
|
|
102
|
+
stack.use Faraday::HttpCache, store: store
|
|
103
|
+
stack.adapter ENV['FARADAY_ADAPTER'].to_sym
|
|
104
|
+
end
|
|
105
|
+
# Mimics an adapter that gives up mid-response: the env is completed
|
|
106
|
+
# with no status worth the name and no response headers at all.
|
|
107
|
+
headerless_adapter = Class.new(Faraday::Adapter) do
|
|
108
|
+
def call(env)
|
|
109
|
+
super
|
|
110
|
+
env.status = 0
|
|
111
|
+
env.response_headers = nil
|
|
112
|
+
env.response.finish(env)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
broken = Faraday.new(url: ENV['FARADAY_SERVER']) do |stack|
|
|
116
|
+
stack.use Faraday::HttpCache, store: store
|
|
117
|
+
stack.adapter headerless_adapter
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
cached.get('get')
|
|
121
|
+
broken.post('get')
|
|
122
|
+
|
|
123
|
+
expect(cached.get('get').body).to eq('2')
|
|
124
|
+
end
|
|
125
|
+
|
|
99
126
|
it 'expires entries for the "Location" header' do
|
|
100
127
|
client.get('get')
|
|
101
128
|
client.post('delete-with-location')
|
metadata
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: faraday-http-cache
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Lucas Mazza
|
|
8
8
|
- George Guimarães
|
|
9
9
|
- Gustavo Araujo
|
|
10
|
-
autorequire:
|
|
11
10
|
bindir: bin
|
|
12
11
|
cert_chain: []
|
|
13
|
-
date:
|
|
12
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
14
13
|
dependencies:
|
|
15
14
|
- !ruby/object:Gem::Dependency
|
|
16
15
|
name: faraday
|
|
@@ -68,7 +67,6 @@ homepage: https://github.com/sourcelevel/faraday-http-cache
|
|
|
68
67
|
licenses:
|
|
69
68
|
- Apache-2.0
|
|
70
69
|
metadata: {}
|
|
71
|
-
post_install_message:
|
|
72
70
|
rdoc_options: []
|
|
73
71
|
require_paths:
|
|
74
72
|
- lib
|
|
@@ -76,15 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
76
74
|
requirements:
|
|
77
75
|
- - ">="
|
|
78
76
|
- !ruby/object:Gem::Version
|
|
79
|
-
version: 3.
|
|
77
|
+
version: 3.3.0
|
|
80
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
79
|
requirements:
|
|
82
80
|
- - ">="
|
|
83
81
|
- !ruby/object:Gem::Version
|
|
84
82
|
version: '0'
|
|
85
83
|
requirements: []
|
|
86
|
-
rubygems_version: 3.
|
|
87
|
-
signing_key:
|
|
84
|
+
rubygems_version: 3.6.9
|
|
88
85
|
specification_version: 4
|
|
89
86
|
summary: A Faraday middleware that stores and validates cache expiration.
|
|
90
87
|
test_files:
|