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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00c9816f80d3f4a4f700bf153c6e4b20eed4f28fc0fcbc59d3e6a9135dd864fb
4
- data.tar.gz: 1298dc1a38f6529db56b8d862524268908ac09a2825678450958955b2af8f096
3
+ metadata.gz: 8308a71aed66c20d85c6bc345b40c36f63d6362b562e2ce21ba11c002c25929a
4
+ data.tar.gz: 707c84e7c6b8d0f54f7f55142649db9623f0031197d0fe08f195ab96d9e2fe49
5
5
  SHA512:
6
- metadata.gz: 959cd511c16d707e18369d66527bcd33dcec3de70e093f906cc688c6c68f715e2e74eefc0777e97eb00070d7edb906505b2edfd2e77e05778d66c42d33597d6b
7
- data.tar.gz: 6acca89a4a35247611b553d293c627c053f7eba252ae2fa65ccc1fa07b85d5cedfb815e7924963fce288ecc113d98ca3c565426e604781aa0db2c83dd80482f6
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
- @directives['max-age'].to_i if @directives.key?('max-age')
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'] = @directives['max-age'].to_i - age if @directives.key?('max-age')
49
- @directives['s-maxage'] = @directives['s-maxage'].to_i - age if @directives.key?('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
- @directives['s-maxage'].to_i if @directives.key?('s-maxage')
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
- @directives['stale-while-revalidate'].to_i if @directives.key?('stale-while-revalidate')
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  class HttpCache
5
- VERSION = '2.8.0'
5
+ VERSION = '3.0.0'
6
6
  end
7
7
  end
@@ -314,9 +314,11 @@ module Faraday
314
314
  end
315
315
 
316
316
  def delete(request, response)
317
- headers = %w[Location Content-Location]
318
- headers.each do |header|
319
- url = response.headers[header]
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
 
@@ -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
@@ -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: 2.8.0
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: 2026-09-15 00:00:00.000000000 Z
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.2.0
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.5.22
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: