forked from plack/Plack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_body_parser.t
86 lines (71 loc) · 2.15 KB
/
request_body_parser.t
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use strict;
use warnings;
use utf8;
use Test::More;
use Plack::Request;
use Plack::BodyParser::UrlEncoded;
use Plack::BodyParser::JSON;
use Plack::BodyParser::MultiPart;
use Plack::BodyParser::OctetStream;
subtest 'JSON' => sub {
is_deeply make_request('application/json', '{"hoge":"fuga"}')->parameters()->as_hashref_multi, {
hoge => ['fuga'],
};
};
subtest 'UrlEncoded' => sub {
is_deeply make_request('application/x-www-form-urlencoded', 'xxx=yyy')->parameters()->as_hashref_multi, {
xxx => ['yyy'],
};
};
subtest 'MultiPart' => sub {
my $content = <<'...';
--BOUNDARY
Content-Disposition: form-data; name="xxx"
Content-Type: text/plain
yyy
--BOUNDARY
Content-Disposition: form-data; name="yappo"; filename="osawa.txt"
Content-Type: text/plain
SHOGUN
--BOUNDARY--
...
$content =~ s/\r\n/\n/g;
$content =~ s/\n/\r\n/g;
my $req = make_request('multipart/form-data; boundary=BOUNDARY', $content);
is_deeply $req->parameters()->as_hashref_multi, {
xxx => ['yyy'],
};
is slurp($req->upload('yappo')), 'SHOGUN';
is $req->upload('yappo')->filename, 'osawa.txt';
isa_ok $req->upload('yappo')->headers, 'HTTP::Headers';
};
subtest 'OctetStream' => sub {
my $content = 'hogehoge';
my $req = make_request('application/octet-stream', $content);
is $req->content, 'hogehoge';
is 0+($req->parameters->keys), 0;
is 0+($req->uploads->keys), 0;
};
done_testing;
sub make_request {
my ($content_type, $content) = @_;
my $parser = Plack::BodyParser->new();
$parser->register('application/json', 'Plack::BodyParser::JSON');
$parser->register('application/x-www-form-urlencoded', 'Plack::BodyParser::UrlEncoded');
$parser->register('multipart/form-data', 'Plack::BodyParser::MultiPart');
open my $input, '<', \$content;
my $req = Plack::Request->new(
+{
'psgi.input' => $input,
CONTENT_TYPE => $content_type,
CONTENT_LENGTH => length($content),
},
request_body_parser => $parser,
);
return $req;
}
sub slurp {
my $up = shift;
open my $fh, "<", $up->path or die "$!";
scalar do { local $/; <$fh> };
}