[Catalyst-dev] subdomain hook for Catalyst::Test

Jason Gottshall jgottshall at capwiz.com
Mon Sep 29 21:32:10 BST 2008

Jan Henning Thorsen wrote:
> On Thu, Sep 18, 2008 at 12:12 AM, Jason Gottshall <jgottshall at capwiz.com> wrote:
>> Our app allows for virtual subdomains that (among other things) enable
>> specific behaviors in the app.
>> For example,
>>  http://www.myapp.com/foo/bar
>> and
>>  http://magic.myapp.com/foo/bar
>> both point to same app, but the latter has "magic" behaviors associated with
>> it.
>> The problem is that we're having trouble writing tests against specific
>> behaviors in our controller tests, particularly when we want to test several
>> different subdomains within the same script.
>> My current solution is to set an environment variable in the test script
>> specifing the desired subdomain. I've added hooks to the app that will use
>> this value if available, so that controller tests using a "local" instance
>> of the app instantiated with Catalyst::Test and a faked request will Just
>> Work. But we run into trouble when we try to run the tests against a
>> "remote" server by setting CATALYST_SERVER. The env var setting embedded in
>> the script obviously is not visible to the server instance that's running
>> remotely.
>> In order to remedy this problem, I've patched Catalyst::Test to look for my
>> new env var and prepend it to the CATALYST_SERVER host component. It works
>> great! But I'm wondering whether this patch is worthy of adding to the core,
>> or if there's a different way I should be approaching the problem. Here's a
>> diff against 5.70/trunk:
>> Index: lib/Catalyst/Test.pm
>> ===================================================================
>> --- lib/Catalyst/Test.pm        (revision 8432)
>> +++ lib/Catalyst/Test.pm        (working copy)
>> @@ -148,6 +148,10 @@
>>     my $request = Catalyst::Utils::request( shift(@_) );
>>     my $server  = URI->new( $ENV{CATALYST_SERVER} );
>> +    if ( $ENV{CATALYST_SUBDOMAIN} ) {
>> +        $server->host("$ENV{CATALYST_SUBDOMAIN}." . $server->host);
>> +    }
>> +
>>     if ( $server->path =~ m|^(.+)?/$| ) {
>>         my $path = $1;
>>         $server->path("$path") if $path;    # need to be quoted
>> If this approach makes sense, I'll add documentation to the patch, of
>> course. I may need a little help coming up with a working test, though.
>> Thoughts, anyone? Thanks,
>> Jason
>> _______________________________________________
>> Catalyst-dev mailing list
>> Catalyst-dev at lists.scsys.co.uk
>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
> I got a possible messier suggestion:)
> sub foo :Global Domain(foo.com) { .. }
> sub foo :Global Domain(magic.foo.com) { .. }
> The idea is (stating the obvious) adding a new attribute that match
> the domain-name. Could also be regex..?

I wish it were that simple! I should have been a bit clearer: we 
actually use a distinct virtual subdomain for each of our customers 
(2000+), and we use that value to set up wrappers and look up site 
preferences, so it's not just a question of testing for explicit domain 
values for specific actions.

But your comments do lead me thinking in a different direction: if I 
want to write a test for the behavior of a specific site preference, 
maybe I should try to force a value for that preference on the next 
request, rather than trying to force the selection of a particular 
virtual domain that I know (or hope) has the desired setting. Certainly 
something to consider.

Thanks for sparing a few brain cells on this one...

More information about the Catalyst-dev mailing list