| 1 | package DummyIO; |
|---|
| 2 | use overload qw{""} => sub { 'bless' }; |
|---|
| 3 | sub new { bless {}, shift } |
|---|
| 4 | |
|---|
| 5 | package main; |
|---|
| 6 | use Test::Base; |
|---|
| 7 | use IO::Scalar; |
|---|
| 8 | use HTTP::Engine::ResponseWriter; |
|---|
| 9 | use HTTP::Engine::Response; |
|---|
| 10 | use HTTP::Engine::Request; |
|---|
| 11 | use HTTP::Response; |
|---|
| 12 | use File::Temp qw/:seekable/; |
|---|
| 13 | |
|---|
| 14 | plan skip_all => 'File::Temp 0.20 required for this test' unless $File::Temp::VERSION >= 0.20; |
|---|
| 15 | |
|---|
| 16 | plan tests => 1*blocks; |
|---|
| 17 | |
|---|
| 18 | filters { |
|---|
| 19 | input => [qw/eval/], |
|---|
| 20 | expected => [qw/chomp crlf/], |
|---|
| 21 | }; |
|---|
| 22 | |
|---|
| 23 | run_is input => 'expected'; |
|---|
| 24 | |
|---|
| 25 | sub crlf { |
|---|
| 26 | my $x = shift; |
|---|
| 27 | $x =~ s/\n/\r\n/g; |
|---|
| 28 | $x; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | __END__ |
|---|
| 32 | |
|---|
| 33 | === |
|---|
| 34 | --- input |
|---|
| 35 | my $writer = HTTP::Engine::ResponseWriter->new( |
|---|
| 36 | should_write_response_line => 1, |
|---|
| 37 | ); |
|---|
| 38 | |
|---|
| 39 | my $tmp = File::Temp->new(); |
|---|
| 40 | $tmp->write("OK!"); |
|---|
| 41 | $tmp->flush(); |
|---|
| 42 | $tmp->seek(0, File::Temp::SEEK_SET); |
|---|
| 43 | |
|---|
| 44 | tie *STDOUT, 'IO::Scalar', \my $out; |
|---|
| 45 | |
|---|
| 46 | my $req = HTTP::Engine::Request->new( |
|---|
| 47 | protocol => 'HTTP/1.1', |
|---|
| 48 | method => 'GET', |
|---|
| 49 | ); |
|---|
| 50 | my $res = HTTP::Engine::Response->new(body => $tmp, status => 200); |
|---|
| 51 | HTTP::Engine::ResponseFinalizer->finalize( $req, $res ); |
|---|
| 52 | $writer->finalize($req, $res); |
|---|
| 53 | |
|---|
| 54 | untie *STDOUT; |
|---|
| 55 | |
|---|
| 56 | $out; |
|---|
| 57 | --- expected |
|---|
| 58 | HTTP/1.1 200 OK |
|---|
| 59 | Connection: close |
|---|
| 60 | Content-Length: 3 |
|---|
| 61 | Content-Type: text/html |
|---|
| 62 | Status: 200 |
|---|
| 63 | |
|---|
| 64 | OK! |
|---|
| 65 | |
|---|
| 66 | === |
|---|
| 67 | --- input |
|---|
| 68 | my $writer = HTTP::Engine::ResponseWriter->new( |
|---|
| 69 | should_write_response_line => 1, |
|---|
| 70 | ); |
|---|
| 71 | |
|---|
| 72 | my $tmp = DummyIO->new; |
|---|
| 73 | |
|---|
| 74 | tie *STDOUT, 'IO::Scalar', \my $out; |
|---|
| 75 | |
|---|
| 76 | my $req = HTTP::Engine::Request->new( |
|---|
| 77 | protocol => 'HTTP/1.1', |
|---|
| 78 | method => 'GET', |
|---|
| 79 | ); |
|---|
| 80 | my $res = HTTP::Engine::Response->new(body => $tmp, status => 200); |
|---|
| 81 | HTTP::Engine::ResponseFinalizer->finalize( $req, $res ); |
|---|
| 82 | $writer->finalize($req, $res); |
|---|
| 83 | |
|---|
| 84 | untie *STDOUT; |
|---|
| 85 | |
|---|
| 86 | $out; |
|---|
| 87 | --- expected |
|---|
| 88 | HTTP/1.1 200 OK |
|---|
| 89 | Connection: close |
|---|
| 90 | Content-Length: 5 |
|---|
| 91 | Content-Type: text/html |
|---|
| 92 | Status: 200 |
|---|
| 93 | |
|---|
| 94 | bless |
|---|
| 95 | |
|---|
| 96 | === |
|---|
| 97 | --- input |
|---|
| 98 | my $writer = HTTP::Engine::ResponseWriter->new( |
|---|
| 99 | should_write_response_line => 1, |
|---|
| 100 | ); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | my $ftmp = File::Temp->new(); |
|---|
| 104 | $ftmp->write('dummy'x5000); |
|---|
| 105 | $ftmp->flush(); |
|---|
| 106 | $ftmp->seek(0, File::Temp::SEEK_SET); |
|---|
| 107 | |
|---|
| 108 | open my $tmp, '<', $ftmp->filename or die $!; |
|---|
| 109 | tie *STDOUT, 'IO::Scalar', \my $out; |
|---|
| 110 | |
|---|
| 111 | my $req = HTTP::Engine::Request->new( |
|---|
| 112 | protocol => 'HTTP/1.1', |
|---|
| 113 | method => 'GET', |
|---|
| 114 | ); |
|---|
| 115 | my $res = HTTP::Engine::Response->new(body => $tmp, status => 200); |
|---|
| 116 | HTTP::Engine::ResponseFinalizer->finalize( $req, $res ); |
|---|
| 117 | $writer->finalize($req, $res); |
|---|
| 118 | |
|---|
| 119 | untie *STDOUT; |
|---|
| 120 | |
|---|
| 121 | $out; |
|---|
| 122 | |
|---|
| 123 | --- expected eval |
|---|
| 124 | "HTTP/1.1 200 OK |
|---|
| 125 | Connection: close |
|---|
| 126 | Content-Length: 25000 |
|---|
| 127 | Content-Type: text/html |
|---|
| 128 | Status: 200 |
|---|
| 129 | |
|---|
| 130 | ".('dummy'x5000) |
|---|
| 131 | === |
|---|
| 132 | --- input |
|---|
| 133 | my $writer = HTTP::Engine::ResponseWriter->new( |
|---|
| 134 | should_write_response_line => 1, |
|---|
| 135 | ); |
|---|
| 136 | |
|---|
| 137 | tie *STDOUT, 'IO::Scalar', \my $out; |
|---|
| 138 | |
|---|
| 139 | my $req = HTTP::Engine::Request->new( |
|---|
| 140 | protocol => 'HTTP/1.1', |
|---|
| 141 | method => 'GET', |
|---|
| 142 | ); |
|---|
| 143 | my $res = HTTP::Engine::Response->new(body => 'OK!', status => 200); |
|---|
| 144 | $res->header( Connection => 'keepalive' ); |
|---|
| 145 | HTTP::Engine::ResponseFinalizer->finalize( $req, $res ); |
|---|
| 146 | $writer->finalize($req, $res); |
|---|
| 147 | |
|---|
| 148 | untie *STDOUT; |
|---|
| 149 | |
|---|
| 150 | $out; |
|---|
| 151 | --- expected |
|---|
| 152 | HTTP/1.1 200 OK |
|---|
| 153 | Connection: keepalive |
|---|
| 154 | Content-Length: 3 |
|---|
| 155 | Content-Type: text/html |
|---|
| 156 | Status: 200 |
|---|
| 157 | |
|---|
| 158 | OK! |
|---|