| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
plan tests => 47; |
| 6 |
|
| 7 |
use HTTP::Message; |
| 8 |
use HTTP::Request::Common qw(POST); |
| 9 |
|
| 10 |
my $m = HTTP::Message->new; |
| 11 |
|
| 12 |
is(ref($m->headers), "HTTP::Headers"); |
| 13 |
is($m->headers_as_string, ""); |
| 14 |
is($m->content, ""); |
| 15 |
is(j($m->parts), ""); |
| 16 |
is($m->as_string, "\n"); |
| 17 |
|
| 18 |
my $m_clone = $m->clone; |
| 19 |
$m->push_header("Foo", 1); |
| 20 |
$m->add_content("foo"); |
| 21 |
|
| 22 |
is($m_clone->as_string, "\n"); |
| 23 |
is($m->headers_as_string, "Foo: 1\n"); |
| 24 |
is($m->header("Foo"), 1); |
| 25 |
is($m->as_string, "Foo: 1\n\nfoo\n"); |
| 26 |
is($m->as_string("\r\n"), "Foo: 1\r\n\r\nfoo"); |
| 27 |
is(j($m->parts), ""); |
| 28 |
|
| 29 |
$m->content_type("message/foo"); |
| 30 |
$m->content(<<EOT); |
| 31 |
H1: 1 |
| 32 |
H2: 2 |
| 33 |
3 |
| 34 |
H3: abc |
| 35 |
|
| 36 |
FooBar |
| 37 |
EOT |
| 38 |
|
| 39 |
my @parts = $m->parts; |
| 40 |
is(@parts, 1); |
| 41 |
my $m2 = $parts[0]; |
| 42 |
is(ref($m2), "HTTP::Message"); |
| 43 |
|
| 44 |
is($m2->header("h1"), 1); |
| 45 |
is($m2->header("h2"), "2\n 3"); |
| 46 |
is($m2->header("h3"), " abc"); |
| 47 |
is($m2->content, "FooBar\n"); |
| 48 |
is($m2->as_string, $m->content); |
| 49 |
is(j($m2->parts), ""); |
| 50 |
|
| 51 |
$m = POST("http://www.example.com", |
| 52 |
Content_Type => 'form-data', |
| 53 |
Content => [ foo => 1, bar => 2 ]); |
| 54 |
is($m->content_type, "multipart/form-data"); |
| 55 |
@parts = $m->parts; |
| 56 |
is(@parts, 2); |
| 57 |
is($parts[0]->header("Content-Disposition"), 'form-data; name="foo"'); |
| 58 |
is($parts[0]->content, 1); |
| 59 |
is($parts[1]->header("Content-Disposition"), 'form-data; name="bar"'); |
| 60 |
is($parts[1]->content, 2); |
| 61 |
|
| 62 |
$m = HTTP::Message->new; |
| 63 |
$m->content_type("message/http"); |
| 64 |
$m->content(<<EOT); |
| 65 |
GET / HTTP/1.0 |
| 66 |
Host: example.com |
| 67 |
|
| 68 |
How is this? |
| 69 |
EOT |
| 70 |
|
| 71 |
@parts = $m->parts; |
| 72 |
is(@parts, 1); |
| 73 |
is($parts[0]->method, "GET"); |
| 74 |
is($parts[0]->uri, "/"); |
| 75 |
is($parts[0]->protocol, "HTTP/1.0"); |
| 76 |
is($parts[0]->header("Host"), "example.com"); |
| 77 |
is($parts[0]->content, "How is this?\n"); |
| 78 |
|
| 79 |
$m = HTTP::Message->new; |
| 80 |
$m->content_type("message/http"); |
| 81 |
$m->content(<<EOT); |
| 82 |
HTTP/1.1 200 is |
| 83 |
Content-Type : text/html |
| 84 |
|
| 85 |
<H1>Hello world!</H1> |
| 86 |
EOT |
| 87 |
|
| 88 |
@parts = $m->parts; |
| 89 |
is(@parts, 1); |
| 90 |
is($parts[0]->code, 200); |
| 91 |
is($parts[0]->message, "is"); |
| 92 |
is($parts[0]->protocol, "HTTP/1.1"); |
| 93 |
is($parts[0]->content_type, "text/html"); |
| 94 |
is($parts[0]->content, "<H1>Hello world!</H1>\n"); |
| 95 |
|
| 96 |
$m->parts(HTTP::Request->new("GET", "http://www.example.com")); |
| 97 |
is($m->as_string, "Content-Type: message/http\n\nGET http://www.example.com\r\n\r\n"); |
| 98 |
|
| 99 |
$m = HTTP::Request->new("PUT", "http://www.example.com"); |
| 100 |
$m->parts(HTTP::Message->new([Foo => 1], "abc\n"), HTTP::Message->new([Bar => 2], "def")); |
| 101 |
is($m->as_string, <<EOT); |
| 102 |
PUT http://www.example.com |
| 103 |
Content-Type: multipart/mixed; boundary=xYzZY |
| 104 |
|
| 105 |
--xYzZY\r |
| 106 |
Foo: 1\r |
| 107 |
\r |
| 108 |
abc |
| 109 |
\r |
| 110 |
--xYzZY\r |
| 111 |
Bar: 2\r |
| 112 |
\r |
| 113 |
def\r |
| 114 |
--xYzZY--\r |
| 115 |
EOT |
| 116 |
|
| 117 |
$m->content(<<EOT); |
| 118 |
--xYzZY |
| 119 |
Content-Length: 4 |
| 120 |
|
| 121 |
abcd |
| 122 |
--xYzZY-- |
| 123 |
EOT |
| 124 |
|
| 125 |
@parts = $m->parts; |
| 126 |
is(@parts, 1); |
| 127 |
is($parts[0]->content_length, 4); |
| 128 |
is($parts[0]->content, "abcd"); |
| 129 |
|
| 130 |
$m->content(" |
| 131 |
|
| 132 |
--xYzZY |
| 133 |
Content-Length: 4 |
| 134 |
|
| 135 |
efgh |
| 136 |
--xYzZY |
| 137 |
Content-Length: 3 |
| 138 |
|
| 139 |
ijk |
| 140 |
--xYzZY--"); |
| 141 |
|
| 142 |
@parts = $m->parts; |
| 143 |
is(@parts, 2); |
| 144 |
is($parts[0]->content_length, 4); |
| 145 |
is($parts[0]->content, "efgh"); |
| 146 |
is($parts[1]->content_length, 3); |
| 147 |
is($parts[1]->content, "ijk"); |
| 148 |
|
| 149 |
sub j { join(":", @_) } |