Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/faraday/http_cache/cache_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions spec/cache_control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading