Debian Codegraph @

t/04response-format.t

raw symbols SCIP
1 use strict;
2 use warnings;
3 use Test::More;
4 use Data::Dumper;
5 use HTTP::Parser::XS qw/:all/;
6
7 my %formats = (
8 HEADERS_NONE() => "NONE",
9 HEADERS_AS_HASHREF() => "HASH",
10 HEADERS_AS_ARRAYREF() => "ARRAY",
11 );
12
13 my $tests = <<'__HEADERS';
14 HTTP/1.0 200 OK
15
16 ----------
17 { ret => [17,"0","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {}, HEADER_AS_ARRAY => [] }
18 ----------
19 HTTP/1.0 200 OK
20 Content-Type: text/html
21
22 hogehoge
23 ----------
24 { ret => [41,"0","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html"}, HEADER_AS_ARRAY => ["content-type","text/html"] }
25 ----------
26 HTTP/1.0 200 OK
27 Content-Type: text/html
28 X-Test: 1
29 X-Test: 2
30
31 hogehoge
32 ----------
33 { ret => [61,"0","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html","x-test" => ["1","2"]}, HEADER_AS_ARRAY => ["content-type","text/html","x-test","1","x-test","2"] }
34 ----------
35 HTTP/1.0 200 OK
36 Content-Type: text/html
37 X-Test: 1
38 X-Test: 2
39
40 hogehoge
41 ----------
42 { ret => [62,"0","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html","x-test" => "1\n X-Test: 2"}, HEADER_AS_ARRAY => ["content-type","text/html","x-test","1\n X-Test: 2"] }
43 ----------
44 HTTP/1.0 200 OK
45 Content-Type: text/html
46
47 ----------
48 { ret => [41,"0","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html"}, HEADER_AS_ARRAY => ["content-type","text/html"] }
49 ----------
50 HTTP/1.1 200 OK
51 Content-Type: text/html
52
53 ----------
54 { ret => [41,"1","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html"}, HEADER_AS_ARRAY => ["content-type","text/html"] }
55 ----------
56 HTTP/1.1 404 Not Found
57 Content-Type: text/html
58
59 ----------
60 { ret => [48,"1","404","Not Found"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html"}, HEADER_AS_ARRAY => ["content-type","text/html"] }
61 ----------
62 HTTP/1.1 200 OK
63 Content-Type: text/html
64 FOO-BAR: BAZ
65
66 ----------
67 { ret => [54,"1","200","OK"], HEADER_AS_NONE => undef, HEADER_AS_HASH => {"content-type" => "text/html","foo-bar" => "BAZ"}, HEADER_AS_ARRAY => ["content-type","text/html","foo-bar","BAZ"] }
68 __HEADERS
69
70 my @tests = split '-'x10, $tests;
71 my $i = 0;
72
73 while (@tests) {
74 $i++;
75 my $header = shift @tests;
76 my $expect = shift @tests;
77 $header =~ s/^\n//;
78 last unless $expect;
79
80 my $r = eval($expect);
81
82 for my $format (0..2) {
83 my @a = parse_http_response($header, $format);
84 my $headers = pop @a;
85
86 is_deeply( \@a, $r->{ret}, 'test-' . $i) or diag(explain(\@a));
87 is_deeply(
88 $headers,
89 $r->{ "HEADER_AS_". $formats{$format} },
90 'test-format-'. $formats{$format} . "-" .$i
91 ) or diag(explain($headers))
92 }
93
94 }
95
96 done_testing;
97