54dcd3ad54962933f799d056d769cbf4cb1f2735
[WWW-Mechanize-Script.git] / lib / WWW / Mechanize / Script / Plugin / RegexMatchTest.pm
blob54dcd3ad54962933f799d056d769cbf4cb1f2735
1 package WWW::Mechanize::Script::Plugin::RegexMatchTest;
3 use strict;
4 use warnings;
6 use parent qw(WWW::Mechanize::Script::Plugin);
8 # ABSTRACT: check for required/forbidden text via regular expression in response
10 use Params::Util qw(_ARRAY0);
12 our $VERSION = '0.001_003';
14 use 5.014;
16 sub check_value_names
18 return qw(regex_forbid regex_require);
21 sub check_response
23 my ( $self, $check, $mech ) = @_;
25 my $regex_require = $self->get_check_value( $check, "regex_require" );
26 my $regex_forbid = $self->get_check_value( $check, "regex_forbid" );
27 my $ignore_case = $self->get_check_value_as_bool( $check, "ignore_case" );
28 my $content = $mech->is_html() ? $mech->text() : $mech->content();
30 defined($regex_require)
31 and ref($regex_require) ne "ARRAY"
32 and $regex_require = [$regex_require];
33 defined($regex_forbid) and ref($regex_forbid) ne "ARRAY" and $regex_forbid = [$regex_forbid];
35 my @match_fails;
36 my $code = 0;
37 my $case_ign = $ignore_case ? "(?i)" : "";
38 my @msg;
39 foreach my $regex_line ( @{$regex_require} )
41 if ( $content !~ m/$case_ign$regex_line/ )
43 my $err_code = $self->get_check_value( $check, "regex_require_code" ) // 1;
44 $code = &{ $check->{compute_code} }( $code, $err_code );
45 push( @match_fails, $regex_line );
48 @match_fails
49 and push( @msg,
50 "required regex "
51 . join( ", ", map { "'" . $_ . "'" } @match_fails )
52 . " not found in received content" );
54 @match_fails = ();
55 foreach my $regex_line ( @{$regex_forbid} )
57 if ( $content =~ m/$case_ign$regex_line/ )
59 my $err_code = $self->get_check_value( $check, "regex_forbid_code" ) // 1;
60 $code = &{ $check->{compute_code} }( $code, $err_code );
61 push( @match_fails, $regex_line );
64 @match_fails
65 and push( @msg,
66 "forbidden regex "
67 . join( ", ", map { "'" . $_ . "'" } @match_fails )
68 . " found in received content" );
70 if ( $code or @msg )
72 return ( $code, @msg );
75 return (0);
76 return (0);