lib/HTTP/Request/FromFetch.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 66.6% 4 / 6 Conditioncond 80.0% 12 / 15 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 87.1% 68 / 78 CC 11 SCAR 20.0
CC
11
Cov
90%
new
11.4
BEGIN
1.0
BEGIN
1.0
CRAP 7.4
« lib/HTTP/Request/FromCurl.pm ↑ Summary script/pcurl.pl »
1 package HTTP::Request::FromFetch 0.57;
2 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request::CurlParameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 HTTP::Request::FromFetch - turn a Javascript fetch() statement into HTTP::Request
13  
14 =head1 SYNOPSIS
15  
16 my $ua = LWP::UserAgent->new();
17 my $req = HTTP::Request::FromFetch->new(<<'JS')->as_request;
18  
19 await fetch("https://www.example.com/index.html", {
20 "credentials": "include",
21 "headers": {
22 "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0",
23 "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
24 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
25 "X-CSRF-Token": "secret",
26 "X-Requested-With": "XMLHttpRequest"
27 },
28 "referrer": "https://www.example.com/",
29 "method": "GET",
30 "mode": "cors"
31 });
32  
33 JS
34 $ua->request( $req );
35  
36 =head1 DESCRIPTION
37  
38 This module parses a call to the L<Javascript Fetch API|https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>
39 and returns an object that you can turn into a L<HTTP::Request> to use
40 with L<LWP::UserAgent> or other user agents to perform a largely identical
41 HTTP request.
42  
43 The parsing of the Javascript stanza is done through a regular expression, so
44 the test must largely follow the pattern shown in the synopsis. Usually, the
45 C<fetch()> stanzas come from a browsers "Copy as fetch" context menu, so there
46 is no problem parsing these.
47  
48 This is mostly a factory class for L<HTTP::Request::CurlParameters> objects.
49  
50 =cut
51  
52 5 ▶sub new( $class, $fetch, @rest ) {
Subroutine new: called CC=11 SCAR=24.3
Pod undocumented
53 5 my %options;
54  
55 5 ▶ if( @rest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) { } 0 5 5
56 0 %options = ($fetch, @rest);
57 } else {
58 5 $options{ fetch } = $fetch;
59 };
60  
61 5 $fetch = delete $options{ fetch };
62  
63 5 ▶ $fetch =~ m!\A\s*(await\s+)?
Branch: 1 branch
BranchTrueFalseTotal
$fetch =~ /\A\s*(await\s+)? fetch\s*\(\s*"(?<uri>(?:[^[\\"]+|\\.)+)"\s*(?:,\s* (?<options>\{.*\}))?\s* \)\s*;? \s*\z/msux or croak("Couldn't parse fetch string '${fetch}'") 5 0 5
64 fetch\s*\(\s*"(?<uri>(?:[^[\\"]+|\\.)+)"\s*(?:,\s*
65 (?<options>\{.*\}))?\s*
66 \)\s*;?
67 \s*\z!msx
68 or croak "Couldn't parse fetch string '$fetch'";
69  
70 5 my $options;
71 5 my $o = $+{options};
72 5 my $u = $+{uri};
73 5 ▶ if( defined $o and $o =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $o and $o =~ /\S/u) { } 3 2 5
Condition66%2/3
defined $o and $o =~ /\S/u
!ll&&!rl&&r
203
MC/DC50%1/2
defined $o and $o =~ /\S/u
defined $o $o =~ /\S/u
Truth table66%2/3
A and B
A= defined $oB= $o =~ /\S/u
ABresult
0X0
100
111
74 3 $options = decode_json($o);
75 } else {
76 2 $options = {};
77 };
78  
79 5 $options->{uri} = $u;
80 5 ▶ $options->{method} ||= 'GET';
Condition100%2/2
$options->{"method"} ||= "GET"
l!l
23
MC/DC100%1/1
$options->{"method"} ||= "GET"
$options->{"method"}
Truth table100%2/2
A
A= $options->{"method"}
Aresult
11
00
81 5 ▶ $options->{mode} ||= 'cors';
Condition100%2/2
$options->{"mode"} ||= "cors"
l!l
14
MC/DC100%1/1
$options->{"mode"} ||= "cors"
$options->{"mode"}
Truth table100%2/2
A
A= $options->{"mode"}
Aresult
11
00
82 5 ▶ $options->{cache} ||= 'default';
Condition50%1/2
$options->{"cache"} ||= "default"
l!l
05
MC/DC0%0/1
$options->{"cache"} ||= "default"
$options->{"cache"}
Truth table50%1/2
A
A= $options->{"cache"}
Aresult
11
00
83 5 ▶ $options->{credentials} ||= 'same-origin';
Condition100%2/2
$options->{"credentials"} ||= "same-origin"
l!l
14
MC/DC100%1/1
$options->{"credentials"} ||= "same-origin"
$options->{"credentials"}
Truth table100%2/2
A
A= $options->{"credentials"}
Aresult
11
00
84 5 ▶ $options->{headers} ||= {};
Condition100%2/2
$options->{"headers"} ||= {}
l!l
14
MC/DC100%1/1
$options->{"headers"} ||= {}
$options->{"headers"}
Truth table100%2/2
A
A= $options->{"headers"}
Aresult
11
00
85  
86  
87 HTTP::Request::CurlParameters->new({
88 method => delete $options->{method} || 'GET',
89 uri => $options->{uri},
90 headers => $options->{headers},
91 maybe body => $options->{body},
92 #maybe credentials => $options->{ user },
93 5 ▶ });
Condition50%1/2
delete $options->{'method'} || 'GET'
l!l
50
MC/DC0%0/1
delete $options->{'method'} || 'GET'
delete $options->{'method'}
Truth table50%1/2
A
A= delete $options->{'method'}
Aresult
11
00
94 }
95  
96 1 1;
97  
98 =head1 SEE ALSO
99  
100 L<Javascript Fetch API|https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>
101  
102 =head1 REPOSITORY
103  
104 The public repository of this module is
105 L<http://github.com/Corion/HTTP-Request-FromCurl>.
106  
107 =head1 SUPPORT
108  
109 The public support forum of this module is
110 L<https://perlmonks.org/>.
111  
112 =head1 BUG TRACKER
113  
114 Please report bugs in this module via the RT CPAN bug queue at
115 L<https://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Request-FromCurl>
116 or via mail to L<filter-signatures-Bugs@rt.cpan.org>.
117  
118 =head1 AUTHOR
119  
120 Max Maischein C<corion@cpan.org>
121  
122 =head1 COPYRIGHT (c)
123  
124 Copyright 2018 by Max Maischein C<corion@cpan.org>.
125  
126 =head1 LICENSE
127  
128 This module is released under the same terms as Perl itself.
129  
130 =cut
« lib/HTTP/Request/FromCurl.pm ↑ Summary script/pcurl.pl »