| 1 | package HTTP::Request::Generator; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Filter::signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Algorithm::Loops 'NestedLoops'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use List::MoreUtils 'zip'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | HTTP::Request::Generator - generate HTTP requests | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use HTTP::Request::Generator 'generate_requests'; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | @requests = generate_requests( | ||||||||||||||||||||||||||||
| 22 | method => 'GET', | ||||||||||||||||||||||||||||
| 23 | pattern => 'https://example.com/{bar,foo,gallery}/[00..99].html', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # generates 300 requests from | ||||||||||||||||||||||||||||
| 27 | # https://example.com/bar/00.html to | ||||||||||||||||||||||||||||
| 28 | # https://example.com/gallery/99.html | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | @requests = generate_requests( | ||||||||||||||||||||||||||||
| 31 | method => 'POST', | ||||||||||||||||||||||||||||
| 32 | host => ['example.com','www.example.com'], | ||||||||||||||||||||||||||||
| 33 | path => '/profiles/:name', | ||||||||||||||||||||||||||||
| 34 | url_params => { | ||||||||||||||||||||||||||||
| 35 | name => ['Corion','Co-Rion'], | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | query_params => { | ||||||||||||||||||||||||||||
| 38 | stars => [2,3], | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | body_params => { | ||||||||||||||||||||||||||||
| 41 | comment => ['Some comment', 'Another comment, A++'], | ||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | headers => [ | ||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | "Content-Type" => 'text/plain; encoding=UTF-8', | ||||||||||||||||||||||||||||
| 46 | Cookie => 'my_session_id', | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | "Content-Type" => 'text/plain; encoding=Latin-1', | ||||||||||||||||||||||||||||
| 50 | Cookie => 'my_session_id', | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | ], | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | # Generates 32 requests out of the combinations | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | for my $req (@requests) { | ||||||||||||||||||||||||||||
| 57 | $ua->request( $req ); | ||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 6 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 63 | 6 | our @EXPORT_OK = qw( generate_requests as_dancer as_plack as_http_request | |||||||||||||||||||||||||||
| 64 | expand_curl_pattern | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 174 | ▶ | sub unwrap($item,$default) { | ||||||||||||||||||||||||||
|
Subroutine
unwrap: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 174 | ▶ | defined $item | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 69 | ? (ref $item ? $item : [$item]) | ||||||||||||||||||||||||||||
| 70 | : $default | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 28 | ▶ | sub fetch_all( $iterator, $limit=0 ) { | ||||||||||||||||||||||||||
|
Subroutine
fetch_all: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 28 | my @res; | |||||||||||||||||||||||||||
| 75 | 28 | ▶ | while( my @r = $iterator->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 202 | push @res, @r; | |||||||||||||||||||||||||||
| 77 | 202 | ▶ | if( $limit && (@res > $limit )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$limit and @res > $limit
MC/DC100%2/2
$limit and @res > $limit
$limit
@res > $limit
Truth table100%3/3
A and B
A= $limitB= @res > $limit
| |||||||||||||||||||||||||||||
| 78 | 1 | splice @res, $limit; | |||||||||||||||||||||||||||
| 79 | last | ||||||||||||||||||||||||||||
| 80 | 1 | }; | |||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||
| 82 | return @res | ||||||||||||||||||||||||||||
| 83 | 28 | }; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | our %defaults = ( | |||||||||||||||||||||||||||
| 86 | method => ['GET'], | ||||||||||||||||||||||||||||
| 87 | path => ['/'], | ||||||||||||||||||||||||||||
| 88 | host => [''], | ||||||||||||||||||||||||||||
| 89 | port => [0], | ||||||||||||||||||||||||||||
| 90 | scheme => ['http'], | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # How can we specify various values for the headers? | ||||||||||||||||||||||||||||
| 93 | headers => [{}], | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | #query_params => [], | ||||||||||||||||||||||||||||
| 96 | #body_params => [], | ||||||||||||||||||||||||||||
| 97 | #url_params => [], | ||||||||||||||||||||||||||||
| 98 | #values => [[]], # the list over which to iterate for *_params | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # We want to skip a set of values if they make a test fail | ||||||||||||||||||||||||||||
| 102 | # if a value appears anywhere with a failing test, skip it elsewhere | ||||||||||||||||||||||||||||
| 103 | # or look at the history to see whether that value has passing tests somewhere | ||||||||||||||||||||||||||||
| 104 | # and then keep it?! | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 70538 | ▶ | sub fill_url( $url, $values, $raw=undef ) { | ||||||||||||||||||||||||||
|
Subroutine
fill_url: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 70538 | ▶ | if( $values ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 70538 | ▶ | if( $raw ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 70490 | ▶ | $url =~ s!:(\w+)!exists $values->{$1} ? $values->{$1} : ":$1"!ge; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 48 | ▶ | $url =~ s!:(\w+)!exists $values->{$1} ? uri_escape($values->{$1}) : ":$1"!ge; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||
| 114 | 70538 | $url | |||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Convert nonref arguments to arrayrefs | ||||||||||||||||||||||||||||
| 118 | 116 | ▶ | sub _makeref { | ||||||||||||||||||||||||||
|
Subroutine
_makeref: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | map { | ||||||||||||||||||||||||||||
| 120 | 116 | ▶ | ref $_ ne 'ARRAY' ? [$_] : $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } @_ | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 17 | ▶ | sub _extract_enum( $name, $item ) { | ||||||||||||||||||||||||||
|
Subroutine
_extract_enum: called CC=6 SCAR=27.8
| |||||||||||||||||||||||||||||
| 125 | # Explicitly enumerate all ranges | ||||||||||||||||||||||||||||
| 126 | 17 | ▶ | my @res = @{ $defaults{ $name } || []}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$HTTP::Request::Generator::defaults{$name} || []
MC/DC0%0/1
$HTTP::Request::Generator::defaults{$name} || []
$HTTP::Request::Generator::defaults{$name}
Truth table50%1/2
A
A= $HTTP::Request::Generator::defaults{$name}
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 17 | ▶ | if( $item ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | # Expand all ranges into the enumerated lists | ||||||||||||||||||||||||||||
| 130 | 0 | $item =~ s!\[([^.\]]+?)\.\.([^.\]]+?)\]!"{" . join(",", $1..$2 )."}"!ge; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Explode all enumerated items into their list | ||||||||||||||||||||||||||||
| 133 | # We should punt this into a(nother) iterator, maybe?! | ||||||||||||||||||||||||||||
| 134 | 1 | ▶ | if( $item =~ /\{.*\}/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | my $changed = 1; | |||||||||||||||||||||||||||
| 136 | 0 | @res = $item; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | while ($changed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | undef $changed; | |||||||||||||||||||||||||||
| 139 | @res = map { | ||||||||||||||||||||||||||||
| 140 | 0 | my $i = $_; | |||||||||||||||||||||||||||
| 141 | 0 | my @r; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | if( $i =~ /^([^{]*)\{([^}]+)\}([^{]*)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | my($pre, $m, $post) = ($1,$2,$3); | |||||||||||||||||||||||||||
| 144 | 0 | $changed = 1; | |||||||||||||||||||||||||||
| 145 | 0 | @r = map { "$pre$_$post" } split /,/, $m, -1; | |||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||
| 147 | 0 | @r = $i | |||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||
| 149 | @r | ||||||||||||||||||||||||||||
| 150 | 0 | } @res; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 1 | @res = $item; | |||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||
| 156 | return \@res | ||||||||||||||||||||||||||||
| 157 | 17 | } | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 17 | ▶ | sub _extract_enum_query( $query ) { | ||||||||||||||||||||||||||
|
Subroutine
_extract_enum_query: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 160 | 17 | my $items = _extract_enum( 'query', $query ); | |||||||||||||||||||||||||||
| 161 | 17 | my %parameters; | |||||||||||||||||||||||||||
| 162 | 17 | for my $q (@$items) { | |||||||||||||||||||||||||||
| 163 | 1 | my $u = URI->new('example.com', 'http'); | |||||||||||||||||||||||||||
| 164 | 1 | $u->query($q); | |||||||||||||||||||||||||||
| 165 | 1 | my %f = $u->query_form; | |||||||||||||||||||||||||||
| 166 | 1 | for my $k (keys %f) { | |||||||||||||||||||||||||||
| 167 | 2 | $parameters{ $k }->{ $f{ $k }} = 1; | |||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 17 | for my $v (values %parameters) { | |||||||||||||||||||||||||||
| 172 | 2 | $v = [ sort keys %$v ]; | |||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 17 | \%parameters | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 C<< expand_curl_pattern >> | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | my %res = expand_curl_pattern( 'https://' ); | ||||||||||||||||||||||||||||
| 182 | # | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Expands a curl-style pattern to a pattern using positional placeholders. | ||||||||||||||||||||||||||||
| 185 | See the C<curl> documentation on the patterns. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 17 | ▶ | sub expand_curl_pattern( $pattern ) { | ||||||||||||||||||||||||||
|
Subroutine
expand_curl_pattern: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 17 | my %ranges; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Split up the URL pattern into a scheme, host(pattern), port number and | ||||||||||||||||||||||||||||
| 193 | # path (pattern) | ||||||||||||||||||||||||||||
| 194 | #use Regexp::Debugger; | ||||||||||||||||||||||||||||
| 195 | 17 | my $ipv4_digit = '(?:[01]?\d?\d|25[0-5]|2[0-4]\d)'; | |||||||||||||||||||||||||||
| 196 | 17 | my $ipv4 = "(?:$ipv4_digit\\.$ipv4_digit\\.$ipv4_digit\\.$ipv4_digit)"; | |||||||||||||||||||||||||||
| 197 | 17 | my $ipv6s = qr!(?:[\da-fA-F]{1,4})!; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 17 | my( $scheme, $host, $port, $path, $query ) | |||||||||||||||||||||||||||
| 200 | = $pattern =~ m!^(?:([^:]+):)? # scheme | ||||||||||||||||||||||||||||
| 201 | /?/? # optional? slashes | ||||||||||||||||||||||||||||
| 202 | ( # hostname | ||||||||||||||||||||||||||||
| 203 | \[(?: # ipv6 | ||||||||||||||||||||||||||||
| 204 | (?:$ipv6s ){1,1}(?::$ipv6s){7,7} # plain ipv6 | ||||||||||||||||||||||||||||
| 205 | |(?:$ipv6s:){1,7}(?:: ){1,1} # plain ipv6 | ||||||||||||||||||||||||||||
| 206 | |(?:$ipv6s:){1,6}(?::$ipv6s){1,1} # plain ipv6 | ||||||||||||||||||||||||||||
| 207 | |(?:$ipv6s:){1,5}(?::$ipv6s){1,2} # plain ipv6 | ||||||||||||||||||||||||||||
| 208 | |(?:$ipv6s:){1,4}(?::$ipv6s){1,3} # plain ipv6 | ||||||||||||||||||||||||||||
| 209 | |(?:$ipv6s:){1,3}(?::$ipv6s){1,4} # plain ipv6 | ||||||||||||||||||||||||||||
| 210 | |(?:$ipv6s:){1,2}(?::$ipv6s){1,5} # plain ipv6 | ||||||||||||||||||||||||||||
| 211 | |(?:$ipv6s:){1,1}(?::$ipv6s){1,6} # plain ipv6 | ||||||||||||||||||||||||||||
| 212 | |(?: :){1,1}(?::$ipv6s){1,7} # plain ipv6 | ||||||||||||||||||||||||||||
| 213 | |fe80:(?:$ipv6s){0,4}%%[0-9a-zA-Z]+ # link local+index | ||||||||||||||||||||||||||||
| 214 | |(?:(?:0{1,4}:){1,5}|::)(?:ffff:(?:0{1,4}:)?)$ipv4 # ipv4-in-ipv6 | ||||||||||||||||||||||||||||
| 215 | )\] | ||||||||||||||||||||||||||||
| 216 | |[^/:\[]+ | ||||||||||||||||||||||||||||
| 217 | (?:\[[^/\]]+\][^/:\[]*)* | ||||||||||||||||||||||||||||
| 218 | (?=[:/]|$) # plain, or expansion | ||||||||||||||||||||||||||||
| 219 | |\[[^:\]]+\][^/:]* # expansion | ||||||||||||||||||||||||||||
| 220 | ) | ||||||||||||||||||||||||||||
| 221 | (?::(\d+))? # optional port | ||||||||||||||||||||||||||||
| 222 | ([^?]*) # path | ||||||||||||||||||||||||||||
| 223 | (?:\?(.*))? # optional query part | ||||||||||||||||||||||||||||
| 224 | $!xi; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # Explicitly enumerate all ranges | ||||||||||||||||||||||||||||
| 227 | 17 | my $idx = 0; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 17 | ▶ | if( $scheme ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | $scheme =~ s!\[([^.\]]+?)\.\.([^.\[]+?)\]!$ranges{$idx} = [$1..$2]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 231 | 16 | $scheme =~ s!\{([^\}]*)\}!$ranges{$idx} = [split /,/, $1, -1]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 232 | }; | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 17 | ▶ | if( $host ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 17 | $host =~ s!\[([^.\]]+?)\.\.([^.\[]+?)\]!$ranges{$idx} = [$1..$2]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 236 | 17 | $host =~ s!\{([^\}]*)\}!$ranges{$idx} = [split /,/, $1, -1]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 237 | }; | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 17 | ▶ | if( $port ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 0 | $port =~ s!\[([^.\]]+?)\.\.([^.\[]+?)\]!$ranges{$idx} = [$1..$2]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 241 | 0 | $port =~ s!\{([^\}]*)\}!$ranges{$idx} = [split /,/, $1, -1]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 242 | }; | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 17 | ▶ | if( $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 13 | $path =~ s!\[([^.\]]+?)\.\.([^.\[]+?)\]!$ranges{$idx} = [$1..$2]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 246 | # Move all explicitly enumerated parts into lists: | ||||||||||||||||||||||||||||
| 247 | 13 | $path =~ s!\{([^\}]*)\}!$ranges{$idx} = [split /,/, $1, -1]; ":".$idx++!ge; | |||||||||||||||||||||||||||
| 248 | }; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 17 | my %res = ( | |||||||||||||||||||||||||||
| 251 | url_params => \%ranges, | ||||||||||||||||||||||||||||
| 252 | host => $host, | ||||||||||||||||||||||||||||
| 253 | scheme => $scheme, | ||||||||||||||||||||||||||||
| 254 | port => $port, | ||||||||||||||||||||||||||||
| 255 | path => $path, | ||||||||||||||||||||||||||||
| 256 | query_params => _extract_enum_query( $query ), | ||||||||||||||||||||||||||||
| 257 | raw_params => 1, | ||||||||||||||||||||||||||||
| 258 | ); | ||||||||||||||||||||||||||||
| 259 | 17 | %res | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 29 | ▶ | sub _generate_requests_iter(%options) { | ||||||||||||||||||||||||||
|
Subroutine
_generate_requests_iter: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 263 | 29 | ▶ | my $wrapper = delete $options{ wrap } || sub { wantarray ? @_ : $_[0]}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
Condition66%2/3
delete $options{'wrap'} || sub {
wantarray ? @_ : $_[0];
}
MC/DC0%0/2
delete $options{'wrap'} || sub {
wantarray ? @_ : $_[0];
}
delete $options{'wrap'}
sub {
wantarray ? @_ : $_[0];
}
Truth table66%2/3
A || B
A= delete $options{'wrap'}B= sub {
wantarray ? @_ : $_[0];
}
| |||||||||||||||||||||||||||||
| 264 | 29 | my @keys = sort keys %defaults; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 29 | ▶ | if( my $pattern = delete $options{ pattern }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 17 | %options = (%options, expand_curl_pattern( $pattern )); | |||||||||||||||||||||||||||
| 268 | }; | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 29 | ▶ | my $query_params = $options{ query_params } || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'query_params'} || {}
MC/DC100%1/1
$options{'query_params'} || {}
$options{'query_params'}
Truth table100%2/2
A
A= $options{'query_params'}
| |||||||||||||||||||||||||||||
| 271 | 29 | ▶ | my $body_params = $options{ body_params } || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'body_params'} || {}
MC/DC100%1/1
$options{'body_params'} || {}
$options{'body_params'}
Truth table100%2/2
A
A= $options{'body_params'}
| |||||||||||||||||||||||||||||
| 272 | 29 | ▶ | my $url_params = $options{ url_params } || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'url_params'} || {}
MC/DC100%1/1
$options{'url_params'} || {}
$options{'url_params'}
Truth table100%2/2
A
A= $options{'url_params'}
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | $options{ "fixed_$_" } ||= {} | ||||||||||||||||||||||||||||
| 275 | 29 | ▶ | for @keys; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options{"fixed_$_"} ||= {}
MC/DC0%0/1
$options{"fixed_$_"} ||= {}
$options{"fixed_$_"}
Truth table50%1/2
A
A= $options{"fixed_$_"}
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # Now only iterate over the non-empty lists | ||||||||||||||||||||||||||||
| 278 | 29 | my %args = map { my @v = unwrap($options{ $_ }, [@{$defaults{ $_ }}]); | |||||||||||||||||||||||||||
| 279 | 174 | ▶ | @v ? ($_ => @v) : () } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | @keys; | ||||||||||||||||||||||||||||
| 281 | 29 | @keys = sort keys %args; # somewhat predictable | |||||||||||||||||||||||||||
| 282 | $args{ $_ } ||= {} | ||||||||||||||||||||||||||||
| 283 | 29 | ▶ | for qw(query_params body_params url_params); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{$_} ||= {}
MC/DC0%0/1
$args{$_} ||= {}
$args{$_}
Truth table50%1/2
A
A= $args{$_}
| |||||||||||||||||||||||||||||
| 284 | 29 | my @loops = _makeref @args{ @keys }; | |||||||||||||||||||||||||||
| 285 | #use Data::Dumper; warn Dumper \@loops, \@keys, \%options; | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # Turn all query_params into additional loops for each entry in keys %$query_params | ||||||||||||||||||||||||||||
| 288 | # Turn all body_params into additional loops over keys %$body_params | ||||||||||||||||||||||||||||
| 289 | 29 | my @query_params = keys %$query_params; | |||||||||||||||||||||||||||
| 290 | 29 | push @loops, _makeref values %$query_params; | |||||||||||||||||||||||||||
| 291 | 29 | my @body_params = keys %$body_params; | |||||||||||||||||||||||||||
| 292 | 29 | push @loops, _makeref values %$body_params; | |||||||||||||||||||||||||||
| 293 | 29 | my @url_params = keys %$url_params; | |||||||||||||||||||||||||||
| 294 | 29 | push @loops, _makeref values %$url_params; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | #use Data::Dumper; warn "Looping over " . Dumper \@loops; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 29 | my $iter = NestedLoops(\@loops,{}); | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Set up the fixed parts | ||||||||||||||||||||||||||||
| 301 | 29 | my %template; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 29 | for(qw(query_params body_params headers)) { | |||||||||||||||||||||||||||
| 304 | 87 | ▶ | $template{ $_ } = $options{ "fixed_$_" } || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{"fixed_$_"} || {}
MC/DC100%1/1
$options{"fixed_$_"} || {}
$options{"fixed_$_"}
Truth table100%2/2
A
A= $options{"fixed_$_"}
| |||||||||||||||||||||||||||||
| 305 | }; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 17806 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 308 | 17806 | my @v = $iter->(); | |||||||||||||||||||||||||||
| 309 | 17806 | ▶ | return unless @v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | #use Data::Dumper; warn Dumper \@v; | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | # Patch in the new values | ||||||||||||||||||||||||||||
| 313 | 17778 | my %values = %template; | |||||||||||||||||||||||||||
| 314 | 17778 | my @vv = splice @v, 0, 0+@keys; | |||||||||||||||||||||||||||
| 315 | 17778 | @values{ @keys } = @vv; | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | # Now add the query_params, if any | ||||||||||||||||||||||||||||
| 318 | 17778 | ▶ | if(@query_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 126 | my @get_values = splice @v, 0, 0+@query_params; | |||||||||||||||||||||||||||
| 320 | 126 | $values{ query_params } = { (%{ $values{ query_params } }, zip( @query_params, @get_values )) }; | |||||||||||||||||||||||||||
| 321 | }; | ||||||||||||||||||||||||||||
| 322 | # Now add the body_params, if any | ||||||||||||||||||||||||||||
| 323 | 17778 | ▶ | if(@body_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 12 | my @values = splice @v, 0, 0+@body_params; | |||||||||||||||||||||||||||
| 325 | 12 | $values{ body_params } = { %{ $values{ body_params } }, zip @body_params, @values }; | |||||||||||||||||||||||||||
| 326 | }; | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | # Recreate the URL with the substituted values | ||||||||||||||||||||||||||||
| 329 | 17778 | ▶ | if( @url_params ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 17636 | my %v; | |||||||||||||||||||||||||||
| 331 | 17636 | @v{ @url_params } = splice @v, 0, 0+@url_params; | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 17636 | for my $key (qw(scheme port path host)) { | |||||||||||||||||||||||||||
| 334 | 70544 | ▶ | if( $key ne 'host' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key ne "host" or not $values{$key} =~ /^\[.*\]$/
MC/DC100%2/2
$key ne "host" or not $values{$key} =~ /^\[.*\]$/
$key ne "host"
not $values{$key} =~ /^\[.*\]$/
Truth table100%3/3
A or not(B)
A= $key ne "host"B= not $values{$key} =~ /^\[.*\]$/
| |||||||||||||||||||||||||||||
| 335 | or $values{ $key } !~ /^\[.*\]$/ ) { | ||||||||||||||||||||||||||||
| 336 | 70538 | $values{ $key } = fill_url($values{ $key }, \%v, $options{ raw_params }); | |||||||||||||||||||||||||||
| 337 | }; | ||||||||||||||||||||||||||||
| 338 | }; | ||||||||||||||||||||||||||||
| 339 | }; | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 17778 | $values{ url } = _build_uri( \%values ); | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # Merge the headers as well | ||||||||||||||||||||||||||||
| 344 | #warn "Merging headers: " . Dumper($values{headers}). " + " . (Dumper $template{headers}); | ||||||||||||||||||||||||||||
| 345 | 17778 | ▶ | %{$values{headers}} = (%{$template{headers}}, %{$values{headers} || {}}); | ||||||||||||||||||||||||||
|
Condition50%1/2
$values{'headers'} || {}
MC/DC0%0/1
$values{'headers'} || {}
$values{'headers'}
Truth table50%1/2
A
A= $values{'headers'}
| |||||||||||||||||||||||||||||
| 346 | 17778 | return $wrapper->(\%values); | |||||||||||||||||||||||||||
| 347 | 29 | }; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 17790 | ▶ | sub _build_uri( $req ) { | ||||||||||||||||||||||||||
|
Subroutine
_build_uri: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 351 | 17790 | my $uri = URI->new( '', $req->{scheme} ); | |||||||||||||||||||||||||||
| 352 | 17790 | ▶ | if( $req->{host}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | 17650 | $uri->host( $req->{host}); | |||||||||||||||||||||||||||
| 354 | 17650 | $uri->scheme( $req->{scheme}); | |||||||||||||||||||||||||||
| 355 | 17650 | ▶ | $uri->port( $req->{port}) if( $req->{port} and $req->{port} != $uri->default_port ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$req->{"port"} and $req->{"port"} != $uri->default_port
MC/DC100%2/2
$req->{"port"} and $req->{"port"} != $uri->default_port
$req->{"port"}
$req->{"port"} != $uri->default_port
Truth table100%3/3
A and B
A= $req->{"port"}B= $req->{"port"} != $uri->default_port
| |||||||||||||||||||||||||||||
| 356 | }; | ||||||||||||||||||||||||||||
| 357 | 17790 | $uri->path( $req->{path}); | |||||||||||||||||||||||||||
| 358 | # We want predictable URIs, so we sort the keys here instead of | ||||||||||||||||||||||||||||
| 359 | # just passing the hash reference | ||||||||||||||||||||||||||||
| 360 | 17790 | $uri->query_form( map { $_ => $req->{query_params}->{$_} } sort keys %{ $req->{query_params} }); | |||||||||||||||||||||||||||
| 361 | #$uri->query_form( $req->{query_params}); | ||||||||||||||||||||||||||||
| 362 | 17790 | $uri | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head2 C<< generate_requests( %options ) >> | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | my $g = generate_requests( | ||||||||||||||||||||||||||||
| 368 | url => '/profiles/:name', | ||||||||||||||||||||||||||||
| 369 | url_params => ['Mark','John'], | ||||||||||||||||||||||||||||
| 370 | wrap => sub { | ||||||||||||||||||||||||||||
| 371 | my( $req ) = @_; | ||||||||||||||||||||||||||||
| 372 | # Fix up some values | ||||||||||||||||||||||||||||
| 373 | $req->{headers}->{'Content-Length'} = 666; | ||||||||||||||||||||||||||||
| 374 | return $req; | ||||||||||||||||||||||||||||
| 375 | }, | ||||||||||||||||||||||||||||
| 376 | ); | ||||||||||||||||||||||||||||
| 377 | while( my $r = $g->()) { | ||||||||||||||||||||||||||||
| 378 | send_request( $r ); | ||||||||||||||||||||||||||||
| 379 | }; | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | This function creates data structures that are suitable for sending off | ||||||||||||||||||||||||||||
| 382 | a mass of similar but different HTTP requests. All array references are expanded | ||||||||||||||||||||||||||||
| 383 | into the cartesian product of their contents. The above example would create | ||||||||||||||||||||||||||||
| 384 | two requests: | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | url => '/profiles/Mark, | ||||||||||||||||||||||||||||
| 387 | url => '/profiles/John', | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | C<generate_requests> returns an iterator in scalar context. In list context, it | ||||||||||||||||||||||||||||
| 390 | returns the complete list of requests: | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | my @requests = generate_requests( | ||||||||||||||||||||||||||||
| 393 | url => '/profiles/:name', | ||||||||||||||||||||||||||||
| 394 | url_params => ['Mark','John'], | ||||||||||||||||||||||||||||
| 395 | wrap => sub { | ||||||||||||||||||||||||||||
| 396 | my( $req ) = @_; | ||||||||||||||||||||||||||||
| 397 | # Fix up some values | ||||||||||||||||||||||||||||
| 398 | $req->{headers}->{'Content-Length'} = 666; | ||||||||||||||||||||||||||||
| 399 | return $req; | ||||||||||||||||||||||||||||
| 400 | }, | ||||||||||||||||||||||||||||
| 401 | ); | ||||||||||||||||||||||||||||
| 402 | for my $r (@requests) { | ||||||||||||||||||||||||||||
| 403 | send_request( $r ); | ||||||||||||||||||||||||||||
| 404 | }; | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | Note that returning a list instead of the iterator will use up quite some memory | ||||||||||||||||||||||||||||
| 407 | quickly, as the list will be the cartesian product of the input parameters. | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | There are helper functions | ||||||||||||||||||||||||||||
| 410 | that will turn that data into a data structure suitable for your HTTP framework | ||||||||||||||||||||||||||||
| 411 | of choice. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | { | ||||||||||||||||||||||||||||
| 414 | method => 'GET', | ||||||||||||||||||||||||||||
| 415 | url => '/profiles/Mark', | ||||||||||||||||||||||||||||
| 416 | scheme => 'http', | ||||||||||||||||||||||||||||
| 417 | port => 80, | ||||||||||||||||||||||||||||
| 418 | headers => {}, | ||||||||||||||||||||||||||||
| 419 | body_params => {}, | ||||||||||||||||||||||||||||
| 420 | query_params => {}, | ||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | As a shorthand for creating lists, you can use the C<pattern> option, which | ||||||||||||||||||||||||||||
| 424 | will expand a string into a set of requests. C<{}> will expand into alternatives | ||||||||||||||||||||||||||||
| 425 | while C<[xx..yy]> will expand into the range C<xx> to C<yy>. Note that these | ||||||||||||||||||||||||||||
| 426 | lists will be expanded in memory. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =head3 Options | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =over 4 | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =item B<pattern> | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | pattern => 'https://example.{com,org,net}/page_[00..99].html', | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | Generate URLs from this pattern instead of C<query_params>, C<url_params> | ||||||||||||||||||||||||||||
| 437 | and C<url>. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =item B<url> | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | URL template to use. | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =item B<url_params> | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | Parameters to replace in the C<url> template. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =item B<body_params> | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | Parameters to replace in the POST body. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =item B<query_params> | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Parameters to replace in the GET request. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =item B<host> | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | Hostname(s) to use. | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =item B<port> | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | Port(s) to use. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =item B<headers> | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | Headers to use. Currently, no templates are generated for the headers. You have | ||||||||||||||||||||||||||||
| 466 | to specify complete sets of headers for each alternative. | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =item B<limit> | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | Limit the number of requests generated. | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | =back | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =cut | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 29 | ▶ | sub generate_requests(%options) { | ||||||||||||||||||||||||||
|
Subroutine
generate_requests: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 477 | croak "Option 'protocol' is now named 'scheme'." | ||||||||||||||||||||||||||||
| 478 | 29 | ▶ | if $options{ protocol }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 29 | my $i = _generate_requests_iter(%options); | |||||||||||||||||||||||||||
| 481 | 29 | ▶ | if( wantarray ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | 28 | return fetch_all($i, $options{ limit }); | |||||||||||||||||||||||||||
| 483 | } else { | ||||||||||||||||||||||||||||
| 484 | 1 | return $i | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =head2 C<< as_http_request >> | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | generate_requests( | ||||||||||||||||||||||||||||
| 491 | method => 'POST', | ||||||||||||||||||||||||||||
| 492 | url => '/feedback/:item', | ||||||||||||||||||||||||||||
| 493 | wrap => \&HTTP::Request::Generator::as_http_request, | ||||||||||||||||||||||||||||
| 494 | ) | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | Converts the request data to a L<HTTP::Request> object. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =cut | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 12 | ▶ | sub as_http_request($req) { | ||||||||||||||||||||||||||
|
Subroutine
as_http_request: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 501 | 12 | require HTTP::Request; | |||||||||||||||||||||||||||
| 502 | 12 | HTTP::Request->VERSION(6); # ->flatten() | |||||||||||||||||||||||||||
| 503 | 12 | require URI; | |||||||||||||||||||||||||||
| 504 | 12 | require URI::QueryParam; | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 12 | my $body = ''; | |||||||||||||||||||||||||||
| 507 | 12 | my $headers; | |||||||||||||||||||||||||||
| 508 | my $form_ct; | ||||||||||||||||||||||||||||
| 509 | 12 | ▶ | if( keys %{$req->{body_params}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | 4 | require HTTP::Request::Common; | |||||||||||||||||||||||||||
| 511 | my $r = HTTP::Request::Common::POST( $req->{url}, | ||||||||||||||||||||||||||||
| 512 | 4 | [ %{ $req->{body_params} }], | |||||||||||||||||||||||||||
| 513 | ); | ||||||||||||||||||||||||||||
| 514 | 4 | $headers = HTTP::Headers->new( %{ $req->{headers} }, $r->headers->flatten ); | |||||||||||||||||||||||||||
| 515 | 4 | $body = $r->content; | |||||||||||||||||||||||||||
| 516 | 4 | $form_ct = $r->content_type; | |||||||||||||||||||||||||||
| 517 | } else { | ||||||||||||||||||||||||||||
| 518 | 8 | $headers = HTTP::Headers->new( %$headers ); | |||||||||||||||||||||||||||
| 519 | }; | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | # Store metadata / generate "signature" for later inspection/isolation? | ||||||||||||||||||||||||||||
| 522 | 12 | my $uri = _build_uri( $req ); | |||||||||||||||||||||||||||
| 523 | 12 | ▶ | $uri->query_param( %{ $req->{query_params} || {} }); | ||||||||||||||||||||||||||
|
Condition50%1/2
$req->{'query_params'} || {}
MC/DC0%0/1
$req->{'query_params'} || {}
$req->{'query_params'}
Truth table50%1/2
A
A= $req->{'query_params'}
| |||||||||||||||||||||||||||||
| 524 | my $res = HTTP::Request->new( | ||||||||||||||||||||||||||||
| 525 | 12 | $req->{method} => $uri, | |||||||||||||||||||||||||||
| 526 | $headers, | ||||||||||||||||||||||||||||
| 527 | $body, | ||||||||||||||||||||||||||||
| 528 | ); | ||||||||||||||||||||||||||||
| 529 | 12 | $res | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head2 C<< as_dancer >> | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | generate_requests( | ||||||||||||||||||||||||||||
| 535 | method => 'POST', | ||||||||||||||||||||||||||||
| 536 | url => '/feedback/:item', | ||||||||||||||||||||||||||||
| 537 | wrap => \&HTTP::Request::Generator::as_dancer, | ||||||||||||||||||||||||||||
| 538 | ) | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | Converts the request data to a L<Dancer::Request> object. | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | During the creation of Dancer::Request objects, C<< %ENV >> will be empty except | ||||||||||||||||||||||||||||
| 543 | for C<< $ENV{TMP} >> and C<< $ENV{TEMP} >>. | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | This function needs and dynamically loads the following modules: | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | L<Dancer::Request> | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | L<HTTP::Request> | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =cut | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | 0 | ▶ | sub as_dancer($req) { | ||||||||||||||||||||||||||
|
Subroutine
as_dancer: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 555 | 0 | require Dancer::Request; | |||||||||||||||||||||||||||
| 556 | 0 | require HTTP::Request; | |||||||||||||||||||||||||||
| 557 | 0 | HTTP::Request->VERSION(6); # ->flatten() | |||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | 0 | my $body = ''; | |||||||||||||||||||||||||||
| 560 | 0 | my $headers; | |||||||||||||||||||||||||||
| 561 | my $form_ct; | ||||||||||||||||||||||||||||
| 562 | 0 | ▶ | if( keys %{$req->{body_params}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 0 | require HTTP::Request::Common; | |||||||||||||||||||||||||||
| 564 | my $r = HTTP::Request::Common::POST( $req->{url}, | ||||||||||||||||||||||||||||
| 565 | 0 | [ %{ $req->{body_params} }], | |||||||||||||||||||||||||||
| 566 | ); | ||||||||||||||||||||||||||||
| 567 | 0 | $headers = HTTP::Headers->new( %{ $req->{headers} }, $r->headers->flatten ); | |||||||||||||||||||||||||||
| 568 | 0 | $body = $r->content; | |||||||||||||||||||||||||||
| 569 | 0 | $form_ct = $r->content_type; | |||||||||||||||||||||||||||
| 570 | } else { | ||||||||||||||||||||||||||||
| 571 | 0 | $headers = HTTP::Headers->new( %$headers ); | |||||||||||||||||||||||||||
| 572 | }; | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | 0 | my $uri = _build_uri( $req ); | |||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | # Store metadata / generate "signature" for later inspection/isolation? | ||||||||||||||||||||||||||||
| 577 | 0 | my %old_ENV = %ENV; | |||||||||||||||||||||||||||
| 578 | 0 | local %ENV; # wipe out non-overridable default variables of Dancer::Request | |||||||||||||||||||||||||||
| 579 | 0 | my @keep = (qw(TMP TEMP)); | |||||||||||||||||||||||||||
| 580 | 0 | @ENV{ @keep } = @old_ENV{ @keep }; | |||||||||||||||||||||||||||
| 581 | my $res = Dancer::Request->new_for_request( | ||||||||||||||||||||||||||||
| 582 | $req->{method} => $uri->path, | ||||||||||||||||||||||||||||
| 583 | $req->{query_params}, | ||||||||||||||||||||||||||||
| 584 | $body, | ||||||||||||||||||||||||||||
| 585 | $headers, | ||||||||||||||||||||||||||||
| 586 | { CONTENT_LENGTH => length($body), | ||||||||||||||||||||||||||||
| 587 | CONTENT_TYPE => $form_ct, | ||||||||||||||||||||||||||||
| 588 | HTTP_HOST => (join ":", $req->{host}, $req->{port}), | ||||||||||||||||||||||||||||
| 589 | SERVER_NAME => $req->{host}, | ||||||||||||||||||||||||||||
| 590 | SERVER_PORT => $req->{port}, | ||||||||||||||||||||||||||||
| 591 | REQUEST_METHOD => $req->{ method }, | ||||||||||||||||||||||||||||
| 592 | 0 | REQUEST_URI => $uri, | |||||||||||||||||||||||||||
| 593 | SCRIPT_NAME => $uri->path, | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | }, | ||||||||||||||||||||||||||||
| 596 | ); | ||||||||||||||||||||||||||||
| 597 | 0 | $res->{_http_body}->add($body); | |||||||||||||||||||||||||||
| 598 | #use Data::Dumper; warn Dumper $res; | ||||||||||||||||||||||||||||
| 599 | 0 | $res | |||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | =head2 C<< as_plack >> | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | generate_requests( | ||||||||||||||||||||||||||||
| 605 | method => 'POST', | ||||||||||||||||||||||||||||
| 606 | url => '/feedback/:item', | ||||||||||||||||||||||||||||
| 607 | wrap => \&HTTP::Request::Generator::as_plack, | ||||||||||||||||||||||||||||
| 608 | ) | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | Converts the request data to a L<Plack::Request> object. | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | During the creation of Plack::Request objects, C<< %ENV >> will be empty except | ||||||||||||||||||||||||||||
| 613 | for C<< $ENV{TMP} >> and C<< $ENV{TEMP} >>. | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | This function needs and dynamically loads the following modules: | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | L<Plack::Request> | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | L<HTTP::Headers> | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | L<Hash::MultiValue> | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | =cut | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 0 | ▶ | sub as_plack($req) { | ||||||||||||||||||||||||||
|
Subroutine
as_plack: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 626 | 0 | require Plack::Request; | |||||||||||||||||||||||||||
| 627 | 0 | Plack::Request->VERSION(1.0047); | |||||||||||||||||||||||||||
| 628 | 0 | require HTTP::Headers; | |||||||||||||||||||||||||||
| 629 | 0 | require Hash::MultiValue; | |||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 0 | my %env = %$req; | |||||||||||||||||||||||||||
| 632 | 0 | $env{ 'psgi.version' } = '1.0'; | |||||||||||||||||||||||||||
| 633 | 0 | $env{ 'psgi.url_scheme' } = delete $env{ scheme }; | |||||||||||||||||||||||||||
| 634 | 0 | ▶ | $env{ 'plack.request.query_parameters' } = [%{delete $env{ query_params }||{}} ]; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $env{'query_params'} || {}
MC/DC0%0/1
delete $env{'query_params'} || {}
delete $env{'query_params'}
Truth table0%0/2
A
A= delete $env{'query_params'}
| |||||||||||||||||||||||||||||
| 635 | 0 | ▶ | $env{ 'plack.request.body_parameters' } = [%{delete $env{ body_params }||{}} ]; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $env{'body_params'} || {}
MC/DC0%0/1
delete $env{'body_params'} || {}
delete $env{'body_params'}
Truth table0%0/2
A
A= delete $env{'body_params'}
| |||||||||||||||||||||||||||||
| 636 | 0 | $env{ 'plack.request.headers' } = HTTP::Headers->new( %{ delete $req->{headers} }); | |||||||||||||||||||||||||||
| 637 | 0 | $env{ REQUEST_METHOD } = delete $env{ method }; | |||||||||||||||||||||||||||
| 638 | 0 | $env{ REQUEST_URI } = _build_uri( $req ); | |||||||||||||||||||||||||||
| 639 | 0 | $env{ SCRIPT_NAME } = $env{ REQUEST_URI }->path; | |||||||||||||||||||||||||||
| 640 | 0 | delete $env{ url }; | |||||||||||||||||||||||||||
| 641 | 0 | $env{ QUERY_STRING } = ''; # not correct, but... | |||||||||||||||||||||||||||
| 642 | 0 | $env{ SERVER_NAME } = delete $env{ host }; | |||||||||||||||||||||||||||
| 643 | 0 | $env{ SERVER_PORT } = delete $env{ port }; | |||||||||||||||||||||||||||
| 644 | # need to convert the headers into %env HTTP_ keys here | ||||||||||||||||||||||||||||
| 645 | 0 | $env{ CONTENT_TYPE } = undef; | |||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | # Store metadata / generate "signature" for later inspection/isolation? | ||||||||||||||||||||||||||||
| 648 | 0 | my %old_ENV = %ENV; | |||||||||||||||||||||||||||
| 649 | 0 | local %ENV; # wipe out non-overridable default variables of Dancer::Request | |||||||||||||||||||||||||||
| 650 | 0 | my @keep = (qw(TMP TEMP)); | |||||||||||||||||||||||||||
| 651 | 0 | @ENV{ @keep } = @old_ENV{ @keep }; | |||||||||||||||||||||||||||
| 652 | 0 | my $res = Plack::Request->new(\%env); | |||||||||||||||||||||||||||
| 653 | 0 | $res | |||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | 6 | 1; | |||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | L<The Curl Manpage|https://curl.haxx.se/docs/manpage.html> for the pattern syntax | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | The public repository of this module is | ||||||||||||||||||||||||||||
| 665 | L<https://github.com/Corion/HTTP-Request-Generator>. | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | The public support forum of this module is L<https://perlmonks.org/>. | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | =head1 BUG TRACKER | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||||||||||
| 674 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Request-Generator> | ||||||||||||||||||||||||||||
| 675 | or via mail to L<HTTP-Request-Generator-Bugs@rt.cpan.org|mailto:HTTP-Request-Generator-Bugs@rt.cpan.org>. | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | Max Maischein C<corion@cpan.org> | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | =head1 COPYRIGHT (c) | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | Copyright 2017-2019 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | =cut | ||||||||||||||||||||||||||||