-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathrequest.t
More file actions
178 lines (142 loc) · 4.45 KB
/
request.t
File metadata and controls
178 lines (142 loc) · 4.45 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Test extra HTTP::Request methods. Basic operation is tested in the
# message.t test suite.
use strict;
use warnings;
use Test::More;
plan tests => 43;
use HTTP::Request;
use Try::Tiny qw( catch try );
my $req = HTTP::Request->new( GET => "http://www.example.com" );
$req->accept_decodable;
is( $req->method, "GET" );
is( $req->uri, "http://www.example.com" );
like( $req->header("Accept-Encoding"), qr/\bgzip\b/ )
; # assuming Compress::Raw::Zlib is there
$req->dump( prefix => "# " );
is( $req->method("DELETE"), "GET" );
is( $req->method, "DELETE" );
is( $req->uri("http:"), "http://www.example.com" );
is( $req->uri, "http:" );
$req->protocol("HTTP/1.1");
my $r2 = HTTP::Request->parse( $req->as_string );
is( $r2->method, "DELETE" );
is( $r2->uri, "http:" );
is( $r2->protocol, "HTTP/1.1" );
is( $r2->header("Accept-Encoding"), $req->header("Accept-Encoding") );
# Test objects which are accepted as URI-like
{
package Foo::URI;
use strict;
use warnings;
sub new { return bless {}, shift; }
sub clone { return shift }
sub scheme { }
1;
package Foo::URI::WithCanonical;
sub new { return bless {}, shift; }
sub clone { return shift }
sub scheme { }
sub canonical { }
1;
package Foo::URI::WithoutScheme;
sub new { return bless {}, shift; }
sub clone { return shift }
1;
package main;
ok( Foo::URI->new->can('scheme'), 'Object can scheme()' );
ok(
!do {
try {
HTTP::Request->new( GET => Foo::URI->new );
return 1;
}
catch { return 0 };
},
'Object without canonical method triggers an exception'
);
ok(
Foo::URI::WithCanonical->new->can('canonical'),
'Object can canonical()'
);
ok(
do {
try {
HTTP::Request->new( GET => Foo::URI::WithCanonical->new );
return 1;
}
catch { return 0 };
},
'Object with canonical method does not trigger an exception'
);
ok( !Foo::URI::WithoutScheme->new->can('scheme'), 'Object cannot scheme()' );
ok(
!do {
try {
HTTP::Request->new( GET => Foo::URI::WithoutScheme->new );
return 1;
}
catch { return 0 };
},
'Object without scheme method triggers an exception'
);
# test uri_canonical cache
{
my $url = 'https://localhost/';
my $r = HTTP::Request->new(GET => $url);
is($r->uri_canonical, $url, 'Works when canonical uri not yet cached');
is($r->uri_canonical, $url, 'Works when canonical uri has been cached');
}
{
require URI::URL;
my $url = 'https://localhost/';
my $r = HTTP::Request->new(GET => URI::URL->new($url));
is($r->uri_canonical, $url, 'Works when canonical uri not yet cached with URI::URL');
is($r->uri_canonical, $url, 'Works when canonical uri has been cached with URI::URL');
}
}
eval { $req->uri ({ foo => 'bar'}); };
like($@, qr/A URI can't be a HASH reference/);
eval { $req->uri (['foo']); };
like($@, qr/A URI can't be a ARRAY reference/);
$req = HTTP::Request->new;
is($req->as_string, "- -\n\n");
is($req->dump, <<EOT);
- -
(no content)
EOT
$req->protocol("HTTP/1.1");
is($req->dump, <<EOT);
- - HTTP/1.1
(no content)
EOT
{
my @warn;
local $SIG{__WARN__} = sub { push @warn, @_ };
local $^W = 0;
$r2 = HTTP::Request->parse( undef );
is($#warn, -1);
local $^W = 1;
$r2 = HTTP::Request->parse( undef );
is($#warn, 0);
like($warn[0], qr/Undefined argument to parse\(\)/);
}
is( $r2->method, undef );
is( $r2->uri, undef );
is( $r2->protocol, undef );
is( $r2->header("Accept-Encoding"), $req->header("Accept-Encoding") );
$r2 = HTTP::Request->parse('methonly');
is( $r2->method, 'methonly' );
is( $r2->uri, undef );
is( $r2->protocol, undef );
$r2 = HTTP::Request->parse('methonly http://www.example.com/');
is( $r2->method, 'methonly' );
is( $r2->uri, 'http://www.example.com/' );
is( $r2->protocol, undef );
my $r3 = HTTP::Request->new(0 => "0");
is($r3->method, '0', '0 is a valid HTTP method');
is($r3->uri, '0', '0 is a valid URI');
is($r3->as_string, "0 0\n\n", 'parsed zero method, zero uri req');
is($r3->dump, <<EOT, 'dumped zero method, zero uri req');
0 0
(no content)
EOT