Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base infrastructure for testing RobotUA changes
- Loading branch information
David Christensen
committed
Jan 17, 2017
1 parent
348660b
commit 44c4b80
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!perl | ||
|
||
use strict; | ||
use warnings; | ||
use lib 'lib'; | ||
|
||
use Test::More; | ||
use Data::Dumper; | ||
|
||
use Vend::Server; | ||
use Vend::Config; | ||
|
||
*check_is_robot = *Vend::Server::check_is_robot; | ||
|
||
my $robot_cfg_path = "dist/robots.cfg"; | ||
|
||
my @robot_uas = read_file("t/robot_ua/ua.robot"); | ||
my @not_robot_uas = read_file("t/robot_ua/ua.norobot"); | ||
|
||
=for docs | ||
We have to mock a few things for the testing: | ||
Request environment: | ||
- $CGI::remote_addr | ||
- $CGI::remote_host | ||
- $CGI::host | ||
- $CGI::useragent | ||
Configuration: | ||
- Global::RobotIP | ||
- Global::RobotUA | ||
- Global::NotRobotUA | ||
=cut | ||
|
||
local $CGI::remote_addr = "127.0.0.1"; | ||
local $CGI::remote_host = "example.com"; | ||
local $CGI::host = "localhost"; | ||
|
||
parse_robot_cfg($robot_cfg_path); | ||
|
||
# some sanity checks here | ||
|
||
ok( $Global::RobotIP, "RobotIP regex exists" ); | ||
ok( $Global::RobotHost, "RobotHost regex exists" ); | ||
ok( $Global::RobotUA, "RobotUA regex exists" ); | ||
ok( $Global::NotRobotUA, "NotRobotUA regex exists" ); | ||
#ok( $Global::RobotUAFinal, "RobotUAFinal regex was created implicitly"); | ||
|
||
# check various hard-coded UA strings that should/shouldn't get flagged as robots | ||
for my $ua (@robot_uas) { | ||
is(check_is_robot($ua), 1); | ||
} | ||
|
||
for my $ua (@not_robot_uas) { | ||
is(check_is_robot($ua), 0); | ||
} | ||
|
||
done_testing(); | ||
|
||
sub parse_robot_cfg { | ||
my $path = shift; | ||
die "No such file '$path'!\n" unless $path && -f $path; | ||
|
||
my $robot_lines = read_file($path); | ||
|
||
my %D; | ||
|
||
for my $directive (qw/RobotUA NotRobotUA RobotIP RobotHost/) { | ||
# assuming these are and will stay here-docs | ||
if ($robot_lines =~ m/$directive <<(\w+)(.*?)^\1/imsg) { | ||
my $routine = $directive eq 'RobotUA' ? \&Vend::Config::parse_list_robotua : \&Vend::Config::parse_list_wildcard; | ||
|
||
my $string = $2; | ||
$string =~ s/\n//msg; | ||
|
||
my $value = $routine->($directive, $string); | ||
|
||
no strict 'refs'; | ||
${"Global::$directive"} = qr/$value/; | ||
} | ||
} | ||
} | ||
|
||
sub read_file { | ||
my $path = shift; | ||
|
||
open my $fh, '<', $path or die "no such file: $path"; | ||
|
||
local $/ unless wantarray; | ||
return <$fh>; # implicit close | ||
} |
Empty file.
Empty file.