Skip to content

Commit

Permalink
Further solidify OPAC hostname resolution; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctfliblime committed Jun 17, 2011
1 parent 4820502 commit 5c6a0c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
14 changes: 9 additions & 5 deletions C4/Koha.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1164,13 +1164,17 @@ sub _isbn_cleanup {
}

sub CgiOrPlackHostnameFinder {
my $env = shift || \%ENV;

my $hostname
= $ENV{HTTP_X_FORWARDED_HOST}
// $ENV{HTTP_X_FORWARDED_SERVER}
// $ENV{HTTP_HOST}
// $ENV{SERVER_NAME}
= $env->{HTTP_X_FORWARDED_HOST}
// $env->{HTTP_X_FORWARDED_SERVER}
// $env->{HTTP_HOST}
// $env->{SERVER_NAME}
// 'koha-opac.default';
return (split qr{,}, $hostname)[0];
$hostname = (split qr{,}, $hostname)[0];
$hostname =~ s/:.*//;
return $hostname;
}

sub GetOpacConfigByHostname {
Expand Down
62 changes: 50 additions & 12 deletions t/Koha.t
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
BEGIN { $| = 1; print "1..2\n"; }
END {print "not ok 1\n" unless $loaded;}
use C4::Koha;
$loaded = 1;
print "ok 1\n";
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More tests => 7;

BEGIN {
use_ok 'C4::Koha';
}

#
# test that &slashifyDate returns correct (non-US) date
#
$date = "01/01/2002";
$newdate = &slashifyDate("2002-01-01");

if ($date eq $newdate) {
print "ok 2\n";
} else {
print "not ok 2\n";
}
is '01/01/2002', slashifyDate('2002-01-01'), 'slashify';

my $opacname_tests = [
{
env => {
HTTP_X_FORWARDED_HOST => "localhost.x-host.com:5000",
HTTP_X_FORWARDED_SERVER => "localhost.x-server.com:5000,proxy.mydomain.org:1234",
HTTP_HOST => "localhost.host.com:5000",
SERVER_NAME => "localhost.server.com:5000",
},
res => 'localhost.x-host.com',
},
{
env => {
HTTP_X_FORWARDED_SERVER => "localhost.x-server.com:5000",
HTTP_HOST => "localhost.host.com:5000",
SERVER_NAME => "localhost.server.com:5000",
},
res => 'localhost.x-server.com',
},
{
env => {
HTTP_HOST => "localhost.host.com:5000",
SERVER_NAME => "localhost.server.com:5000",
},
res => 'localhost.host.com',
},
{
env => {
SERVER_NAME => "localhost.server.com:5000",
},
res => 'localhost.server.com',
},
{
env => {
},
res => 'koha-opac.default',
},
];

for my $test (@$opacname_tests) {
is C4::Koha::CgiOrPlackHostnameFinder($test->{env}), $test->{res}, 'OPAC hostname resolution';
}

0 comments on commit 5c6a0c3

Please sign in to comment.