Permalink
Browse files

[develop]: Extended specs to handle the HTTP-cache cases, yet not pas…

…sing though - need to decide on best approach.
  • Loading branch information...
1 parent 66e3c11 commit 820a702d041e1c7f916211da46ae97c7536d9cd1 @grimen committed Oct 25, 2011
Showing with 206 additions and 115 deletions.
  1. +3 −2 TODO
  2. +3 −2 lib/rack/iframe.rb
  3. +4 −0 rack-iframe.gemspec
  4. +101 −111 spec/rack-iframe_spec.rb
  5. +95 −0 spec/spec_helper.rb
View
5 TODO
@@ -2,15 +2,16 @@
== HIGH-PRIO
-- [feature/issue]: Handle case where HTTP cache headers makes web servers strip P3P tag (based on W3C spec).
+x [feature/issue]: Handle case where HTTP cache headers makes web servers strip P3P tag (based on W3C spec).
-- [feature/issue]: handle case where IE-browsers deletes the cookie if status is 302 (potentially the solution on above squashes this too).
+- [feature/issue]: handle case where IE-browsers deletes the cookie if status is 304 (potentially the solution on above squashes this too).
== LOW-PRIO
- [refactor/enhancement]: Use 'useragent' gem to parse 'USER_AGENT'-header - https://rubygems.org/gems/useragent
+- [feature]: Track if P3P-headers should be sent via cookie. Reason: We can't assume that only bodies that contain <iframe>-elements needs the P3P header (or so I think right now).
== MAYBE
View
@@ -1,4 +1,5 @@
-require "rack/iframe/version"
+require 'rack'
+require 'rack/iframe/version'
module Rack
class Iframe
@@ -16,7 +17,7 @@ def call(env)
protected
def user_agent(env)
- env['HTTP_USER_AGENT'] || ''
+ env['HTTP_USER_AGENT']
end
def etag(env)
View
@@ -27,4 +27,8 @@ Gem::Specification.new do |s|
s.add_development_dependency 'guard-bundler'
s.add_development_dependency 'guard-minitest'
s.add_development_dependency 'rack-test'
+ s.add_development_dependency 'rack-cache'
+ s.add_development_dependency 'chronic'
+ s.add_development_dependency 'awesome_print'
+ s.add_development_dependency 'sinatra'
end
View
@@ -1,6 +1,4 @@
require 'spec_helper'
-require 'digest/md5'
-require 'time'
# == References:
# - http://tempe.st/tag/ruby-on-rails
@@ -23,168 +21,160 @@
describe "Middleware" do
before do
- @app_headers = {'Content-Type' => 'text/plain'}
- @app_body = %()
- @app = lambda { |env| [200, @app_headers, [@app_body]] } # TODO: Customize
+ # @app = app({})
+ @app = CachedApp.new # rack_cache(@app)
end
describe "without Rack::Iframe" do
before do
- @user_agents = [:ie, :safari, :chrome, :firefox, :opera]
+ @user_agents = all_user_agents
end
- it 'should not send P3P-headers' do
+ it 'should not have P3P headers' do
@user_agents.each do |user_agent|
- request = mock_request_from_user_agent(user_agent)
+ request = mock_request(user_agent)
+
response = @app.call(request)
+ status, headers, body = response
- headers(response).key?('P3P').must_equal false
+ headers.key?('P3P').must_equal false
end
end
end
describe "with Rack::Iframe" do
- describe "browsers: IE, Safari" do
+ describe "browsers that require the P3P header: IE, Safari" do
before do
@user_agents = [:ie, :safari]
end
describe "without any HTTP-cache headers" do
- it 'should send P3P-header' do
+ it 'should send P3P header - modified (200 OK)' do
@user_agents.each do |user_agent|
- request = mock_request_from_user_agent(user_agent)
+ request = mock_request(user_agent)
+
response = Rack::Iframe.new(@app).call(request)
+ status, headers, body = response
- headers(response).key?('P3P').must_equal true
- headers(response)['P3P'].must_equal %(CP="ALL DSP COR CURa ADMa DEVa OUR IND COM NAV")
+ headers['P3P'].must_equal %(CP="ALL DSP COR CURa ADMa DEVa OUR IND COM NAV")
+ status.must_equal 200 # modified
end
end
end
- describe "with HTTP-cache header: Etag" do
- # TODO
- end
+ # NOTE: P3P headers with HTTP-cache headers don't work well.
+
+ describe "with HTTP-cache headers" do
+ describe "If-None-Match (Etag)" do
+ it 'should send P3P header - modified (200 OK)' do
+ @user_agents.each do |user_agent|
+ @app = mock_app('Etag' => '123')
+
+ request = mock_request(user_agent, 'HTTP_IF_NONE_MATCH' => '123')
+
+ response = Rack::Iframe.new(@app).call(request)
+ status, headers, body = response
+
+ headers['P3P'].must_equal %(CP="ALL DSP COR CURa ADMa DEVa OUR IND COM NAV")
+ status.must_equal 200 # modified
+ end
+ end
+ end
+
+ describe "Last-Modified" do
+ it 'should send P3P header - modified (200 OK)' do
+ @user_agents.each do |user_agent|
+ @app = mock_app('Last-Modified' => Chronic.parse('0 minutes ago').rfc2822)
+
+ request = mock_request(user_agent, 'HTTP_IF_MODIFIED_SINCE' => Chronic.parse('1 minute ago').rfc2822)
- describe "with HTTP-cache header: Last-Modified" do
- # TODO
+ response = Rack::Iframe.new(@app).call(request)
+ status, headers, body = response
+
+ headers['P3P'].must_equal %(CP="ALL DSP COR CURa ADMa DEVa OUR IND COM NAV")
+ status.must_equal 200 # modified
+ end
+ end
+ end
end
end
- describe "browsers: Chrome, Firefox, Opera" do
+ describe "browsers that don't require the P3P header: Chrome, Firefox, Opera" do
before do
- @user_agents = [:chrome, :firefox, :opera]
+ @user_agents = all_user_agents - [:ie, :safari]
end
describe "without any HTTP-cache headers" do
- it 'should not send P3P-header' do
+ it 'should not send P3P header - modified (200 OK)' do
@user_agents.each do |user_agent|
- request = mock_request_from_user_agent(user_agent)
+ @app = mock_app()
+
+ request = mock_request(user_agent)
+
response = Rack::Iframe.new(@app).call(request)
+ status, headers, body = response
- headers(response).key?('P3P').must_equal false
+ headers.key?('P3P').must_equal false
+ status.must_equal 200 # modified
end
end
end
- describe "with HTTP-cache header: Etag" do
- # TODO
- end
+ describe "with HTTP-cache headers" do
+ describe "If-None-Match (Etag)" do
+ it 'should not send P3P header - not modified (304 Not Modified)' do
+ @user_agents.each do |user_agent|
+ @app = mock_app('Etag' => '123')
- describe "with HTTP-cache header: Last-Modified" do
- # TODO
- end
- end
- end
- end
+ request = mock_request(user_agent, 'HTTP_IF_NONE_MATCH' => '123')
- private
+ response = Rack::Iframe.new(@app).call(request)
+ status, headers, body = response
- def status(response)
- response[0]
- end
+ headers.key?('P3P').must_equal false
+ status.must_equal 304 # not modified
- def headers(response)
- response[1]
- end
+ # browser = Rack::Test::Session.new(Rack::MockSession.new(CachedApp))
+ # browser.get '/', {}, 'HTTP_IF_NONE_MATCH' => '123'
- def body(response)
- response[2]
- end
+ # browser.last_response.headers.key?('P3P').must_equal false
+ # browser.last_response.status.must_equal 304
+ end
+ end
+ end
- def bogus_etag
- Digest::MD5.hexdigest(Time.now.to_s)
- end
- alias :bogus_if_none_match :bogus_etag
+ describe "Last-Modified" do
+ it 'should not send P3P header - not modified (304 Not Modified)' do
+ @user_agents.each do |user_agent|
+ @app = mock_app('Last-Modified' => Chronic.parse('1 minute ago').rfc2822)
- def bogus_last_modified
- Time.now.rfc2822
- end
- alias :bogus_if_modified_since :bogus_last_modified
-
- def response_etag(response)
- if block_given?
- old_last_modified = headers(response)['Etag']
- headers(response)['Etag'] = bogus_etag
- yield
- headers(response)['Etag'] = old_last_modified
- else
- headers(response)['Etag'] = bogus_etag
- end
- end
+ request = mock_request(user_agent, 'HTTP_IF_MODIFIED_SINCE' => Chronic.parse('0 minutes ago').rfc2822)
- def response_last_modified(response)
- if block_given?
- old_last_modified = headers(response)['Last-Modified']
- headers(response)['Last-Modified'] = bogus_last_modified
- yield
- headers(response)['Last-Modified'] = old_last_modified
- else
- headers(response)['Last-Modified'] = bogus_last_modified
- end
- end
+ response = Rack::Iframe.new(@app).call(request)
+ status, headers, body = response
- def request_if_none_match(request)
- if block_given?
- old_last_modified = request.env['HTTP_IF_NONE_MATCH']
- request.env['HTTP_IF_NONE_MATCH'] = bogus_etag
- yield
- request.env['HTTP_IF_NONE_MATCH'] = old_last_modified
- else
- request.env['HTTP_IF_NONE_MATCH'] = bogus_etag
- end
- end
+ ap headers
+ headers.key?('P3P').must_equal false
+ status.must_equal 304 # not modified
- def request_if_modified_since(request)
- if block_given?
- old_last_modified = request.env['HTTP_IF_MODIFIED_SINCE']
- request.env['HTTP_IF_MODIFIED_SINCE'] = bogus_etag
- yield
- request.env['HTTP_IF_MODIFIED_SINCE'] = old_last_modified
- else
- request.env['HTTP_IF_MODIFIED_SINCE'] = bogus_etag
- end
- end
+ # response = Rack::Iframe.new(@app).call(request)
+ # status, headers, body = response
- # TODO: Use "more real" HTTP_USER_AGENT values.
- def user_agent_string(id)
- case id
- when :ie
- 'MSIE'
- when :safari
- 'Safari'
- when :chrome
- 'Chrome'
- when :opera
- 'Opera'
- when :firefox
- 'Firefox'
- else
- raise "Define mising browser agent: #{id.inspect}"
- end
- end
+ # ap headers
+ # headers.key?('P3P').must_equal false
+ # status.must_equal 304 # not modified
+
+ # browser = Rack::Test::Session.new(Rack::MockSession.new(CachedApp))
+ # browser.get '/', {}, 'HTTP_IF_MODIFIED_SINCE' => Chronic.parse('1 minute ago')
- def mock_request_from_user_agent(user_agent_key)
- Rack::MockRequest.env_for('/', {'HTTP_USER_AGENT' => user_agent_string(user_agent_key)})
+ # browser.last_response.headers.key?('P3P').must_equal false
+ # browser.last_response.status.must_equal 200
+ end
+ end
+ end
+ end
+ end
end
+ end
end
Oops, something went wrong.

0 comments on commit 820a702

Please sign in to comment.