Skip to content

Commit bb38ceb

Browse files
protect {boolean}.to_i raising on invalid stale-while-revalidate cache control (#144)
1 parent 221ccec commit bb38ceb

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎lib/faraday/http_cache/cache_control.rb‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ def proxy_revalidate?
7070

7171
# Internal: Gets the 'stale-while-revalidate' directive as an Integer.
7272
#
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+
# assigned as boolean.
7475
def stale_while_revalidate
75-
@directives['stale-while-revalidate'].to_i if @directives.key?('stale-while-revalidate')
76+
return unless @directives.key?('stale-while-revalidate')
77+
return if @directives['stale-while-revalidate'] == true
78+
79+
@directives['stale-while-revalidate'].to_i
7680
end
7781

7882
# Internal: Gets the String representation for the cache directives.

‎spec/cache_control_spec.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,14 @@
116116
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60')
117117
expect(cache_control.stale_while_revalidate).to be_nil
118118
end
119+
120+
it 'responds to #stale_while_revalidate with 0 when directive is invalid true string value' do
121+
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60, stale-while-revalidate=true')
122+
expect(cache_control.stale_while_revalidate).to eq(0)
123+
end
124+
125+
it 'responds to #stale_while_revalidate with nil when directive has no integer assignment' do
126+
cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60, stale-while-revalidate')
127+
expect(cache_control.stale_while_revalidate).to be_nil
128+
end
119129
end

0 commit comments

Comments
 (0)