Skip to content

Commit

Permalink
Fixing specs and adding other test cases.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Bleigh <michael@intridea.com>
  • Loading branch information
eric authored and Michael Bleigh committed May 26, 2009
1 parent 24866ce commit dab049b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
19 changes: 12 additions & 7 deletions lib/subdomain-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ def self.tld_size=(value)

# Is the current subdomain either nil or a mirror?
def self.has_subdomain?(subdomain)
!subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
subdomain != false && !subdomain.blank? && !SubdomainFu.mirrors.include?(subdomain)
end

# Is the subdomain a preferred mirror
def self.preferred_mirror?(subdomain)
subdomain == SubdomainFu.preferred_mirror || SubdomainFu.preferred_mirror.nil?
end

# Gets the subdomain from the host based on the TLD size
Expand All @@ -50,7 +55,7 @@ def self.host_without_subdomain(host)
# Rewrites the subdomain of the host unless they are equivalent (i.e. mirrors of each other)
def self.rewrite_host_for_subdomains(subdomain, host)
unless needs_rewrite?(subdomain, host)
if has_subdomain?(subdomain) || (subdomain_from(host) == SubdomainFu.preferred_mirror) || (!has_subdomain?(subdomain) && SubdomainFu.preferred_mirror == nil)
if has_subdomain?(subdomain) || preferred_mirror?(subdomain_from(host))
host
else
change_subdomain_of_host(SubdomainFu.preferred_mirror, host)
Expand All @@ -70,14 +75,14 @@ def self.change_subdomain_of_host(subdomain, host)
# Is this subdomain equivalent to the subdomain found in this host string?
def self.same_subdomain?(subdomain, host)
subdomain = nil unless subdomain
(subdomain == SubdomainFu.subdomain_from(host)) ||
(!SubdomainFu.has_subdomain?(subdomain) && !SubdomainFu.has_subdomain?(SubdomainFu.subdomain_from(host)))
(subdomain == subdomain_from(host)) ||
(!has_subdomain?(subdomain) && !has_subdomain?(subdomain_from(host)))
end

def self.needs_rewrite?(subdomain, host)
def self.needs_rewrite?(subdomain, host)
return false if subdomain.nil?
subdomain = nil if subdomain.blank?
(!has_subdomain?(subdomain) && subdomain != SubdomainFu.preferred_mirror && SubdomainFu.preferred_mirror != nil) ||

(!has_subdomain?(subdomain) && preferred_mirror?(subdomain) && !preferred_mirror?(subdomain_from(host))) ||
!same_subdomain?(subdomain, host)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/subdomain_fu/url_rewriter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UrlRewriter #:nodoc:
private

def rewrite_url_with_subdomains(options)
unless SubdomainFu.needs_rewrite?(options[:subdomain], (options[:host] || @request.host_with_port))
unless SubdomainFu.needs_rewrite?(options[:subdomain], options[:host] || @request.host_with_port)
options.delete(:subdomain)
else
options[:only_path] = false
Expand Down
50 changes: 49 additions & 1 deletion spec/subdomain_fu_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "SubdomainFu" do
before do
SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
SubdomainFu.preferred_mirror = nil
end

describe "TLD Sizes" do
before do
SubdomainFu.tld_sizes = SubdomainFu::DEFAULT_TLD_SIZES.dup
Expand Down Expand Up @@ -37,6 +42,7 @@
it "shoud be false for a nil or blank subdomain" do
SubdomainFu.has_subdomain?("").should be_false
SubdomainFu.has_subdomain?(nil).should be_false
SubdomainFu.has_subdomain?(false).should be_false
end
end

Expand All @@ -63,7 +69,19 @@
SubdomainFu.host_without_subdomain("awesome.localhost:3000").should == "localhost:3000"
SubdomainFu.host_without_subdomain("something.awful.localhost:3000").should == "localhost:3000"
end


describe "#preferred_mirror?" do
describe "when preferred_mirror is false" do
before do
SubdomainFu.preferred_mirror = false
end

it "should return true for false" do
SubdomainFu.preferred_mirror?(false).should be_true
end
end
end

describe "#rewrite_host_for_subdomains" do
it "should not change the same subdomain" do
SubdomainFu.rewrite_host_for_subdomains("awesome","awesome.localhost").should == "awesome.localhost"
Expand All @@ -84,6 +102,16 @@
it "should not remove the subdomain if passed false when it is a mirror" do
SubdomainFu.rewrite_host_for_subdomains(false,"www.localhost").should == "www.localhost"
end

describe "when preferred_mirror is false" do
before do
SubdomainFu.preferred_mirror = false
end

it "should remove the subdomain if passed false when it is a mirror" do
SubdomainFu.rewrite_host_for_subdomains(false,"www.localhost").should == "localhost"
end
end
end

describe "#change_subdomain_of_host" do
Expand Down Expand Up @@ -130,4 +158,24 @@
it { SubdomainFu.same_subdomain?(nil,"www.localhost").should be_true }
it { SubdomainFu.same_subdomain?("www","awesome.localhost").should be_false }
end

describe "#needs_rewrite?" do
it { SubdomainFu.needs_rewrite?("www","www.localhost").should be_false }
it { SubdomainFu.needs_rewrite?("www","localhost").should be_false }
it { SubdomainFu.needs_rewrite?("awesome","www.localhost").should be_true }
it { SubdomainFu.needs_rewrite?("cool","awesome.localhost").should be_true }
it { SubdomainFu.needs_rewrite?(nil,"www.localhost").should be_false }
it { SubdomainFu.needs_rewrite?(nil,"awesome.localhost").should be_false }
it { SubdomainFu.needs_rewrite?(false,"awesome.localhost").should be_true }
it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_false }
it { SubdomainFu.needs_rewrite?("www","awesome.localhost").should be_true }

describe "when preferred_mirror is false" do
before do
SubdomainFu.preferred_mirror = false
end

it { SubdomainFu.needs_rewrite?(false,"www.localhost").should be_true }
end
end
end
28 changes: 24 additions & 4 deletions spec/url_rewriter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe "SubdomainFu URL Writing" do
before do
SubdomainFu.tld_size = 1
SubdomainFu.preferred_mirror = nil
default_url_options[:host] = "testapp.com"
end

Expand Down Expand Up @@ -47,7 +48,12 @@
default_url_options[:host] = "awesome.testapp.com"
needs_subdomain_path(:subdomain => "awesome").should == "/needs_subdomain"
end


it "should force the full url if it's a different subdomain" do
default_url_options[:host] = "awesome.testapp.com"
needs_subdomain_path(:subdomain => "crazy").should == "http://crazy.testapp.com/needs_subdomain"
end

it "should not force the full url if the current subdomain is nil and so is the target" do
needs_subdomain_path(:subdomain => nil).should == "/needs_subdomain"
end
Expand All @@ -69,14 +75,28 @@
foo_path(:id => "something", :subdomain => false).should == "http://testapp.com/foos/something"
end

it "should work when passed in a paramable object" do
foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
end

it "should work when passed in a paramable object" do
foo_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something"
end

it "should work when passed in a paramable object and no subdomain to a _path" do
default_url_options[:host] = "awesome.testapp.com"
foo_path(Paramed.new("something")).should == "/foos/something"
end

it "should work when passed in a paramable object and no subdomain to a _url" do
default_url_options[:host] = "awesome.testapp.com"
foo_url(Paramed.new("something")).should == "http://awesome.testapp.com/foos/something"
end

it "should work on nested resource collections" do
foo_bars_path(Paramed.new("something"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars"
end

it "should work on nested resource members" do
foo_bar_path(Paramed.new("something"),Paramed.new("else"), :subdomain => "awesome").should == "http://awesome.testapp.com/foos/something/bars/else"
end
Expand All @@ -91,13 +111,13 @@
default_url_options[:host] = "awesome.testapp.com"
needs_subdomain_url(:subdomain => false).should == "http://www.testapp.com/needs_subdomain"
end

it "should force a switch to no subdomain on a mirror if preferred_mirror is false" do
SubdomainFu.preferred_mirror = false
default_url_options[:host] = "www.testapp.com"
needs_subdomain_url(:subdomain => false).should == "http://testapp.com/needs_subdomain"
end

after do
SubdomainFu.preferred_mirror = nil
end
Expand Down

0 comments on commit dab049b

Please sign in to comment.