Skip to content

Commit

Permalink
* Add Rack::Response::Helpers#etag and etag=. Use this for
Browse files Browse the repository at this point in the history
setting etag values on the response.
  • Loading branch information
tenderlove committed Oct 6, 2015
1 parent d938cb5 commit e2a8388
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
@@ -1,3 +1,8 @@
Tue Oct 6 13:12:21 2015 Aaron Patterson <tenderlove@ruby-lang.org>

* Add `Rack::Response::Helpers#etag` and `etag=`. Use this for
setting etag values on the response.

Sun Oct 3 18:25:03 2015 Jeremy Daer <jeremydaer@gmail.com>

* Introduce `Rack::Response::Helpers#add_header` to add a value to a
Expand Down
1 change: 1 addition & 0 deletions lib/rack.rb
Expand Up @@ -43,6 +43,7 @@ def self.release
SET_COOKIE = 'Set-Cookie'.freeze
TRANSFER_ENCODING = 'Transfer-Encoding'.freeze
HTTP_COOKIE = 'HTTP_COOKIE'.freeze
ETAG = 'ETag'.freeze

# HTTP method verbs
GET = 'GET'.freeze
Expand Down
3 changes: 2 additions & 1 deletion lib/rack/etag.rb
@@ -1,3 +1,4 @@
require 'rack'
require 'digest/md5'

module Rack
Expand All @@ -11,7 +12,7 @@ module Rack
# used when Etag is absent and a directive when it is present. The first
# defaults to nil, while the second defaults to "max-age=0, private, must-revalidate"
class ETag
ETAG_STRING = 'ETag'.freeze
ETAG_STRING = Rack::ETAG
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate".freeze

def initialize(app, no_cache_control = nil, cache_control = DEFAULT_CACHE_CONTROL)
Expand Down
11 changes: 10 additions & 1 deletion lib/rack/response.rb
Expand Up @@ -195,14 +195,23 @@ def set_cookie_header
def set_cookie_header= v
set_header SET_COOKIE, v
end

def etag
get_header ETAG
end

def etag= v
set_header ETAG, v
end
end

include Helpers

class Raw
include Helpers

attr_reader :status, :headers
attr_reader :headers
attr_accessor :status

def initialize status, headers
@status = status
Expand Down
4 changes: 2 additions & 2 deletions test/spec_conditional_get.rb
Expand Up @@ -33,7 +33,7 @@ def conditional_get(app)

it "set a 304 status and truncate body when If-None-Match hits" do
app = conditional_get(lambda { |env|
[200, {'Etag'=>'1234'}, ['TEST']] })
[200, {'ETag'=>'1234'}, ['TEST']] })

response = Rack::MockRequest.new(app).
get("/", 'HTTP_IF_NONE_MATCH' => '1234')
Expand All @@ -57,7 +57,7 @@ def conditional_get(app)
it "set a 304 status and truncate body when both If-None-Match and If-Modified-Since hits" do
timestamp = Time.now.httpdate
app = conditional_get(lambda { |env|
[200, {'Last-Modified'=>timestamp, 'Etag'=>'1234'}, ['TEST']] })
[200, {'Last-Modified'=>timestamp, 'ETag'=>'1234'}, ['TEST']] })

response = Rack::MockRequest.new(app).
get("/", 'HTTP_IF_MODIFIED_SINCE' => timestamp, 'HTTP_IF_NONE_MATCH' => '1234')
Expand Down
7 changes: 7 additions & 0 deletions test/spec_response.rb
Expand Up @@ -4,6 +4,13 @@
require 'stringio'

describe Rack::Response do
it 'has an etag method' do
response = Rack::Response.new
etag = 'foo'
response.etag = etag
assert_equal etag, response.etag
end

it "have sensible default values" do
response = Rack::Response.new
status, header, body = response.finish
Expand Down

0 comments on commit e2a8388

Please sign in to comment.