1#!/usr/bin/perl -w 2 3# $Id: 05-report.t,v 1.13 2003/03/24 11:21:16 m_ilya Exp $ 4 5# This script tests core plugins of HTTP::WebTest. 6 7use strict; 8use CGI::Cookie; 9use IO::File; 10use HTTP::Status; 11 12use HTTP::WebTest; 13use HTTP::WebTest::SelfTest; 14use HTTP::WebTest::Utils qw(start_webserver stop_webserver); 15 16use Test::More tests => 12; 17 18# init tests 19my $PID = start_webserver(port => $PORT, server_sub => \&server_sub); 20my $WEBTEST = HTTP::WebTest->new; 21my $TEST = { url => abs_url($URL, '/test-file1'), 22 text_require => [ '987654' ] }; 23my $COOKIE_TEST = { url => abs_url($URL, '/set-cookie-c1-v1') }; 24my $COOKIE_FILTER = sub { $_[0] =~ s/expires=.*?GMT/expires=SOMEDAY/;}; 25my $HEADER_FILTER = 26 sub { 27 $_[0] =~ s/: .*?GMT/: SOMEDAY/g; 28 $_[0] =~ s|Server: libwww-perl-daemon/[\w\.]*|Server: libwww-perl-daemon/NN|g; 29 $_[0] =~ s|User-Agent: HTTP-WebTest/[\w\.]*|User-Agent: HTTP-WebTest/NN|g; 30 $_[0] =~ s|Client-.*?: .*?\n||g; 31 }; 32 33# 1: test fh_out parameter 34{ 35 my $temp_file = 't/report'; 36 37 my $fh = new IO::File; 38 $fh->open("> $temp_file") or die "Can't open file $temp_file: $!"; 39 $WEBTEST->run_tests([ $TEST ], { fh_out => $fh }); 40 $fh->close; 41 42 my $output = read_file($temp_file); 43 44 canonical_output(server_url => $URL, output_ref => \$output); 45 compare_output(check_file => 't/test.out/report-fh', 46 output_ref => \$output); 47 48 unlink $temp_file; 49} 50 51# 2: test show_html parameter 52{ 53 my $opts = { show_html => 'yes' }; 54 55 check_webtest(webtest => $WEBTEST, 56 server_url => $URL, 57 opts => $opts, 58 tests => [ $TEST ], 59 check_file => 't/test.out/report-html'); 60} 61 62# 3-4: test show_cookie parameter 63SKIP: { 64 my $skip = $HOSTNAME !~ /\..*\./ ? 65 'cannot test cookies - hostname does not contain two dots' : 66 undef; 67 skip $skip, 2 if $skip; 68 69 my $opts = { show_cookies => 'yes' }; 70 71 check_webtest(webtest => $WEBTEST, 72 server_url => $URL, 73 opts => $opts, 74 out_filter => $COOKIE_FILTER, 75 tests => [ $COOKIE_TEST ], 76 check_file => 't/test.out/report-cookie1'); 77 78 # note that second time we should send cookie ourselves 79 check_webtest(webtest => $WEBTEST, 80 server_url => $URL, 81 opts => $opts, 82 out_filter => $COOKIE_FILTER, 83 tests => [ $COOKIE_TEST ], 84 check_file => 't/test.out/report-cookie2'); 85} 86 87# 5: test show_cookie and show_html parameters 88SKIP: { 89 my $skip = $HOSTNAME !~ /\..*\./ ? 90 'cannot test cookies - hostname does not contain two dots' : 91 undef; 92 skip $skip, 1 if $skip; 93 94 my $opts = { show_html => 'yes', 95 show_cookies => 'yes' }; 96 97 check_webtest(webtest => $WEBTEST, 98 server_url => $URL, 99 opts => $opts, 100 out_filter => $COOKIE_FILTER, 101 tests => [ $COOKIE_TEST ], 102 check_file => 't/test.out/report-html-cookie'); 103} 104 105# 6-7: test terse parameter 106{ 107 my $tests = [ $TEST, 108 { url => abs_url($URL, '/non-existent') } ]; 109 110 for my $terse (qw(summary failed_only)) { 111 my $opts = { terse => $terse }; 112 113 check_webtest(webtest => $WEBTEST, 114 server_url => $URL, 115 opts => $opts, 116 tests => $tests, 117 check_file => "t/test.out/report-terse-$terse"); 118 } 119} 120 121# 8-9: test default_report parameter 122{ 123 my $tests = [ $TEST, 124 { url => abs_url($URL, '/non-existent') } ]; 125 126 for my $default_report (qw(yes no)) { 127 my $opts = { default_report => $default_report }; 128 129 check_webtest(webtest => $WEBTEST, 130 server_url => $URL, 131 opts => $opts, 132 tests => $tests, 133 check_file => "t/test.out/default-report-$default_report"); 134 } 135} 136 137# 10: test show_headers parameter 138{ 139 # remove cookies from cookie jar - it affects output of report plugin 140 $WEBTEST->reset_user_agent; 141 142 my $tests = [ $TEST, 143 { url => abs_url($URL, '/non-existent') } ]; 144 145 my $opts = { show_headers => 'yes' }; 146 147 my $out_filter = sub { 148 $HEADER_FILTER->($_[0]); 149 }; 150 151 check_webtest(webtest => $WEBTEST, 152 server_url => $URL, 153 opts => $opts, 154 out_filter => $out_filter, 155 tests => $tests, 156 check_file => 't/test.out/show-headers') 157} 158 159# 11-12: test show_html, show_cookie, show_headers with terse parameter 160SKIP: { 161 my $skip = $HOSTNAME !~ /\..*\./ ? 162 'cannot test cookies - hostname does not contain two dots' : 163 undef; 164 skip $skip, 2 if $skip; 165 166 my $tests = [ $COOKIE_TEST, 167 { url => abs_url($URL, '/non-existent') } ]; 168 169 my $out_filter = sub { 170 $HEADER_FILTER->($_[0]); 171 $COOKIE_FILTER->($_[0]); 172 }; 173 174 for my $terse (qw(summary failed_only)) { 175 my $opts = { terse => $terse, 176 show_html => 'yes', 177 show_cookie => 'yes', 178 show_headers => 'yes' }; 179 180 check_webtest(webtest => $WEBTEST, 181 server_url => $URL, 182 opts => $opts, 183 tests => $tests, 184 out_filter => $out_filter, 185 check_file => "t/test.out/report-terse-show-$terse"); 186 } 187} 188 189# try to stop server even we have been crashed 190END { stop_webserver($PID) if defined $PID } 191 192# here we handle connects to our mini web server 193sub server_sub { 194 my %param = @_; 195 196 my $request = $param{request}; 197 my $connect = $param{connect}; 198 199 my $path = $request->url->path; 200 201 if($path eq '/test-file1' ) { 202 $connect->send_file_response('t/test1.txt'); 203 } elsif($path =~ m|^/set-cookie-(\w+)-(\w+)$| ) { 204 my $name = $1; 205 my $value = $2; 206 207 # create cookie 208 my $cookie = new CGI::Cookie(-name => $name, 209 -value => $value, 210 -path => '/', 211 -expires => '+1M' ); 212 213 # create response object 214 my $response = new HTTP::Response(RC_OK); 215 $response->header(Content_Type => 'text/plain'); 216 $response->header(Set_Cookie => $cookie->as_string); 217 $response->content('Set cookie test'); 218 219 # send it to browser 220 $connect->send_response($response); 221 } else { 222 $connect->send_error(RC_NOT_FOUND); 223 } 224} 225