File: 030_response.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (73 lines) | stat: -rw-r--r-- 2,705 bytes parent folder | download | duplicates (5)
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
#!perl

use strict;
use warnings;

use Test::More qw[no_plan];
use lib 't';
use Util    qw[tmpfile rewind $CRLF $LF];
use HTTP::Tiny;

sub _header {
  return [ @{$_[0]}{qw/status reason headers protocol/} ]
}

{
    no warnings 'redefine';
    sub HTTP::Tiny::Handle::can_read  { 1 };
    sub HTTP::Tiny::Handle::can_write { 1 };
}

{
    my $response = join $CRLF, 'HTTP/1.1 200 OK', 'Foo: Foo', 'Bar: Bar', '', '';
    my $fh       = tmpfile($response);
    my $handle   = HTTP::Tiny::Handle->new(fh => $fh);
    my $exp      = [ 200, 'OK', { foo => 'Foo', bar => 'Bar' }, 'HTTP/1.1' ];
    is_deeply(_header($handle->read_response_header), $exp, "->read_response_header CRLF");
}

{
    my $response = join $LF, 'HTTP/1.1 200 OK', 'Foo: Foo', 'Bar: Bar', '', '';
    my $fh       = tmpfile($response);
    my $handle   = HTTP::Tiny::Handle->new(fh => $fh);
    my $exp      = [ 200, 'OK', { foo => 'Foo', bar => 'Bar' }, 'HTTP/1.1' ];
    is_deeply(_header($handle->read_response_header), $exp, "->read_response_header LF");
}

{
    # broken status-line
    my $response = join $LF, "HTTP/08.15 66x   Foo\nbar", 'Foo: Foo', 'Bar: Bar', '', '';
    my $fh       = tmpfile($response);
    my $handle   = HTTP::Tiny::Handle->new(fh => $fh);
    my $res      = eval{ $handle->read_response_header };
    my $err      = $@;
    like $err, qr/Malformed Status-Line: /, '->read_response_header diagnoses malformed status line';
}

{
    my $response = join $LF, "HTTP/2.0 200 Okish", 'Foo: Foo', 'Bar: Bar', '', '';
    my $fh       = tmpfile($response);
    my $handle   = HTTP::Tiny::Handle->new(fh => $fh);
    my $res      = eval{ $handle->read_response_header };
    my $err      = $@;
    like $err, qr/Unsupported HTTP protocol: /, '->read_response_header unsupported HTTP protocol';
}

{
    # strict RFC7230#3.1.2 compliance, require space after code
    my $response = join $LF, 'HTTP/1.1 200 ', 'Foo: Foo', 'Bar: Bar', '', '';
    my $fh       = tmpfile($response);
    my $handle   = HTTP::Tiny::Handle->new(fh => $fh);
    my $exp      = [ 200, '', { foo => 'Foo', bar => 'Bar' }, 'HTTP/1.1' ];
    is_deeply(_header($handle->read_response_header), $exp, "->read_response_header empty phrase preceded by SP");
}

{
    # practical RFC7230#3.1.2 interpretation, require space after code
    # only if there is a reason-phrase
    my $response = join $LF, 'HTTP/1.1 200', 'Foo: Foo', 'Bar: Bar', '', '';
    my $fh       = tmpfile($response);
    my $handle   = HTTP::Tiny::Handle->new(fh => $fh);
    my $exp      = [ 200, '', { foo => 'Foo', bar => 'Bar' }, 'HTTP/1.1' ];
    is_deeply(_header($handle->read_response_header), $exp, "->read_response_header empty phrase without preceding SP");
}