Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/bb4f88a71e6e276aafc9 to your computer and use it in GitHub Desktop.
Save jberger/bb4f88a71e6e276aafc9 to your computer and use it in GitHub Desktop.
Use Mojo::UserAgent/Mojo::DOM to verify /fortunes test
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
my $url = shift or die "Usage: $0 <url>\n";
my $t = Test::Mojo->new;
$t->get_ok($url)
->status_is(200)
->text_is('html > head > title' => 'Fortunes')
->text_is('table th:nth-child(1)' => 'id')
->text_is('table th:nth-child(2)' => 'message')
->content_unlike(qr/<script>/)
->element_exists_not('script');
# convert the remaining table to an array of arrayrefs
my @rows = $t->tx->res->dom->find('table tr')->map(sub{[$_->find('th, td')->pluck('text')->each]})->each;
my $expected = [
[ 'id', 'message' ],
[ '11', '<script>alert("This should not be displayed in a browser alert box.");</script>' ],
[ '4', 'A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1' ],
[ '5', 'A computer program does what you tell it to do, not what you want it to do.' ],
[ '2', 'A computer scientist is someone who fixes things that aren\'t broken.' ],
[ '8', "A list is only as strong as its weakest link. \x{2014} Donald Knuth" ],
[ '0', 'Additional fortune added at request time.' ],
[ '3', 'After enough decimal places, nobody gives a damn.' ],
[ '7', 'Any program that runs right is obsolete.' ],
[ '10', 'Computers make very fast, very accurate mistakes.' ],
[ '6', "Emacs is a nice operating system, but I prefer UNIX. \x{2014} Tom Christaensen" ],
[ '9', 'Feature: A bug with seniority.' ],
[ '1', 'fortune: No such file or directory' ],
[ '12', "\x{30d5}\x{30ec}\x{30fc}\x{30e0}\x{30ef}\x{30fc}\x{30af}\x{306e}\x{30d9}\x{30f3}\x{30c1}\x{30de}\x{30fc}\x{30af}" ]
];
is_deeply \@rows, $expected, 'Correct table data';
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment