Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created June 2, 2012 18:04
Show Gist options
  • Save pmarreck/2859382 to your computer and use it in GitHub Desktop.
Save pmarreck/2859382 to your computer and use it in GitHub Desktop.
An attempt to test a railtie which installs rack middlewares
require 'test/unit' unless defined? Test::Unit
# require_relative '../../lib/object_utils' unless defined? ObjectUtils
# require_relative '../../lib/hash_utils' unless defined? HashUtils
# require_relative '../../lib/array_utils' unless defined? ArrayUtils
require 'rack/test' unless defined? Rack::Test
# require 'active_support/all' unless defined? ActiveSupport
# require 'rack'
# require 'action_dispatch'
# require 'action_controller'
require 'ap'
# require 'rails' unless defined? Rails
ENV['RACK_ENV'] = 'test'
# set :environment, :test
require 'rails/application'
require File.expand_path('../../../config/environment', __FILE__)
# require_relative '../../lib/session_loss_destroyer' unless defined? SessionLossDestroyer
class SessionLossDestroyerTest < ActiveSupport::TestCase #Test::Unit::TestCase
include Rack::Test::Methods
# Rack::Test removed the host! method at some point recently. I like it so I am re-adding.
# This should set it for all subsequent requests.
def host!(hostname)
header "HTTP_HOST", hostname
header "SERVER_NAME", hostname
env['HTTP_HOST'] = hostname
end
def setup
# @app ||= Rack::Builder.new {
# map "/" do
# use ActionDispatch::Static, Dir['../../..']
# run Rails.application
# end
# }.to_app
# ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
# m.use ActionController::Failsafe
# m.use ActiveRecord::QueryCache
# m.use Rack::Head
# end
host! "testdomain.test.com"
end
def app
@app ||= ::ActionController::Dispatcher.new #lambda { |env| [200, {}, "Coolness"] }
# @app ||= Rack::Builder.new do
# map "/" do
# run Assistly::Application
# end
# end.to_app
end
def env
@env ||= {}
end
test 'sld_middleware_exists_interspersed_with_stock_mw' do
orig_mw_count = SessionLossDestroyer.config.original_middleware_stack.count
mw_count = app.config.middleware.count
assert_equal mw_count, (orig_mw_count*2)+1
# this middleware should be around all other middlewares
assert_equal orig_mw_count+1, app.config.middleware.count{|m| m==SessionLossDestroyer::Middleware}
# this middleware should be every other middleware
assert app.config.middleware.each_with_index.all?{|m,i| i.odd? ^ (m==SessionLossDestroyer::Middleware)}
end
test 'env_does_NOT_contain_desk_uuid_cookie_for_unmatched_requests' do
host! "someotherdomain.example.com"
request('/',env) do |last_response|
# assert last_response.ok?, "Response was not 200 OK, it was #{last_response.status}"
# ensure the cookie is NOT set for non-matching domains
assert !last_response.headers['Set-Cookie'], "There is a desk_uuid cookie being set on a host that is not matched in the test environment! The header is: #{last_response.headers['Set-Cookie']}"
end
end
test 'env_contains_desk_uuid_cookie_for_matched_requests' do
host! 'testdomain.test.com'
request('/',env) do |last_response|
# assert last_response.ok?, "Response was not 200 OK, it was #{last_response.status}"
# ensure the cookie is set for matching domains
assert (last_response.headers['Set-Cookie'] && last_response.headers['Set-Cookie'].match(/\bdesk_uuid=/)), "There ISN'T a desk_uuid cookie being set on a host that is matched in the test environment! The header is: #{last_response.headers.inspect}"
end
end
test 'env_contains_NO_trace_for_unmatched_requests' do
host! 'crazydomain.testfirst.com'
request('/',env) do |last_response|
assert env['desk.rack_trace'].blank?, "The desk.rack_trace env key exists for unmatched requests!"
end
end
test 'env_contains_valid_trace_for_matched_requests' do
host! 'testdomain.test.com'
request('/',env) do |last_response|
ap env
assert !env['desk.rack_trace'].blank?, "The desk.rack_trace env key does NOT exist for matched requests!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment