Skip to content

Commit

Permalink
Item14380: Fix failing unit tests.
Browse files Browse the repository at this point in the history
Foswiki::Request now asks the engine to interpret the HOST / CLIENT
related headers.  So the tests need to set the appropriate ENV
variables.
  • Loading branch information
gac410 committed Jan 24, 2018
1 parent 837a468 commit f1622a6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ sub test_login {
);
$query->path_info("");
$query->header( 'Host' => 'www.home.org' );
$ENV{HTTP_HOST} = 'www.home.org';

$this->{test_topicObject}->finish() if $this->{test_topicObject};
$this->{session}->finish() if $this->{session};
Expand All @@ -105,6 +106,7 @@ sub test_view {
);
$query->path_info("");
$query->header( 'Host' => 'www.home.org' );
$ENV{HTTP_HOST} = 'www.home.org';

$this->{test_topicObject}->finish() if $this->{test_topicObject};
$this->{session}->finish() if $this->{session};
Expand Down
3 changes: 3 additions & 0 deletions UnitTestContrib/lib/Unit/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ sub setUrl {
my $host = $2;
if ( $protocol =~ /https/i ) {
$this->secure(1);
$ENV{HTTPS} = 'ON';
}
else {
$this->secure(0);
$ENV{HTTPS} = undef;
}

#print STDERR "setting Host to $host\n";
$this->header( -name => 'Host', -value => $host );
$ENV{HTTP_HOST} = $host;
}

my @pairs = split /[&;]/, $urlParams;
Expand Down
61 changes: 61 additions & 0 deletions UnitTestContrib/test/unit/RequestTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,71 @@ sub test_queryString {
);
}

sub test_forwarded_for {
my $this = shift;
$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}{UseForwardedFor} = 1;
$Foswiki::cfg{PROXY}{UseForwardedHeaders} = 1;

my $req = new Foswiki::Request("");
$req->secure('1');
$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'
);

# 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{ForceDefaultUrlHost} = 1;
$this->assert_str_equals(
$base,
$req->url( -base => 1 ),
'Wrong BASE url with Forwarded-Host single header + forceDefaultUrlHost'
);

}

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
3 changes: 2 additions & 1 deletion core/lib/Foswiki/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ sub _getConnectionData {

my $detectProxy = shift;

# $detectProxy = 'b'; #Force debug tracing (ie bootstrap mode)

my ( $client, $proto, $host, $port, $proxy );

# These are the defaults populated in a conventional server, no proxy
Expand Down Expand Up @@ -598,7 +600,6 @@ sub _getConnectionData {
}

if ( my $hdr = $ENV{HTTP_X_FORWARDED_PORT} ) {
print STDERR "HEADER $hdr\n";
my $first = ( split /\s?,\s?/, $hdr )[0];
if ( defined $first ) {
$fwdPort = $first;
Expand Down

0 comments on commit f1622a6

Please sign in to comment.