root/branches/release-39/t/44-spider.t @ 2533

Revision 2533, 4.3 kB (checked in by mpaschal, 18 months ago)

Make spider tests optional

  • Property svn:mime-type set to text/plain
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2
3use strict;
4
5use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
6use Test::More;
7
8BEGIN {
9    plan $ENV{MT_TEST_SPIDER}
10        ? (tests => 4)
11        : (skip_all => 'Enable spider test with MT_TEST_SPIDER environment variable')
12        ;
13}
14
15use LWP::UserAgent::Local;
16use URI;
17use MT::Test qw(:db :data);
18
19my $username = "Chuck D"; # Melody
20my $password = "bass"; # Nelson
21
22unlink "t/cookie.jar";
23use HTTP::Cookies;
24my $cgipath = MT->config->CGIPath;
25$cgipath =~ s/\/*$//;
26my $cookie_jar = HTTP::Cookies->new(file => "t/cookie.jar");
27my $ua = new LWP::UserAgent::Local({ ScriptAlias => "$cgipath/",
28                                     AddHandler => 'cgi-script .cgi',
29                                     cookie_jar => $cookie_jar,
30                                 });
31my $start_link = "http://localhost" . $cgipath . "/mt.cgi?username=$username&password=$password";
32my $start_url = new URI($start_link);
33
34my %link_queue;
35my %links_checked;
36my $count = 0;
37my $link_count = 0;
38
39my @failures;
40my @notgoods;
41my @notgood_pages;
42my @fetched;
43my @warnings;
44my %modes_seen;
45
46my $skip_pattern = qr{logout|export|magic_token};
47my $must_match = qr{(/cgi-bin/|^\?).*mt\.cgi};
48my $warning_pattern = qr{Uninitialized};
49my $good_pattern = qr{Copyright .* 2001-\d+ Six Apart\. All Rights Reserved\.};
50my $bad_pattern = qr{<input\s+type="submit"\s+value="Log In" />|time\s+to\s+upgrade!}i;
51
52my $verbose = 0;
53my $debug = 1;
54my $test_mode = 1;
55
56$link_queue{$start_link} = $start_link;
57while (keys %link_queue && $count < 500) {
58    my ($curr_link, $its_parent) = %link_queue;
59    $link_count++;
60    delete $link_queue{$curr_link};
61
62    next unless $curr_link =~ m/$must_match/;
63    next if $skip_pattern && $curr_link =~ m/$skip_pattern/;
64
65    $curr_link = URI->new_abs($curr_link, $its_parent);
66    next if $curr_link->scheme ne 'http' && $curr_link->scheme ne 'https';
67    next if $curr_link->host ne $start_url->host();
68
69    unless ($links_checked{$curr_link}) {
70        print "REQUESTING $curr_link\n" if $verbose;
71        my $req = new HTTP::Request(GET => $curr_link)
72                          or die "a thousand deaths";
73        my $resp = $ua->request($req) or next;
74
75        #print STDERR "Response: [" . $resp->content() . "]\n" if $verbose;
76        use Data::Dumper;
77        print STDERR $resp->content() unless $resp->content() =~ m/$good_pattern/;
78
79        push @failures, $curr_link unless $resp->is_success;
80        my ($mode) = ($curr_link =~ m/__mode=([^&]*)/);
81        if ($mode) {
82            if (exists $modes_seen{$mode}) {
83                $modes_seen{$mode}++;
84            } else {
85                $modes_seen{$mode} = 1;
86            }
87        }
88        $count++;
89        my $content = $resp->content();
90        push @notgoods, $curr_link unless $content =~ m/$good_pattern/;
91        push @notgoods, $curr_link if $content =~ m/$bad_pattern/;
92        push @warnings, $curr_link if $content =~ m/$warning_pattern/;
93        push @fetched, $curr_link;
94        my @form_actions = $content =~ m|<form[^>]* action="([^"]*)">|gi; #"
95        my @links = $content =~ m|<[^>]*href="([^"]*)">|gi;   #"
96        @links = grep {$_ =~ /\S/} @links;
97        @links = map { s/\&amp\;/&/g; $_ } @links;
98        @links = map { s/\&offset=\d+//; $_ } @links;
99        @links = map { URI->new_abs($_, $curr_link) } @links;
100        $link_queue{$_} = $curr_link foreach (@links);
101        $links_checked{$curr_link}++;
102#         print join "\n", (keys %link_queue);
103#         print "\n";
104    }
105}
106
107# There should be at least a handful of pages!
108ok($count > 50);
109
110print "\nCrawled $count pages (saw $link_count links). ",
111    "\n", scalar @failures, " pages failed to load.\n";
112print "$_\n" foreach @failures;
113
114if ($verbose) {
115#     print "modes: ", join ", ", (keys %modes_seen), "\n";
116#     print "Fetched:\n";
117#     print "$_\n" foreach @fetched;
118    if ($debug) {
119        print "Faulty pages:";
120        print "$_\n" foreach @notgood_pages;
121    }
122    print "\n", scalar @notgoods, " pages appeared faulty:\n";
123    print "$_\n" foreach @notgoods;
124    print "\n", scalar @warnings, " pages produced warnings:\n";
125    print "$_\n" foreach @warnings;
126}
127
128if ($test_mode) {
129    print "# Checking that there were no failures.\n";
130    ok(!@failures);
131    print "# Checking that there were no warnings.\n";
132    ok(!@warnings);
133    print "# Checking that all pages were good.\n";
134    ok(!@notgoods);
135}
Note: See TracBrowser for help on using the browser.