Skip to content

Commit

Permalink
Merge pull request #121 from seancribbs/bugfix/date-header-required
Browse files Browse the repository at this point in the history
Fixes missing Date header for relevant responses
  • Loading branch information
seancribbs committed Oct 2, 2013
2 parents b31e822 + 435b7d9 commit 1f11efd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/webmachine/decision/fsm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def respond(code, headers={})
end

ensure_content_length
ensure_date_header
end

# When tracing is disabled, this does nothing.
Expand Down
8 changes: 8 additions & 0 deletions lib/webmachine/decision/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'stringio'
require 'time'
require 'webmachine/streaming'
require 'webmachine/media_type'
require 'webmachine/quoted_string'
Expand Down Expand Up @@ -102,6 +103,13 @@ def ensure_content_length
end
end

# Ensures that responses have an appropriate Date header
def ensure_date_header
if (200..499).include?(response.code)
response.headers['Date'] ||= Time.now.httpdate
end
end

# Sets the Content-Length header on the response
def set_content_length
if response.body.respond_to?(:bytesize)
Expand Down
9 changes: 9 additions & 0 deletions spec/webmachine/decision/flow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
let(:default_resource) { resource_with }
let(:missing_resource) { missing_resource_with }

# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18:
# Origin servers MUST include a Date header field in all responses
# ... [except 1xx or 5xx]
after(:each) do
unless response.code < 200 || response.code >= 500
response.headers.should have_key('Date')
end
end

def resource_with(&block)
klass = Class.new(Webmachine::Resource) do
def to_html; "test resource"; end
Expand Down

0 comments on commit 1f11efd

Please sign in to comment.