Debian Codegraph @

t/14-command-identity.t

raw
1 #!/usr/bin/perl -w
2 use strict;
3
4 use FindBin;
5 use File::Temp qw( tempfile );
6 our ($_STDOUT_, $_STDERR_ );
7 use URI::URL;
8 #use LWP::Simple;
9 use Test::HTTP::LocalServer;
10 use Test::More;
11 use lib './inc';
12 use IO::Catch;
13
14 # Catch output:
15 $SIG{__WARN__} = sub { $main::_STDERR_ .= join '', @_; };
16 tie *STDOUT, 'IO::Catch', '_STDOUT_' or die $!;
17 #tie *STDERR, 'IO::Catch', '_STDERR_' or die $!;
18
19 # Make HTML::Display do nothing:
20 BEGIN {
21 $ENV{PERL_HTML_DISPLAY_CLASS} = 'HTML::Display::Dump';
22 delete $ENV{PAGER};
23 };
24 use HTML::Display;
25
26 our %tests = (
27 autofill => { requests => 2, lines => [ 'get %s',
28 'autofill query Fixed foo',
29 'autofill cat Keep',
30 'autofill botcheck_query Fixed checked',
31 'autofill query2 Fixed bar',
32 'fillout',
33 'submit' ], location => qr'^%s/formsubmit$',
34 values => { query2 => 'bar', query => 'foo' }
35 },
36 auth => { requests => 1, lines => [ 'auth localhost:80 realm user password', 'get %s' ], location => qr'^%s/$' },
37 auth_guess => { requests => 1, lines => [ 'get %s', 'auth user password' ], location => qr'^%s/$' },
38 back => { requests => 2, lines => [ 'get %s','open 0','back' ], location => qr'^%s/$' },
39 content_save => { requests => 1, lines => [ 'get %s','content tmp.content','eval unlink "tmp.content"'], location => qr'^%s/$' },
40 comment => { requests => 1, lines => [ '# a comment','get %s','# another comment' ], location => qr'^%s/$' },
41 eval => { requests => 1, lines => [ 'eval "Hello World"', 'get %s','eval "Goodbye World"' ], location => qr'^%s/$' },
42 eval_shell => { requests => 1, lines => [ 'get %s', 'eval $self->agent->ct' ], location => qr'^%s/$' },
43 eval_sub => { requests => 2, lines => [
44 '# Fill in the "date" field with the current date/time as string',
45 'eval sub ::custom_today { "20030511" };',
46 'autofill session Callback ::custom_today',
47 'autofill query Keep',
48 'autofill botcheck_query Fixed checked',
49 'autofill query2 Fixed bar',
50 'autofill cat Keep',
51 'get %s',
52 'fillout',
53 'eval $self->agent->current_form->value("session")',
54 'submit',
55 'content',
56 ], location => qr'^%s/formsubmit$',
57 values => { query2 => 'bar', query => qr!\(empty\)!, session => '20030511' }
58 },
59 eval_multiline => { requests => 2,
60 lines => [ 'get %s',
61 'autofill query Keep',
62 'autofill cat Keep',
63 'autofill botcheck_query Fixed checked',
64 'autofill query2 Fixed bar',
65 'fillout',
66 'submit',
67 'eval "Hello World ",
68 "from ",$self->agent->uri',
69 'content' ],
70 location => qr'^%s/formsubmit$',
71 values => { query2 => 'bar', query => qr!\(empty\)! },
72 },
73 form_name => { requests => 2, lines => [ 'get %s','form f','submit' ],
74 location => qr'^%s/formsubmit$',
75 values => { query2 => qr!\(empty\)!, query => qr!\(empty\)! },
76 },
77 form_num => { requests => 2, lines => [ 'get %s','form 1','submit' ],
78 location => qr'^%s/formsubmit$',
79 values => { query2 => qr!\(empty\)!, query => qr!\(empty\)! },
80 },
81 formfiller_chars => { requests => 2,
82 lines => [ 'eval srand 0',
83 'autofill botcheck_query Fixed checked',
84 'autofill query2 Fixed bar',
85 'autofill cat Keep',
86 'autofill query Random::Chars size 5 set alpha', 'get %s', 'fillout','submit','content' ],
87 values => { query2 => 'bar', query => qr!\w{5}! },
88 location => qr'^%s/formsubmit$' },
89 formfiller_date => { requests => 2,
90 lines => [ 'eval srand 0',
91 'autofill botcheck_query Fixed checked',
92 'autofill query2 Fixed bar',
93 'autofill cat Keep',
94 'autofill query Random::Date string %%Y%%m%%d', 'get %s', 'fillout','submit','content' ],
95 values => { query2 => 'bar', query => qr!\d{8}! },
96 location => qr'^%s/formsubmit$' },
97 formfiller_default => { requests => 2,
98 lines => [ 'autofill query Default foo',
99 'autofill cat Keep',
100 'autofill botcheck_query Fixed checked',
101 'autofill query2 Fixed bar',
102 'get %s', 'fillout','submit','content' ],
103 values => { query2 => 'bar', query => qr!\(empty\)! },
104 location => qr'^%s/formsubmit$' },
105 formfiller_fixed => { requests => 2,
106 lines => [ 'autofill query Fixed foo',
107 'autofill cat Keep',
108 'autofill botcheck_query Fixed checked',
109 'autofill query2 Fixed bar',
110 'get %s', 'fillout','submit','content' ],
111 values => { query2 => 'bar', query => 'foo' },
112 location => qr'^%s/formsubmit$' },
113 formfiller_keep => { requests => 2,
114 lines => [ 'autofill query Keep',
115 'autofill cat Keep',
116 'autofill botcheck_query Fixed checked',
117 'autofill query2 Fixed bar',
118 'get %s', 'fillout','submit','content' ],
119 values => { query2 => 'bar', query => qr!\(empty\)! },
120 location => qr'^%s/formsubmit$' },
121 formfiller_random => { requests => 2,
122 lines => [ 'autofill query Random foo',
123 'autofill cat Keep',
124 'autofill botcheck_query Fixed checked',
125 'autofill query2 Fixed bar',
126 'get %s', 'fillout','submit','content' ],
127 location => qr'^%s/formsubmit$' },
128 formfiller_re => { requests => 2,
129 lines => [ 'eval srand 0',
130 'autofill cat Keep',
131 'autofill botcheck_query Fixed checked',
132 'autofill /qu/ Random::Date string %%Y%%m%%d', 'get %s', 'fillout','submit','content' ],
133 values => { query2 => qr/^\d{8}$/, query => qr/^\d{8}$/ },
134 location => qr'^%s/formsubmit$' },
135 formfiller_word => { requests => 2,
136 lines => [ 'eval srand 0',
137 'autofill cat Keep',
138 'autofill botcheck_query Fixed checked',
139 'autofill query2 Fixed bar',
140 'autofill query Random::Word size 1', 'get %s', 'fillout','submit','content' ],
141 location => qr'^%s/formsubmit$' },
142 get => { requests => 1, lines => [ 'get %s' ], location => qr'^%s/' },
143 get_content => { requests => 1, lines => [ 'get %s', 'content' ], location => qr'^%s/' },
144 get_redirect => { requests => 2, lines => [ 'get %sredirect/startpage' ], location => qr'^%s/startpage' },
145 get_save => { requests => 4, lines => [ 'get %s','save "/\.save_log_server_test\.tmp$/"' ], location => qr'^%s/' },
146 get_value_click => { requests => 2, lines => [ 'get %s','value query foo', 'click submit' ], location => qr'^%s/formsubmit$' },
147 get_value_submit => { requests => 2, lines => [ 'get %s','value query foo', 'submit' ], location => qr'^%s/formsubmit$' },
148 get_value2_submit => { requests => 2, lines => [
149 'get %s',
150 'value query foo',
151 'value session 2',
152 'submit'
153 ], location => qr'^%s/formsubmit$',
154 values => { session => '2', query => 'foo' },
155 },
156 interactive_script_creation => { requests => 2,
157 lines => [ 'eval @::list=qw(foo bar xxx)',
158 'eval no warnings qw"redefine once"; *WWW::Mechanize::FormFiller::Value::Ask::ask_value = sub { my $value=shift @::list; push @{$_[0]->{shell}->{answers}}, [ $_[1]->name, $value ]; $value }',
159 'autofill cat Keep',
160 'autofill botcheck_query Fixed checked',
161 'autofill query2 Fixed bar',
162 'get %s',
163 'fillout',
164 'submit',
165 'content' ],
166 location => qr'^%s/formsubmit$' },
167 open_parm => { requests => 2, lines => [ 'get %s','open 1','content' ], location => qr'^%s/test$' },
168 open_re => { requests => 2, lines => [ 'get %s','open "Link foo1.save_log_server_test.tmp"','content' ], location => qr'^%s/foo1.save_log_server_test.tmp$' },
169 open_re2 => { requests => 2, lines => [ 'get %s','open "/foo1/"','content' ], location => qr'^%s/foo1.save_log_server_test.tmp$' },
170 open_re3 => { requests => 2, lines => [ 'get %s','open "/Link /foo/"','content' ], location => qr'^%s/foo$' },
171 open_re4 => { requests => 2, lines => [ 'get %s','open "/Link \/foo/"','content' ], location => qr'^%s/foo$' },
172 open_re5 => { requests => 2, lines => [ 'get %s','open "/Link /$/"','content' ], location => qr'^%s/slash_end$' },
173 open_re6 => { requests => 2, lines => [ 'get %s','open "/^/Link$/"','content' ], location => qr'^%s/slash_front$' },
174 open_re7 => { requests => 2, lines => [ 'get %s','open "/^/Link in slashes//"','content' ], location => qr'^%s/slash_both$' },
175 reload => { requests => 2, lines => [ 'get %s','reload','content' ], location => qr'^%s/$' },
176 reload_2 => { requests => 3, lines => [ 'get %s','open "/Link \/foo/"','reload','content' ], location => qr'^%s/foo$' },
177 tick => { requests => 2,
178 lines => [ 'get %s','tick cat cat_foo','submit','content' ],
179 location => qr'^%s/formsubmit$' },
180 tick_all => { requests => 2,
181 lines => [ 'get %s','tick cat','submit','content' ],
182 location => qr'^%s/formsubmit$' },
183 timeout => { requests => 1, lines => [ 'timeout 60', 'get %s', 'content' ], location => qr'^%s/' },
184 ua_get => { requests => 1, lines => [ 'ua foo/1.1', 'get %s' ], location => qr'^%s/$' },
185 ua_get_content => { requests => 1, lines => [ 'ua foo/1.1', 'get %s', 'content' ], location => qr'^%s/$' },
186 untick => { requests => 2,
187 lines => [ 'get %s','untick cat cat_foo','submit','content' ],
188 location => qr'^%s/formsubmit$' },
189 untick_all => { requests => 2,
190 lines => [ 'get %s','untick cat','submit','content' ],
191 location => qr'^%s/formsubmit$' },
192 );
193
194 BEGIN {
195 eval {
196 require HTML::TableExtract;
197 $tests{get_table} = { requests => 1, lines => [ 'get %s','table' ], location => qr'^%s/$' };
198 $tests{get_table_params} = { requests => 1, lines => [ 'get %s','table Col2 Col1' ], location => qr'^%s/$' };
199 };
200
201 # To ease zeroing in on tests
202 if (@ARGV) {
203 my $re = join "|", @ARGV;
204 for (sort keys %tests) {
205 delete $tests{$_} unless /$re/o;
206 };
207 };
208 };
209
210 plan tests => (scalar keys %tests)*10;
211 BEGIN {
212 # Disable all ReadLine functionality
213 $ENV{PERL_RL} = 0;
214 #require LWP::UserAgent;
215 #my $old = \&LWP::UserAgent::request;
216 #print STDERR $old;
217 #*LWP::UserAgent::request = sub {print STDERR "LWP::UserAgent::request\n"; goto &$old };
218 };
219 use WWW::Mechanize::Shell;
220
221 SKIP: {
222
223 # We want to be safe from non-resolving local host names
224 delete @ENV{qw(HTTP_PROXY http_proxy CGI_HTTP_PROXY)};
225
226 our ($actual_requests, $dumped_requests );
227 {
228 no warnings qw'redefine once';
229 my $old_request = *WWW::Mechanize::_make_request{CODE};
230 *WWW::Mechanize::_make_request = sub {
231 $actual_requests++;
232 goto &$old_request;
233 };
234
235 *WWW::Mechanize::Shell::status = sub {};
236 *WWW::Mechanize::Shell::request_dumper = sub { $dumped_requests++; return 1 };
237
238 #*Hook::LexWrap::Cleanup::DESTROY = sub {
239 #print STDERR "Disabling hook.\n";
240 #$_[0]->();
241 #};
242 };
243
244 my $server = Test::HTTP::LocalServer->spawn();
245 diag "Spawned local test server at " . $server->url;
246
247 for my $name (sort keys %tests) {
248 $_STDOUT_ = '';
249 undef $_STDERR_;
250 $actual_requests = 0;
251 $dumped_requests = 0;
252 my @lines = @{$tests{$name}->{lines}};
253 my $requests = $tests{$name}->{requests};
254
255 my $code_port = $server->port;
256
257 my $url = $server->url;
258 $url =~ s!/$!!;
259 my $result_location = sprintf $tests{$name}->{location}, quotemeta $url;
260 $result_location = qr{$result_location};
261 my $s = WWW::Mechanize::Shell->new( 'test', rcfile => undef, warnings => undef );
262 $s->option("dumprequests",1);
263 my @commands;
264 eval {
265 for my $line (@lines) {
266 no warnings;
267 $line = sprintf $line, $server->url;
268 push @commands, $line;
269 $s->cmd($line);
270 };
271 };
272 is $@, '', "Commands ran without dieing"
273 or do { diag for @commands };
274 $s->cmd('eval $self->agent->uri');
275 my $code_output = $_STDOUT_;
276 diag join( "\n", $s->history )
277 unless like($s->agent->uri,$result_location,"Shell moved to the specified url for $name");
278
279 my $parameters_ok = 1;
280 my $expected_values = $tests{$name}->{values};
281 for my $valname (sort keys %$expected_values) {
282 if( $s->agent->value( $valname ) !~ /^$expected_values->{ $valname }$/) {
283 $parameters_ok = 0;
284 diag sprintf "Expected '%s', got '%s'",
285 $expected_values->{ $valname },
286 $s->agent->value( $valname ),
287 ;
288 };
289 };
290 is $parameters_ok, 1, "All parameters matched";
291 diag join( "\n", $s->history )
292 unless like($s->agent->uri,$result_location,"Shell moved to the specified url for $name");
293
294 is($_STDERR_,undef,"Shell produced no error output for $name");
295 is($actual_requests,$requests,"$requests requests were made for $name");
296 is($dumped_requests,$requests,"$requests requests were dumped for $name");
297 my $code_requests = $server->get_log;
298
299 # Get a clean start
300 my $script_port = $server->port;
301
302 # Modify the generated Perl script to match the new? port
303 my $script = join "\n", $s->script;
304 s!\b$code_port\b!$script_port!smg for ($script, $code_output);
305 #print STDERR "Releasing hook";
306 undef $s->{request_wrapper};
307 #{
308 # local *WWW::Mechanize::Shell::request_dumper = sub { die };
309 # use HTTP::Request::Common;
310 # $s->agent->request(GET 'http://google.de/');
311 #};
312 $s->release_agent;
313 undef $s;
314
315 # Write the generated Perl script
316 my ($fh,$tempname) = tempfile();
317 print $fh $script;
318 close $fh;
319
320 my ($compile) = `"$^X" -c "$tempname" 2>&1`;
321 chomp $compile;
322 SKIP: {
323 unless (is($compile,"$tempname syntax OK","$name compiles")) {
324 $server->get_log;
325 diag $script;
326 skip "Script $name didn't compile", 2;
327 };
328 my ($output);
329 my $command = qq("$^X" -Iblib/lib "$tempname" 2>&1);
330 $output = `$command`;
331 $output =~ s!^Cookie:.*$!Cookie: <removed>!smg; # cookies get re-ordered, sometimes
332 $code_output =~ s!^Cookie:.*$!Cookie: <removed>!smg; # cookies get re-ordered, sometimes
333 is( $output, $code_output, "Output of $name is identical" )
334 or diag "Script:\n$script";
335 my $script_requests = $server->get_log;
336 $code_requests =~ s!\b$code_port\b!$script_port!smg;
337 $code_requests =~ s!^Cookie:.*$!Cookie: <removed>!smg; # cookies get re-ordered, sometimes
338 $script_requests =~ s!^Cookie:.*$!Cookie: <removed>!smg; # cookies get re-ordered, sometimes
339 is($code_requests,$script_requests,"$name produces identical queries")
340 or diag $script;
341 };
342 unlink $tempname
343 or diag "Couldn't remove tempfile '$name' : $!";
344 };
345 # $server->stop;
346
347 unlink $_ for (<*.save_log_server_test.tmp>);
348
349 };