Skip to content

Commit

Permalink
Item14380: fixed Request unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Feb 27, 2023
1 parent 19c83ae commit 0c79bbc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
16 changes: 12 additions & 4 deletions HomePagePlugin/test/unit/HomePagePlugin/HomePagePluginSuite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ sub test_login {
$this->{session} =
Foswiki->new( $this->{test_user_login}, $query, { login => 1 } );

$this->assert_equals( 'Www', $Foswiki::Plugins::SESSION->{topicName} );
$this->assert_equals( 'Home', $Foswiki::Plugins::SESSION->{webName} );
$this->assert_equals( 'TestTopic',
$Foswiki::Plugins::SESSION->{topicName} );
$this->assert_equals(
'TemporaryHomePagePluginTestWeb',
$Foswiki::Plugins::SESSION->{webName}
);
}

sub test_view {
Expand All @@ -111,8 +115,12 @@ sub test_view {
$this->{session} =
Foswiki->new( $this->{test_user_login}, $query, { view => 1 } );

$this->assert_equals( 'Www', $Foswiki::Plugins::SESSION->{topicName} );
$this->assert_equals( 'Home', $Foswiki::Plugins::SESSION->{webName} );
$this->assert_equals( 'TestTopic',
$Foswiki::Plugins::SESSION->{topicName} );
$this->assert_equals(
'TemporaryHomePagePluginTestWeb',
$Foswiki::Plugins::SESSION->{webName}
);
}

sub test_invalid_redirect {
Expand Down
39 changes: 31 additions & 8 deletions UnitTestContrib/test/unit/RequestTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -238,29 +238,51 @@ sub test_queryString {

sub test_forwarded_for {
my $this = shift;
my $req = new Foswiki::Request("");
$ENV{HOST} = "myhost.com";
$ENV{HTTP_X_FORWARDED_FOR} = '1.2.3.4';
$ENV{HTTP_X_FORWARDED_HOST} = 'hop1.com, hop2.com';
$ENV{HTTP_X_FORWARDED_PROTO} = 'https';

#$ENV{HTTP_X_FORWARDED_PORT} = '443';
$Foswiki::cfg{PROXY}{UseForwardedHeaders} = 1;

my $req = new Foswiki::Request("");
$req->secure('1');
$req->header( Host => 'myhost.com' );
$req->header( 'X-Forwarded-Host' => 'hop1.com, hop2.com' );
$req->action('view');
$req->path_info('/Main/WebHome');

# These are not needed. HTTP_ env variables are parsed from the headers
# by the server. Foswiki::Request::url calls Engine::_getConnectionData
# which processes the headers
#$req->header( Host => 'myhost.com' );
#$req->header( 'X-Forwarded-Host' => 'hop1.com, hop2.com' );

my $base = 'https://hop1.com';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host header'
);

#print STDERR $req->url() . "\n";

$req->header( 'X-Forwarded-Host' => 'onehop.com:8080' );
# Verify that port is recovered from first forwarded host
$ENV{HTTP_X_FORWARDED_HOST} = 'onehop.com:8080, hop2.com';
$base = 'https://onehop.com:8080';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host multiple header'
);

# Verify that Forwarded-Port overrides forwarded host port
$ENV{HTTP_X_FORWARDED_HOST} = 'onehop.com:8080, hop2.com';
$ENV{HTTP_X_FORWARDED_PORT} = '443';
$base = 'https://onehop.com';
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host multiple header'
);

$base = 'http://your.domain.com';
$Foswiki::cfg{DefaultUrlHost} = 'http://your.domain.com';
$Foswiki::cfg{ForceDefaultUrlHost} = 1;
Expand All @@ -270,14 +292,15 @@ sub test_forwarded_for {
'Wrong BASE url with Forwarded-Host single header + forceDefaultUrlHost'
);

#print STDERR $req->url() . "\n";

}

sub perform_url_test {
my $this = shift;
my $req = new Foswiki::Request("");
my ( $secure, $host, $action, $path ) = @_;
$ENV{HTTP_HOST} = $host;
$ENV{HTTPS} = ($secure) ? 'ON' : undef;

$req->secure($secure);
$req->header( Host => $host );
$req->action($action);
Expand Down

0 comments on commit 0c79bbc

Please sign in to comment.