diff --git a/lib/faraday/http_cache/cache_control.rb b/lib/faraday/http_cache/cache_control.rb index acb0349..1b05de7 100644 --- a/lib/faraday/http_cache/cache_control.rb +++ b/lib/faraday/http_cache/cache_control.rb @@ -70,9 +70,13 @@ def proxy_revalidate? # Internal: Gets the 'stale-while-revalidate' directive as an Integer. # - # Returns nil if the 'stale-while-revalidate' directive isn't present. + # Returns nil if the 'stale-while-revalidate' directive isn't present, or + # assigned as boolean. def stale_while_revalidate - @directives['stale-while-revalidate'].to_i if @directives.key?('stale-while-revalidate') + return unless @directives.key?('stale-while-revalidate') + return if @directives['stale-while-revalidate'] == true + + @directives['stale-while-revalidate'].to_i end # Internal: Gets the String representation for the cache directives. diff --git a/spec/cache_control_spec.rb b/spec/cache_control_spec.rb index 01c010b..a6fa284 100644 --- a/spec/cache_control_spec.rb +++ b/spec/cache_control_spec.rb @@ -116,4 +116,14 @@ cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60') expect(cache_control.stale_while_revalidate).to be_nil end + + it 'responds to #stale_while_revalidate with 0 when directive is invalid true string value' do + cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60, stale-while-revalidate=true') + expect(cache_control.stale_while_revalidate).to eq(0) + end + + it 'responds to #stale_while_revalidate with nil when directive has no integer assignment' do + cache_control = Faraday::HttpCache::CacheControl.new('public, max-age=60, stale-while-revalidate') + expect(cache_control.stale_while_revalidate).to be_nil + end end