Debian Codegraph @

t/request.t

raw
1 # Test extra HTTP::Request methods. Basic operation is tested in the
2 # message.t test suite.
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 plan tests => 43;
9
10 use HTTP::Request;
11 use Try::Tiny qw( catch try );
12
13 my $req = HTTP::Request->new( GET => "http://www.example.com" );
14 $req->accept_decodable;
15
16 is( $req->method, "GET" );
17 is( $req->uri, "http://www.example.com" );
18 like( $req->header("Accept-Encoding"), qr/\bgzip\b/ )
19 ; # assuming Compress::Raw::Zlib is there
20
21 $req->dump( prefix => "# " );
22
23 is( $req->method("DELETE"), "GET" );
24 is( $req->method, "DELETE" );
25
26 is( $req->uri("http:"), "http://www.example.com" );
27 is( $req->uri, "http:" );
28
29 $req->protocol("HTTP/1.1");
30
31 my $r2 = HTTP::Request->parse( $req->as_string );
32 is( $r2->method, "DELETE" );
33 is( $r2->uri, "http:" );
34 is( $r2->protocol, "HTTP/1.1" );
35 is( $r2->header("Accept-Encoding"), $req->header("Accept-Encoding") );
36
37 # Test objects which are accepted as URI-like
38 {
39 package Foo::URI;
40
41 use strict;
42 use warnings;
43
44 sub new { return bless {}, shift; }
45 sub clone { return shift }
46 sub scheme { }
47
48 1;
49
50 package Foo::URI::WithCanonical;
51
52 sub new { return bless {}, shift; }
53 sub clone { return shift }
54 sub scheme { }
55 sub canonical { }
56
57 1;
58
59 package Foo::URI::WithoutScheme;
60
61 sub new { return bless {}, shift; }
62 sub clone { return shift }
63
64 1;
65
66 package main;
67
68 ok( Foo::URI->new->can('scheme'), 'Object can scheme()' );
69 ok(
70 !do {
71 try {
72 HTTP::Request->new( GET => Foo::URI->new );
73 return 1;
74 }
75 catch { return 0 };
76 },
77 'Object without canonical method triggers an exception'
78 );
79
80 ok(
81 Foo::URI::WithCanonical->new->can('canonical'),
82 'Object can canonical()'
83 );
84
85 ok(
86 do {
87 try {
88 HTTP::Request->new( GET => Foo::URI::WithCanonical->new );
89 return 1;
90 }
91 catch { return 0 };
92 },
93 'Object with canonical method does not trigger an exception'
94 );
95
96 ok( !Foo::URI::WithoutScheme->new->can('scheme'), 'Object cannot scheme()' );
97 ok(
98 !do {
99 try {
100 HTTP::Request->new( GET => Foo::URI::WithoutScheme->new );
101 return 1;
102 }
103 catch { return 0 };
104 },
105 'Object without scheme method triggers an exception'
106 );
107
108 # test uri_canonical cache
109 {
110 my $url = 'https://localhost/';
111 my $r = HTTP::Request->new(GET => $url);
112 is($r->uri_canonical, $url, 'Works when canonical uri not yet cached');
113 is($r->uri_canonical, $url, 'Works when canonical uri has been cached');
114 }
115
116 {
117 require URI::URL;
118 my $url = 'https://localhost/';
119 my $r = HTTP::Request->new(GET => URI::URL->new($url));
120 is($r->uri_canonical, $url, 'Works when canonical uri not yet cached with URI::URL');
121 is($r->uri_canonical, $url, 'Works when canonical uri has been cached with URI::URL');
122 }
123 }
124
125 eval { $req->uri ({ foo => 'bar'}); };
126 like($@, qr/A URI can't be a HASH reference/);
127 eval { $req->uri (['foo']); };
128 like($@, qr/A URI can't be a ARRAY reference/);
129
130 $req = HTTP::Request->new;
131 is($req->as_string, "- -\n\n");
132 is($req->dump, <<EOT);
133 - -
134
135 (no content)
136 EOT
137 $req->protocol("HTTP/1.1");
138 is($req->dump, <<EOT);
139 - - HTTP/1.1
140
141 (no content)
142 EOT
143
144 {
145 my @warn;
146 local $SIG{__WARN__} = sub { push @warn, @_ };
147 local $^W = 0;
148 $r2 = HTTP::Request->parse( undef );
149 is($#warn, -1);
150 local $^W = 1;
151 $r2 = HTTP::Request->parse( undef );
152 is($#warn, 0);
153 like($warn[0], qr/Undefined argument to parse\(\)/);
154 }
155 is( $r2->method, undef );
156 is( $r2->uri, undef );
157 is( $r2->protocol, undef );
158 is( $r2->header("Accept-Encoding"), $req->header("Accept-Encoding") );
159
160 $r2 = HTTP::Request->parse('methonly');
161 is( $r2->method, 'methonly' );
162 is( $r2->uri, undef );
163 is( $r2->protocol, undef );
164
165 $r2 = HTTP::Request->parse('methonly http://www.example.com/');
166 is( $r2->method, 'methonly' );
167 is( $r2->uri, 'http://www.example.com/' );
168 is( $r2->protocol, undef );
169
170 my $r3 = HTTP::Request->new(0 => "0");
171 is($r3->method, '0', '0 is a valid HTTP method');
172 is($r3->uri, '0', '0 is a valid URI');
173 is($r3->as_string, "0 0\n\n", 'parsed zero method, zero uri req');
174 is($r3->dump, <<EOT, 'dumped zero method, zero uri req');
175 0 0
176
177 (no content)
178 EOT