Skip to content

Commit

Permalink
Completed specs and code for assertions for subdomain route recogniti…
Browse files Browse the repository at this point in the history
…on and generation.
  • Loading branch information
mholling committed Aug 13, 2009
1 parent a9c9d61 commit 21b8e32
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 117 deletions.
2 changes: 1 addition & 1 deletion lib/subdomain_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
require 'subdomain_routes/url_writer'
require 'subdomain_routes/request'
require 'subdomain_routes/validations'
require 'subdomain_routes/routing_assertions'
require 'subdomain_routes/assertions'
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module SubdomainRoutes
module RoutingAssertions
def self.included(base)
[ :assert_recognizes, :recognized_request_for, :assert_generates ].each { |method| base.alias_method_chain method, :subdomains }
end

def assert_recognizes_with_subdomains(expected_options, path, extras={}, message=nil)
module RoutingAssertions
include SubdomainRoutes::RewriteSubdomainOptions

def assert_recognizes_with_host(expected_options, path, extras={}, message=nil)
# copied from Rails source, with modification to set the the supplied host on the request
if path.is_a? Hash
request_method = path[:method]
host = path[:host]
Expand All @@ -15,7 +14,7 @@ def assert_recognizes_with_subdomains(expected_options, path, extras={}, message
end
clean_backtrace do
ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
request = recognized_request_for(path, host, request_method)
request = recognized_request_for_with_host(path, host, request_method)

expected_options = expected_options.clone
extras.each_key { |key| expected_options.delete key } unless extras.nil?
Expand All @@ -28,10 +27,21 @@ def assert_recognizes_with_subdomains(expected_options, path, extras={}, message
assert_block(msg) { request.path_parameters == expected_options }
end
end

def assert_generates_with_host(expected_path, host, options, defaults={}, extras = {}, message=nil)
host_options = options.dup
rewrite_subdomain_options(host_options, host)
if host_options[:only_path] == false
expected_path.slice!(/^https?:\/\//)
msg = build_message(message, "The subdomain route for <?> changed the host to <?> but this did not match the URL <?>", options, host_options[:host], expected_path)
assert_block(msg) { expected_path.slice!(host_options[:host]) }
end
assert_generates(expected_path, options, defaults, extras, message)
end

private

def recognized_request_for_with_subdomains(path, host, request_method = nil)
def recognized_request_for_with_host(path, host, request_method = nil)
path = "/#{path}" unless path.first == '/'

# Assume given controller
Expand All @@ -42,16 +52,6 @@ def recognized_request_for_with_subdomains(path, host, request_method = nil)
ActionController::Routing::Routes.recognize(request)
request
end

include SubdomainRoutes::RewriteSubdomainOptions
def assert_generates_with_subdomains(expected_path, options, defaults={}, extras = {}, message=nil)
host_options = options.dup
rewrite_subdomain_options(host_options, expected_path[:host])
host_options.slice!(:only_path, :host)
if host_options[:only_path] == false
expected_path[:path] =
assert_generates_without_subdomains(expected_path[:path], options, defaults, extras, message)
end
end
end

Expand Down
53 changes: 53 additions & 0 deletions spec/assertions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'test/unit/testresult'
require 'spec_helper'

class UsersController < ActionController::Base
end

describe "routing assertions" do
before(:each) do
map_subdomain(:admin, :name => nil) do |admin|
admin.resources :users
end
@options = { :controller => "users", :action => "index", :subdomains => [ "admin" ] }
end

context "for recognition" do
it "should correctly succeed with a :host option and a subdomain route" do
test = lambda { assert_recognizes_with_host(@options, { :path => "/users", :host => "admin.example.com" }) }
test.should_not have_errors
test.should_not fail
end

it "should correctly fail with a :host option and a subdomain route" do
test = lambda { assert_recognizes_with_host(@options, { :path => "/users", :host => "www.example.com" } ) }
test.should have_errors
end
end

context "for generation" do
it "should correctly succeed with :host and :path options and a subdomain route which changes the subdomain" do
test = lambda { assert_generates_with_host("http://admin.example.com/users", "www.example.com", @options) }
test.should_not have_errors
test.should_not fail
end

it "should correctly fail with :host and :path options and a subdomain route which changes the subdomain" do
test = lambda { assert_generates_with_host("http://admin.example.com/users", "admin.example.com", @options) }
test.should_not have_errors
test.should fail
end

it "should correctly succeed with :host and :path options and a subdomain route which doesn't change the subdomain" do
test = lambda { assert_generates_with_host("/users", "admin.example.com", @options) }
test.should_not have_errors
test.should_not fail
end

it "should correctly fail with :host and :path options and a subdomain route which doesn't change the subdomain" do
test = lambda { assert_generates_with_host("/users", "www.example.com", @options) }
test.should_not have_errors
test.should fail
end
end
end
70 changes: 0 additions & 70 deletions spec/routing_assertions_spec.rb

This file was deleted.

39 changes: 12 additions & 27 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@

require 'subdomain_routes'

require 'action_controller/test_process'
require 'action_view/test_case'
require 'test_unit_matcher'

ActiveSupport::OptionMerger.send(:define_method, :options) { @options }


Spec::Runner.configure do |config|
config.before(:each) do
ActionController::Routing::Routes.clear!
SubdomainRoutes::Config.stub!(:domain_length).and_return(2)
end
config.include(SubdomainRoutes::TestUnitMatcher)
end

require 'action_controller/test_process'
require 'action_view/test_case'

ActiveSupport::OptionMerger.send(:define_method, :options) { @options }

def map_subdomain(*subdomains, &block)
ActionController::Routing::Routes.draw do |map|
map.subdomain(*subdomains, &block)
Expand All @@ -32,28 +35,24 @@ def recognize_path(request)
end

def in_controller_with_host(host, &block)
variables = instance_variables.inject([]) do |array, name|
array << [ name, instance_variable_get(name) ]
end
spec = self
Class.new(ActionView::TestCase::TestController) do
include Spec::Matchers
end.new.instance_eval do
request.host = host
variables.each { |name, value| instance_variable_set(name, value) }
copy_instance_variables_from(spec)
instance_eval(&block)
end
end

def in_object_with_host(host, &block)
variables = instance_variables.inject([]) do |array, name|
array << [ name, instance_variable_get(name) ]
end
spec = self
Class.new do
include Spec::Matchers
include ActionController::UrlWriter
end.new.instance_eval do
self.class.default_url_options = { :host => host }
variables.each { |name, value| instance_variable_set(name, value) }
copy_instance_variables_from(spec)
instance_eval(&block)
end
end
Expand All @@ -71,17 +70,3 @@ def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
end
end

class RoutingTestClass < ActionController::TestCase
end

def testing_routing(&block)
result = Test::Unit::TestResult.new
spec = self
RoutingTestClass.send :define_method, :test do
copy_instance_variables_from(spec)
instance_eval(&block)
end
RoutingTestClass.new(:test).run(result) {}
result
end
46 changes: 46 additions & 0 deletions spec/test_unit_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module SubdomainRoutes
module TestUnitMatcher
class Base
class TestCase < ActionController::TestCase
end

def initialize(count_method, messages, spec)
@count_method, @messages, @spec = count_method, messages, spec
end

def run_test(target)
result = Test::Unit::TestResult.new
spec = @spec
TestCase.send :define_method, :test do
copy_instance_variables_from(spec)
instance_eval(&target)
end
TestCase.new(:test).run(result) {}
result
end

def matches?(target)
@result = run_test(target)
@result_count = @result.send(@count_method)
@result_count > 0
end

def failure_message
"Expected #{@count_method.to_s.humanize.downcase} to be more than zero, got zero.\n"
end

def negative_failure_message
"Expected #{@count_method.to_s.humanize.downcase} to be zero, got #{@result_count}:\n" +
@result.instance_variable_get(@messages).map(&:inspect).join("\n")
end
end

def have_errors
Base.new(:error_count, :@errors, self)
end

def fail
Base.new(:failure_count, :@failures, self)
end
end
end
2 changes: 1 addition & 1 deletion spec/url_writing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class City < ActiveRecord::Base; end
it "should raise a routing error if no subdomain object is supplied to the named route" do
with_host "www.example.com" do
[ lambda { city_events_url }, lambda { city_event_url("id") } ].each do |lamb|
lamb.should raise_error(ActionController::RoutingError) { |e| e.message.should include ":city_id" }
lamb.should raise_error(ActionController::RoutingError) { |e| e.message.should include(":city_id") }
end
end
end
Expand Down

0 comments on commit 21b8e32

Please sign in to comment.