-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplackonce
executable file
·60 lines (47 loc) · 1.47 KB
/
plackonce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
# plackonce [options] someapp.psgi GET /some/url
#
# Possibly should be reworked to use https://metacpan.org/pod/Web::Simple::Application
#
# --type=S - set Content-Type header to S
# --accept=S - set Accept header to S
# --header H=S - set header H to S
#
# The --type and --accept options can use abbreviations for common types:
# json = application/json
# vaj = application/vnd.api+json
# hal = application/hal+json
use strict;
use warnings;
use HTTP::Request;
use HTTP::Response;
use HTTP::Message::PSGI;
use Getopt::Long;
my $typemap = {
json => 'application/json',
vaj => 'application/vnd.api+json',
hal => 'application/hal+json',
};
my %headers;
sub set_header {
my ($header, $value, $map) = @_;
$value = $map->{$value} if $map && exists $map->{$value};
$headers{$header} = $value;
}
GetOptions(
'type|t=s' => sub { set_header('Accept', $_[1], $typemap) },
'accept|a=s' => sub { set_header('Content-Type', $_[1], $typemap) },
'header|h=s%' => sub { set_header($_[0], $_[1], undef) },
) or exit 1;
sub usage {
die "usage: $0 foo.psgi GET '/some/url?param=bar'\n";
}
my $psgi = shift or usage();
my $method = shift or usage();
my $url = shift or usage();
my $app = require $psgi;
my $request = HTTP::Request->new($method, $url);
$request->header(%headers);
$request->dump(maxlength => 2000);
my $res = HTTP::Response->from_psgi( $app->($request->to_psgi) );
$res->dump(maxlength => 5000);