TPSGI.t 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use strict;
  2. use warnings;
  3. use Test2::V0;
  4. use Test2::Plugin::NoWarnings;
  5. use Cwd qw{abs_path};
  6. use File::Temp qw{tempdir};
  7. use File::Basename qw{dirname};
  8. use Test::MockModule;
  9. use lib dirname( abs_path(__FILE__) ) . '/../lib';
  10. use TPSGI;
  11. # Every line the log gets, so a test can ask whether the reason for a 500 is
  12. # among them.
  13. package Test::Logger::Capture {
  14. use parent qw{Log::Dispatch::Output};
  15. our @lines;
  16. sub new {
  17. my ( $class, %p ) = @_;
  18. my $self = bless {}, $class;
  19. $self->_basic_init( name => 'capture', min_level => $p{min_level} );
  20. return $self;
  21. }
  22. sub log_message {
  23. my ( $self, %p ) = @_;
  24. push( @lines, $p{message} );
  25. return;
  26. }
  27. }
  28. $INC{'Test/Logger/Capture.pm'} = __FILE__;
  29. my $dir = tempdir( CLEANUP => 1 );
  30. my %options = (
  31. log_name => "$dir/tpsgi.log",
  32. log_dir => $dir,
  33. verbose => 0,
  34. loggers => ['Test::Logger::Capture'],
  35. );
  36. sub fake_env {
  37. open( my $errors, '>', \my $written ) or die "Cannot open a scalar: $!";
  38. return ( { 'psgi.errors' => $errors }, \$written );
  39. }
  40. # TPSGI builds its log once per process, so this has to run before anything
  41. # else here builds it: a logger that will not load is only a failure the first
  42. # time.
  43. subtest 'cannot_start, when building the log is what failed' => sub {
  44. my ( $env, $written ) = fake_env();
  45. my $res;
  46. ok( lives { $res = TPSGI::cannot_start( "Could not load Bogus.pm!\n", $env, %options, loggers => ['Bogus::Logger::That::Is::Not'] ) },
  47. 'a logger that will not load does not turn the 500 into an exception' )
  48. or note $@;
  49. is( $res->[0], 500, 'it still answers 500' );
  50. like( $$written, qr/Could not load Bogus\.pm/, 'and psgi.errors still gets the reason' );
  51. };
  52. subtest 'cannot_start' => sub {
  53. local @Test::Logger::Capture::lines;
  54. my ( $env, $written ) = fake_env();
  55. my $res = TPSGI::cannot_start( "Could not load Bogus.pm!\nCan't locate Bogus/Dep.pm in \@INC\n", $env, %options );
  56. is( $res->[0], 500, 'answers 500' );
  57. like( $$written, qr/Can't locate Bogus\/Dep\.pm/, 'psgi.errors gets the whole reason' );
  58. like( \@Test::Logger::Capture::lines, bag { item qr/Could not start: Could not load Bogus\.pm!\nCan't locate/; etc() }, 'and so does the log' );
  59. ok( lives { TPSGI::cannot_start( "no env\n", undef, %options ) }, 'a missing env is not a second failure' ) or note $@;
  60. };
  61. subtest 'cannot_start retires the worker' => sub {
  62. my ( $env ) = fake_env();
  63. $env->{'psgix.harakiri'} = 1;
  64. TPSGI::cannot_start( "Could not load Bogus.pm!\n", $env, %options );
  65. ok( $env->{'psgix.harakiri.commit'}, 'a server that can retire the worker is told to' );
  66. ( $env ) = fake_env();
  67. TPSGI::cannot_start( "Could not load Bogus.pm!\n", $env, %options );
  68. ok( !exists $env->{'psgix.harakiri.commit'}, 'a server that cannot is not asked to' );
  69. };
  70. subtest 'cannot_start answers with a page' => sub {
  71. my $error = "Could not load Bogus.pm!\nCan't locate <Bogus/Dep.pm> in \@INC & \"elsewhere\"\n";
  72. my $res = TPSGI::cannot_start( $error, ( fake_env() )[0], %options );
  73. my %head = @{ $res->[1] };
  74. is( $head{'Content-type'}, 'text/html', 'in HTML' );
  75. like( $res->[2][0], qr/<h1>Internal Server Error<\/h1>/, 'saying what happened' );
  76. unlike( $res->[2][0], qr/Bogus/, 'and, without TPSGI_DEBUG, not why' );
  77. local $TPSGI::debug = 1;
  78. $res = TPSGI::cannot_start( $error, ( fake_env() )[0], %options );
  79. like( $res->[2][0], qr/Can't locate &#60;Bogus\/Dep\.pm&#62; in \@INC &#38; &#34;elsewhere&#34;/, 'with TPSGI_DEBUG, the error, escaped' );
  80. unlike( $res->[2][0], qr/<Bogus/, 'none of it read as markup' );
  81. };
  82. subtest 'app, when the request dies' => sub {
  83. local @Test::Logger::Capture::lines;
  84. my ( $env, $written ) = fake_env();
  85. my $mock = Test::MockModule->new( 'TPSGI', no_auto => 1 );
  86. $mock->redefine( _app => sub { die "Can't locate Bogus/Dep.pm in \@INC\n" } );
  87. my $tpsgi = bless( { %options, ip => '0.0.0.0' }, 'TPSGI' );
  88. my $res = $tpsgi->app($env);
  89. is( $res->[0], 500, 'answers 500' );
  90. like( $$written, qr/Can't locate Bogus\/Dep\.pm/, 'psgi.errors gets the reason' );
  91. 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' );
  92. };
  93. done_testing();