Permalink
Browse files

Completed specs and code for assertions for subdomain route recogniti…

…on and generation.
  • Loading branch information...
1 parent a9c9d61 commit 21b8e324c575af9baa4778d226c210558c7a6f03 @mholling committed Aug 13, 2009
View
2 lib/subdomain_routes.rb
@@ -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'
View
36 lib/subdomain_routes/routing_assertions.rb → lib/subdomain_routes/assertions.rb
@@ -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]
@@ -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?
@@ -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
@@ -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
View
53 spec/assertions_spec.rb
@@ -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
View
70 spec/routing_assertions_spec.rb
@@ -1,70 +0,0 @@
-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 assert_recognizes" do
- it "should correctly succeed with a :host option and a subdomain route" do
- result = testing_routing do
- assert_recognizes(@options, { :path => "/users", :host => "admin.example.com" })
- end
- result.error_count.should be_zero
- result.failure_count.should be_zero
- end
-
- it "should correctly fail with a :host option and a subdomain route" do
- result = testing_routing do
- assert_recognizes(@options, { :path => "/users", :host => "www.example.com" } )
- end
- result.error_count.should == 1
- end
- end
-
- context "for assert_generates" do
- it "should correctly succeed with :host and :path options and a subdomain route which changes the subdomain" do
- result = testing_routing do
- assert_generates({ :path => "http://admin.example.com/users", :host => "www.example.com" }, @options)
- end
- result.assertion_count.should > 0
- result.error_count.should be_zero
- puts result.inspect
- result.failure_count.should be_zero
- end
-
- it "should correctly fail with :host and :path options and a subdomain route which changes the subdomain" do
- result = testing_routing do
- assert_generates({ :path => "http://admin.example.com/users", :host => "admin.example.com" }, @options)
- end
- result.assertion_count.should > 0
- result.error_count.should be_zero
- result.failure_count.should == 1
- end
-
- it "should correctly succeed with :host and :path options and a subdomain route which doesn't change the subdomain" do
- result = testing_routing do
- assert_generates({ :path => "/users", :host => "admin.example.com" }, @options)
- end
- result.assertion_count.should > 0
- result.error_count.should be_zero
- result.failure_count.should be_zero
- end
-
- it "should correctly fail with :host and :path options and a subdomain route which doesn't change the subdomain" do
- result = testing_routing do
- assert_generates({ :path => "/users", :host => "www.example.com" }, @options)
- end
- result.assertion_count.should > 0
- result.error_count.should be_zero
- result.failure_count.should == 1
- end
- end
-end
View
39 spec/spec_helper.rb
@@ -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)
@@ -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
@@ -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
View
46 spec/test_unit_matcher.rb
@@ -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
View
2 spec/url_writing_spec.rb
@@ -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

0 comments on commit 21b8e32

Please sign in to comment.