1#!/usr/bin/perl -w 2 3# $Id: 01-api.t,v 1.15 2003/01/25 17:48:05 m_ilya Exp $ 4 5# This script tests public API of HTTP::WebTest. 6 7use strict; 8use HTTP::Status; 9 10use HTTP::WebTest; 11use HTTP::WebTest::SelfTest; 12use HTTP::WebTest::Utils qw(start_webserver stop_webserver); 13 14use Test::More tests => 19; 15 16# init test 17my $PID = start_webserver(port => $PORT, server_sub => \&server_sub); 18my $WEBTEST = HTTP::WebTest->new; 19 20# 1: get default user agent object 21{ 22 ok(defined $WEBTEST->user_agent->can('request')); 23} 24 25# 2: set our user agent 26{ 27 my $user_agent = new LWP::UserAgent; 28 $WEBTEST->user_agent($user_agent); 29 is($WEBTEST->user_agent, $user_agent); 30} 31 32# 3: reset to default user agent 33{ 34 $WEBTEST->user_agent(undef); 35 ok(defined $WEBTEST->user_agent->can('request')); 36} 37 38# 4: check what returns method tests (should be reference on empty array) 39{ 40 my $aref = $WEBTEST->tests; 41 is(@$aref, 0); 42} 43 44# 5-6: run single test and check last response and last request 45{ 46 my $url = abs_url($URL, '/test-file1'); 47 my $test = { url => $url }; 48 $WEBTEST->run_test($test); 49 my $request = $WEBTEST->current_request; 50 my $response = $WEBTEST->current_response; 51 is($request->uri->as_string, $url->as_string); 52 ok($response->is_success); 53} 54 55# 7: run several tests 56{ 57 my $tests = [ { url => abs_url($URL, '/test-file1') }, 58 { url => abs_url($URL, '/status-forbidden') }, 59 { url => abs_url($URL, '/doesnt-exist') } ]; 60 61 check_webtest(webtest => $WEBTEST, 62 server_url => $URL, 63 tests => $tests, 64 check_file => 't/test.out/status'); 65} 66 67# 8: check what returns method tests now 68{ 69 my $aref = $WEBTEST->tests; 70 is(@$aref, 3); 71} 72 73# 9-10: parse wt script 74{ 75 my $data = read_file('t/simple.wt'); 76 77 my ($tests, $opts) = $WEBTEST->parse($data); 78 is($tests->[0]{test_name}, 'Some name here'); 79 is($opts->{text_require}[0], 'Require some'); 80} 81 82# 11: run tests defined in wt script 83{ 84 generate_testfile(file => 't/real.wt', server_url => $URL); 85 86 my $output = ''; 87 88 $WEBTEST->run_wtscript('t/real.wt', { output_ref => \$output }); 89 90 canonical_output(server_url => $URL, output_ref => \$output); 91 compare_output(output_ref => \$output, 92 check_file => 't/test.out/run-wtscript'); 93} 94 95# 12: run inlined wtscript 96{ 97 my $output = ''; 98 99 $WEBTEST->run_wtscript(<<WTSCRIPT, { output_ref => \$output }); 100text_forbid = ( FAILED TEST ) 101 102test_name = Some name here 103 url = ${URL}test-file1 104 regex_require = ( TEST TEST ) 105end_test 106 107test_name = Another name 108 url = ${URL}no-such-file 109end_test 110WTSCRIPT 111 112 canonical_output(server_url => $URL, output_ref => \$output); 113 compare_output(output_ref => \$output, 114 check_file => 't/test.out/run-wtscript'); 115} 116 117# 13-14: test num_fail and num_succeed 118{ 119 my $tests = [ { url => abs_url($URL, '/test-file1') }, 120 { url => abs_url($URL, '/status-forbidden') }, 121 { url => abs_url($URL, '/doesnt-exist') } ]; 122 123 my $output = ''; 124 125 $WEBTEST->run_tests($tests, { output_ref => \$output }); 126 is($WEBTEST->num_fail, 2); 127 is($WEBTEST->num_succeed, 1); 128} 129 130# 15: test current_test after running $WEBTEST->run_tests 131{ 132 my $tests = [ { url => abs_url($URL, '/test-file1') }, 133 { url => abs_url($URL, '/doesnt-exist') } ]; 134 135 my $output = ''; 136 137 $WEBTEST->run_tests($tests, { output_ref => \$output }); 138 is($WEBTEST->current_test->request->uri->as_string, 139 abs_url($URL, '/doesnt-exist')->as_string); 140} 141 142# 16-19: test $WEBTEST->parser_package 143{ 144 is($WEBTEST->parser_package, 'HTTP::WebTest::Parser'); 145 { 146 package TestParser; 147 148 sub parse { [ x => 'z' ], { 1 => 2 } }; 149 }; 150 # set non default parser 151 $WEBTEST->parser_package('TestParser'); 152 is($WEBTEST->parser_package, 'TestParser'); 153 is_deeply([ [ x => 'z' ], { 1 => 2 } ], 154 [ $WEBTEST->parse('a = b') ]); 155 # reset to default 156 $WEBTEST->parser_package(undef); 157 is_deeply([ [ ], { a => 'b' } ], 158 [ $WEBTEST->parse('a = b') ]); 159} 160 161# try to stop server even we have been crashed 162END { stop_webserver($PID) if defined $PID } 163 164# here we handle connects to our mini web server 165sub server_sub { 166 my %param = @_; 167 168 my $request = $param{request}; 169 my $connect = $param{connect}; 170 171 my $path = $request->url->path; 172 173 if($path eq '/test-file1' ) { 174 $connect->send_file_response('t/test1.txt'); 175 } elsif($path eq '/status-forbidden') { 176 $connect->send_error(RC_FORBIDDEN); 177 } else { 178 $connect->send_error(RC_NOT_FOUND); 179 } 180} 181