diff --git a/t/mojo/cgi.t b/t/mojo/cgi.t index 29a08e5601..fead35a509 100644 --- a/t/mojo/cgi.t +++ b/t/mojo/cgi.t @@ -47,18 +47,17 @@ subtest 'Reverse proxy' => sub { subtest 'Simple' => sub { my $msg = ''; - { - local *STDOUT; - open STDOUT, '>', \$msg; - local %ENV = ( - PATH_INFO => '/', - REQUEST_METHOD => 'GET', - SCRIPT_NAME => '/', - HTTP_HOST => 'localhost:8080', - SERVER_PROTOCOL => 'HTTP/1.0' - ); - is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); - } + local *STDOUT; + open STDOUT, '>', \$msg; + local %ENV = ( + PATH_INFO => '/', + REQUEST_METHOD => 'GET', + SCRIPT_NAME => '/', + HTTP_HOST => 'localhost:8080', + SERVER_PROTOCOL => 'HTTP/1.0' + ); + is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); + my $res = Mojo::Message::Response->new->parse("HTTP/1.1 200 OK\x0d\x0a$msg"); is $res->code, 200, 'right status'; is $res->headers->status, '200 OK', 'right "Status" value'; @@ -70,18 +69,17 @@ subtest 'Simple' => sub { subtest 'HEAD request' => sub { my $msg = ''; - { - local *STDOUT; - open STDOUT, '>', \$msg; - local %ENV = ( - PATH_INFO => '/', - REQUEST_METHOD => 'HEAD', - SCRIPT_NAME => '/', - HTTP_HOST => 'localhost:8080', - SERVER_PROTOCOL => 'HTTP/1.0' - ); - is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); - } + local *STDOUT; + open STDOUT, '>', \$msg; + local %ENV = ( + PATH_INFO => '/', + REQUEST_METHOD => 'HEAD', + SCRIPT_NAME => '/', + HTTP_HOST => 'localhost:8080', + SERVER_PROTOCOL => 'HTTP/1.0' + ); + is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); + my $res = Mojo::Message::Response->new->parse("HTTP/1.1 200 OK\x0d\x0a$msg"); is $res->code, 200, 'right status'; is $res->headers->status, '200 OK', 'right "Status" value'; @@ -93,19 +91,18 @@ subtest 'HEAD request' => sub { subtest 'Non-parsed headers' => sub { my $msg = ''; - { - local *STDOUT; - open STDOUT, '>', \$msg; - local %ENV = ( - PATH_INFO => '/', - REQUEST_METHOD => 'GET', - SCRIPT_NAME => '/', - HTTP_HOST => 'localhost:8080', - SERVER_PROTOCOL => 'HTTP/1.0' - ); - is(Mojolicious::Command::cgi->new(app => app)->run('--nph'), - 200, 'right status'); - } + local *STDOUT; + open STDOUT, '>', \$msg; + local %ENV = ( + PATH_INFO => '/', + REQUEST_METHOD => 'GET', + SCRIPT_NAME => '/', + HTTP_HOST => 'localhost:8080', + SERVER_PROTOCOL => 'HTTP/1.0' + ); + is(Mojolicious::Command::cgi->new(app => app)->run('--nph'), + 200, 'right status'); + my $res = Mojo::Message::Response->new->parse($msg); is $res->code, 200, 'right status'; is $res->headers->status, undef, 'no "Status" value'; @@ -118,22 +115,21 @@ subtest 'Non-parsed headers' => sub { subtest 'Chunked' => sub { my $content = 'test1=1&test2=2&test3=3&test4=4&test5=5&test6=6&test7=7'; my $msg = ''; - { - local *STDIN; - open STDIN, '<', \$content; - local *STDOUT; - open STDOUT, '>', \$msg; - local %ENV = ( - PATH_INFO => '/chunked', - CONTENT_LENGTH => length($content), - CONTENT_TYPE => 'application/x-www-form-urlencoded', - REQUEST_METHOD => 'POST', - SCRIPT_NAME => '/', - HTTP_HOST => 'localhost:8080', - SERVER_PROTOCOL => 'HTTP/1.0' - ); - is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); - } + local *STDIN; + open STDIN, '<', \$content; + local *STDOUT; + open STDOUT, '>', \$msg; + local %ENV = ( + PATH_INFO => '/chunked', + CONTENT_LENGTH => length($content), + CONTENT_TYPE => 'application/x-www-form-urlencoded', + REQUEST_METHOD => 'POST', + SCRIPT_NAME => '/', + HTTP_HOST => 'localhost:8080', + SERVER_PROTOCOL => 'HTTP/1.0' + ); + is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); + like $msg, qr/chunked/, 'is chunked'; my $res = Mojo::Message::Response->new->parse("HTTP/1.1 200 OK\x0d\x0a$msg"); is $res->code, 200, 'right status'; @@ -143,19 +139,18 @@ subtest 'Chunked' => sub { subtest 'Parameters' => sub { my $msg = ''; - { - local *STDOUT; - open STDOUT, '>', \$msg; - local %ENV = ( - PATH_INFO => '/params', - QUERY_STRING => 'lalala=23&bar=baz', - REQUEST_METHOD => 'GET', - SCRIPT_NAME => '/', - HTTP_HOST => 'localhost:8080', - SERVER_PROTOCOL => 'HTTP/1.0' - ); - is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); - } + local *STDOUT; + open STDOUT, '>', \$msg; + local %ENV = ( + PATH_INFO => '/params', + QUERY_STRING => 'lalala=23&bar=baz', + REQUEST_METHOD => 'GET', + SCRIPT_NAME => '/', + HTTP_HOST => 'localhost:8080', + SERVER_PROTOCOL => 'HTTP/1.0' + ); + is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); + my $res = Mojo::Message::Response->new->parse("HTTP/1.1 200 OK\x0d\x0a$msg"); is $res->code, 200, 'right status'; is $res->headers->status, '200 OK', 'right "Status" value'; @@ -168,21 +163,20 @@ subtest 'Parameters' => sub { subtest 'Reverse proxy' => sub { my $msg = ''; - { - local *STDOUT; - open STDOUT, '>', \$msg; - local %ENV = ( - PATH_INFO => '/proxy', - REQUEST_METHOD => 'GET', - SCRIPT_NAME => '/', - HTTP_HOST => 'localhost:8080', - SERVER_PROTOCOL => 'HTTP/1.0', - 'HTTP_X_Forwarded_For' => '192.0.2.2, 192.0.2.1', - 'HTTP_X_Forwarded_Proto' => 'https' - ); - local $ENV{MOJO_REVERSE_PROXY} = 1; - is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); - } + local *STDOUT; + open STDOUT, '>', \$msg; + local %ENV = ( + PATH_INFO => '/proxy', + REQUEST_METHOD => 'GET', + SCRIPT_NAME => '/', + HTTP_HOST => 'localhost:8080', + SERVER_PROTOCOL => 'HTTP/1.0', + 'HTTP_X_Forwarded_For' => '192.0.2.2, 192.0.2.1', + 'HTTP_X_Forwarded_Proto' => 'https' + ); + local $ENV{MOJO_REVERSE_PROXY} = 1; + is(Mojolicious::Command::cgi->new(app => app)->run, 200, 'right status'); + my $res = Mojo::Message::Response->new->parse("HTTP/1.1 200 OK\x0d\x0a$msg"); is $res->code, 200, 'right status'; is $res->headers->status, '200 OK', 'right "Status" value';