fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / redirect.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => 13;
5
6 my $r = $HTML::Mason::Commands::r = bless {}, 'R';
7 my $m = $HTML::Mason::Commands::m = bless {}, 'M';
8
9 set_config(
10     CanonicalizeRedirectURLs => 0,
11     WebDomain => 'localhost',
12     WebPort => 80,
13     WebPath => '',
14 );
15 is( RT->Config->Get('WebBaseURL'), 'http://localhost' );
16 is( RT->Config->Get('WebURL'), 'http://localhost/' );
17
18 redirect_ok(
19     'http://localhost/Ticket/', 'http://localhost/Ticket/',
20     { SERVER_NAME => 'localhost', SERVER_PORT => 80 },
21 );
22 redirect_ok(
23     '/Ticket/', 'http://localhost/Ticket/',
24     { SERVER_NAME => 'localhost', SERVER_PORT => 80 },
25 );
26 redirect_ok(
27     'http://localhost/Ticket/', 'http://example.com/Ticket/',
28     { SERVER_NAME => 'example.com', SERVER_PORT => 80 },
29 );
30
31 set_config(
32     CanonicalizeRedirectURLs => 0,
33     WebDomain => 'localhost',
34     WebPort => 443,
35     WebPath => '',
36 );
37 is( RT->Config->Get('WebBaseURL'), 'https://localhost' );
38 is( RT->Config->Get('WebURL'), 'https://localhost/' );
39
40 redirect_ok(
41     'https://localhost/Ticket/', 'https://localhost/Ticket/',
42     { SERVER_NAME => 'localhost', SERVER_PORT => 443, HTTPS => 'on' },
43 );
44 redirect_ok(
45     '/Ticket/', 'https://localhost/Ticket/',
46     { SERVER_NAME => 'localhost', SERVER_PORT => 443, HTTPS => 'on' },
47 );
48 redirect_ok(
49     'https://localhost/Ticket/', 'http://localhost/Ticket/',
50     { SERVER_NAME => 'localhost', SERVER_PORT => 80 },
51 );
52 redirect_ok(
53     '/Ticket/', 'http://localhost/Ticket/',
54     { SERVER_NAME => 'localhost', SERVER_PORT => 80 },
55 );
56 redirect_ok(
57     'https://localhost/Ticket/', 'http://example.com/Ticket/',
58     { SERVER_NAME => 'example.com', SERVER_PORT => 80 },
59 );
60 redirect_ok(
61     'https://localhost/Ticket/', 'https://example.com/Ticket/',
62     { SERVER_NAME => 'example.com', SERVER_PORT => 443, HTTPS => 'on' },
63 );
64
65 sub set_config {
66     my %values = @_;
67     while ( my ($k, $v) = each %values ) {
68         RT->Config->Set( $k => $v );
69     }
70
71     unless ( $values{'WebBaseURL'} ) {
72         my $port = RT->Config->Get('WebPort');
73         RT->Config->Set(
74             WebBaseURL => 
75                 ($port == 443? 'https': 'http') .'://'
76                 . RT->Config->Get('WebDomain')
77                 . ($port != 80 && $port != 443? ":$port" : '')
78         );
79     }
80     unless ( $values{'WebURL'} ) {
81         RT->Config->Set(
82             WebURL => RT->Config->Get('WebBaseURL') . RT->Config->Get('WebPath') . "/"
83         );
84     }
85 }
86
87 sub redirect_ok {
88     my ($to, $expected, $env, $details) = @_;
89
90     local %ENV = %ENV;
91     while ( my ($k, $v) = each %{ $env || {} } ) {
92         $ENV{ $k } = $v;
93     }
94     RT::Interface::Web::Redirect( $to );
95     is($m->redirect, $expected, $details || "correct for '$to'");
96 }
97
98 package R;
99 sub status {};
100
101 package M;
102 sub redirect { $_[0]{'last'} = $_[1] if @_ > 1; return $_[0]{'last'} }
103 sub abort {}
104