#!perl -w use strict; use Test::More; use Test::Requires qw/ HTML::Lint::Pluggable /; use Plack::Builder; use Plack::Test; use HTTP::Request; my $valid_html = q{ hoge

fuga

}; my $error_html = q{ hoge

fuga

}; my $broken_error_html = q{ hoge

fuga

}; my $error_html_res = q{ hoge

HTML Error

elem-unknown
(5:52) Unknown element <hoge>

fuga

}; my $error_html_res_streaming = q{ hoge

fuga

HTML Error

elem-unknown
(5:52) Unknown element <hoge>
}; my $broken_error_html_res = q{ hoge

fuga

HTML Error

elem-unknown
(5:31) Unknown element <hoge>
doc-tag-required
(6:1) <body> tag is required
}; chomp $broken_error_html_res; my $normal_handler = builder { enable 'HTMLLint::Pluggable', plugins => +{ html5 => [qw/HTML5/] }; mount '/text' => sub { return [200, ['Content-Type' => 'text/plain'], [ 'OK' ]]; }; mount '/valid_html' => sub { return [200, ['Content-Type' => 'text/html'], [$valid_html]]; }; mount '/error_html' => sub { return [200, ['Content-Type' => 'text/html'], [$error_html]]; }; mount '/broken_error_html' => sub { return [200, ['Content-Type' => 'text/html'], [$broken_error_html]]; }; }; my $streaming_handler = builder { enable 'HTMLLint::Pluggable', plugins => +{ html5 => [qw/HTML5/] }; mount '/text' => sub { return sub { my $responder = shift; my $writer = $responder->([200, ['Content-Type' => 'text/plain']]); $writer->write("OK"); $writer->close; }; }; mount '/valid_html' => sub { return sub { my $responder = shift; my $writer = $responder->([200, ['Content-Type' => 'text/html']]); foreach my $line (split /\n/, $valid_html) { $writer->write($line); $writer->write("\n"); } $writer->close; }; }; mount '/error_html' => sub { return sub { my $responder = shift; my $writer = $responder->([200, ['Content-Type' => 'text/html']]); foreach my $line (split /\n/, $error_html) { $writer->write($line); $writer->write("\n"); } $writer->close; }; }; mount '/broken_error_html' => sub { return sub { my $responder = shift; my $writer = $responder->([200, ['Content-Type' => 'text/html']]); foreach my $line (split /\n/, $broken_error_html) { $writer->write($line); $writer->write("\n"); } $writer->close; }; }; }; subtest 'normal' => sub { test_psgi( client => sub { my $cb = shift; my $res; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/text') ); is $res->content, 'OK', 'plain text is not modified.'; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/valid_html') ); is $res->content, $valid_html, 'valid html is not modified.'; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/error_html') ); isnt $res->content, $error_html, 'error html is modified.'; is $res->content, $error_html_res, 'error html has error message.'; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/broken_error_html') ); isnt $res->content, $broken_error_html, 'broken error html is modified.'; is $res->content, $broken_error_html_res, 'broken error html has error message.'; }, app => $normal_handler, ); }; subtest 'streaming' => sub { test_psgi( client => sub { my $cb = shift; my $res; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/text') ); is $res->content, 'OK', 'plain text is not modified.'; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/valid_html') ); is $res->content, $valid_html, 'valid html is not modified.'; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/error_html') ); isnt $res->content, $error_html, 'error html is modified.'; is $res->content, $error_html_res_streaming, 'error html has error message.'; $res = $cb->( HTTP::Request->new(GET => 'http://example.com/broken_error_html') ); isnt $res->content, $broken_error_html, 'broken error html is modified.'; is $res->content, $broken_error_html_res, 'broken error html has error message.'; }, app => $streaming_handler, ); }; done_testing;