TPSGI.t 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use warnings FATAL => 'all';
  4. use re '/aasx';
  5. use Test2::V0;
  6. use Test2::Plugin::NoWarnings;
  7. use File::Temp qw{tempdir};
  8. use Mojo::File ();
  9. use Test::MockModule;
  10. use FindBin::libs;
  11. use TPSGI;
  12. # Every line the log gets, so a test can ask whether the reason for a 500 is
  13. # among them.
  14. package Test::Logger::Capture {
  15. use parent qw{Log::Dispatch::Output};
  16. our @lines;
  17. sub new {
  18. my ( $class, %p ) = @_;
  19. my $self = bless {}, $class;
  20. $self->_basic_init( name => 'capture', min_level => $p{min_level} );
  21. return $self;
  22. }
  23. sub log_message {
  24. my ( $self, %p ) = @_;
  25. push( @lines, $p{message} );
  26. return;
  27. }
  28. }
  29. local $INC{'Test/Logger/Capture.pm'} = __FILE__;
  30. my $dir = tempdir( CLEANUP => 1 );
  31. my %options = (
  32. log_name => "$dir/tpsgi.log",
  33. log_dir => $dir,
  34. verbose => 0,
  35. loggers => ['Test::Logger::Capture'],
  36. );
  37. sub fake_env {
  38. # The fake psgi.errors, which the caller keeps open.
  39. open( my $errors, '>', \my $written ) or die "Cannot open a scalar: $!"; ## no critic (RequireBriefOpen)
  40. return ( { 'psgi.errors' => $errors }, \$written );
  41. }
  42. # TPSGI builds its log once per process, so this has to run before anything
  43. # else here builds it: a logger that will not load is only a failure the first
  44. # time.
  45. subtest 'cannot_start, when building the log is what failed' => sub {
  46. my ( $env, $written ) = fake_env();
  47. my $res;
  48. ok(
  49. lives { $res = TPSGI::cannot_start( "Could not load Bogus.pm!\n", $env, %options, loggers => ['Bogus::Logger::That::Is::Not'] ) },
  50. 'a logger that will not load does not turn the 500 into an exception'
  51. ) or note $@;
  52. is( $res->[0], 500, 'it still answers 500' );
  53. like( $$written, qr/Could[ ]not[ ]load[ ]Bogus\.pm/, 'and psgi.errors still gets the reason' );
  54. };
  55. subtest 'cannot_start' => sub {
  56. local @Test::Logger::Capture::lines;
  57. my ( $env, $written ) = fake_env();
  58. my $res = TPSGI::cannot_start( "Could not load Bogus.pm!\nCan't locate Bogus/Dep.pm in \@INC\n", $env, %options );
  59. is( $res->[0], 500, 'answers 500' );
  60. like( $$written, qr/Can't[ ]locate[ ]Bogus\/Dep\.pm/, 'psgi.errors gets the whole reason' );
  61. my $reason = qr/Could[ ]not[ ]load[ ]Bogus[.]pm!/;
  62. like( \@Test::Logger::Capture::lines, bag { item qr/Could[ ]not[ ]start:[ ]$reason\nCan't[ ]locate/; etc() }, 'and so does the log' );
  63. ok( lives { TPSGI::cannot_start( "no env\n", undef, %options ) }, 'a missing env is not a second failure' ) or note $@;
  64. };
  65. subtest 'cannot_start retires the worker' => sub {
  66. my ($env) = fake_env();
  67. $env->{'psgix.harakiri'} = 1;
  68. TPSGI::cannot_start( "Could not load Bogus.pm!\n", $env, %options );
  69. ok( $env->{'psgix.harakiri.commit'}, 'a server that can retire the worker is told to' );
  70. ($env) = fake_env();
  71. TPSGI::cannot_start( "Could not load Bogus.pm!\n", $env, %options );
  72. ok( !exists $env->{'psgix.harakiri.commit'}, 'a server that cannot is not asked to' );
  73. };
  74. subtest 'cannot_start answers with a page' => sub {
  75. my $error = "Could not load Bogus.pm!\nCan't locate <Bogus/Dep.pm> in \@INC & \"elsewhere\"\n";
  76. my $res = TPSGI::cannot_start( $error, ( fake_env() )[0], %options );
  77. my %head = @{ $res->[1] };
  78. is( $head{'Content-type'}, 'text/html', 'in HTML' );
  79. like( $res->[2][0], qr/<h1>Internal[ ]Server[ ]Error<\/h1>/, 'saying what happened' );
  80. unlike( $res->[2][0], qr/Bogus/, 'and, without TPSGI_DEBUG, not why' );
  81. local $TPSGI::debug = 1;
  82. $res = TPSGI::cannot_start( $error, ( fake_env() )[0], %options );
  83. my $escaped = q{Can't locate &#60;Bogus/Dep.pm&#62; in @INC &#38; &#34;elsewhere&#34;};
  84. like( $res->[2][0], qr/\Q$escaped\E/, 'with TPSGI_DEBUG, the error, escaped' );
  85. unlike( $res->[2][0], qr/<Bogus/, 'none of it read as markup' );
  86. };
  87. subtest 'app, when the request dies' => sub {
  88. local @Test::Logger::Capture::lines;
  89. my ( $env, $written ) = fake_env();
  90. my $mock = Test::MockModule->new( 'TPSGI', no_auto => 1 );
  91. $mock->redefine( respond => sub { die "Can't locate Bogus/Dep.pm in \@INC\n" } );
  92. my $tpsgi = bless( { %options, ip => '0.0.0.0' }, 'TPSGI' );
  93. my $res = $tpsgi->app($env);
  94. is( $res->[0], 500, 'answers 500' );
  95. like( $$written, qr/Can't[ ]locate[ ]Bogus\/Dep\.pm/, 'psgi.errors gets the reason' );
  96. like( \@Test::Logger::Capture::lines, bag { item qr/Request[ ]failed:[ ]Can't[ ]locate[ ]Bogus\/Dep\.pm/; etc() }, 'and so does the log, not just that a 500 happened' );
  97. };
  98. subtest 'serve_ranges' => sub {
  99. local @Test::Logger::Capture::lines;
  100. my $tpsgi = bless( { %options, ip => '0.0.0.0' }, 'TPSGI' );
  101. my $file = "$dir/ten-bytes";
  102. Mojo::File->new($file)->spew('0123456789');
  103. my %args = ( size => 10, headers => { 'Content-type' => 'text/plain' }, fullpath => '/ten-bytes' );
  104. open( my $fh, '<', $file ) or die "Cannot read $file: $!";
  105. my $res = $tpsgi->serve_ranges( $fh, [ [ 0, 4 ] ], %args );
  106. is( ref $res, 'CODE', 'a range inside the file is streamed' );
  107. is( [ grep { /GET[ ]416/ } @Test::Logger::Capture::lines ], [], 'and not logged as a 416' );
  108. close($fh) or die "Cannot close: $!";
  109. open( $fh, '<', $file ) or die "Cannot read $file: $!";
  110. $res = $tpsgi->serve_ranges( $fh, [ [ 0, 4 ], [ 20, 25 ] ], %args );
  111. is( $res->[0], 416, 'a range past the end is refused' );
  112. like( \@Test::Logger::Capture::lines, bag { item qr/GET[ ]416[ ]\/ten-bytes/; etc() }, 'and logged as a 416' );
  113. close($fh) or die "Cannot close: $!";
  114. };
  115. # A router's pattern is matched as the router wrote it, whatever flags this
  116. # distribution's own regexes carry.
  117. subtest 'extract_query' => sub {
  118. my $tpsgi = bless( { %options, ip => '0.0.0.0' }, 'TPSGI' );
  119. my $env = { REQUEST_METHOD => 'GET' };
  120. my $query = $tpsgi->extract_query( "/post/caf\N{LATIN SMALL LETTER E WITH ACUTE}", { pattern => '/post/(\w+)', captures => ['slug'] }, $env );
  121. is( $query->{slug}, "caf\N{LATIN SMALL LETTER E WITH ACUTE}", 'a \\w in a route matches a letter outside ASCII' );
  122. $query = $tpsgi->extract_query( '/a b/c', { pattern => '/a b/(\w+)', captures => ['name'] }, $env );
  123. is( $query->{name}, 'c', 'and a space in a route is a space' );
  124. $query = $tpsgi->extract_query( "/note/x\ny", { pattern => '/note/(.+)', captures => ['text'] }, $env );
  125. is( $query->{text}, undef, 'and a dot does not match a newline' );
  126. };
  127. done_testing();