From 097b2afb17da42878c8fffaf7ebdab1ad0cb49fa Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 16 Sep 2022 07:07:00 +0000 Subject: [PATCH] Bug 29744: (QA follow-up) Add underscore test, move to OO The underscore test comes from bug 31468. Context methods should actually be OO. Test plan: Run t/Context.t Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Context.pm | 8 ++++++-- t/Context.t | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 823f8cb89c..8cd7681e6c 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -1007,7 +1007,10 @@ this is a PSGI app or a CGI app, and implementing code as appropriate. =cut -sub psgi_env { any { /(^psgi\.|^plack\.)/i } keys %ENV }; +sub psgi_env { + my ( $self ) = @_; + return any { /^(psgi|plack)[._]/i } keys %ENV; +} =head3 is_internal_PSGI_request @@ -1019,8 +1022,9 @@ app #NOTE: This is not a very robust method but it's the best we have so far sub is_internal_PSGI_request { + my ( $self ) = @_; my $is_internal = 0; - if ( (__PACKAGE__->psgi_env) && ( $ENV{REQUEST_URI} !~ /^(\/intranet|\/opac)/ ) ){ + if( $self->psgi_env && ( $ENV{REQUEST_URI} !~ /^(\/intranet|\/opac)/ ) ) { $is_internal = 1; } return $is_internal; diff --git a/t/Context.t b/t/Context.t index f3e15a56c1..1e351157d8 100755 --- a/t/Context.t +++ b/t/Context.t @@ -18,7 +18,7 @@ use Modern::Perl; use DBI; -use Test::More tests => 32; +use Test::More tests => 33; use Test::MockModule; use Test::Warn; use YAML::XS; @@ -145,3 +145,34 @@ is( C4::Context->interface( 'CRON' ), 'cron', 'interface cron uc' ); $ENV{HTTPS} = 'ON'; is( C4::Context->https_enabled, 1, "ON HTTPS env returns 1"); } + +subtest 'psgi_env and is_internal_PSGI_request' => sub { + plan tests => 11; + + local %ENV = ( no_plack => 1 ); + is( C4::Context->psgi_env, q{}, 'no_plack' ); + $ENV{plackishere} = 1; + is( C4::Context->psgi_env, q{}, 'plackishere is wrong' ); + $ENV{'plack.ishere'} = 1; + is( C4::Context->psgi_env, 1, 'plack.ishere' ); + delete $ENV{'plack.ishere'}; + is( C4::Context->psgi_env, q{}, 'plack.ishere was here' ); + $ENV{'plack_ishere'} = 1; + is( C4::Context->psgi_env, 1, 'plack_ishere' ); + delete $ENV{'plack_ishere'}; + $ENV{'psgi_whatever'} = 1; + is( C4::Context->psgi_env, 1, 'psgi_whatever' ); + delete $ENV{'psgi_whatever'}; + $ENV{'psgi.whatever'} = 1; + is( C4::Context->psgi_env, 1, 'psgi.whatever' ); + delete $ENV{'psgi.whatever'}; + $ENV{'PSGI.UPPERCASE'} = 1; + is( C4::Context->psgi_env, 1, 'PSGI uppercase' ); + + $ENV{'REQUEST_URI'} = '/intranet/whatever'; + is( C4::Context->is_internal_PSGI_request, 0, 'intranet not considered internal in regex' ); + $ENV{'REQUEST_URI'} = '/api/v1/tralala'; + is( C4::Context->is_internal_PSGI_request, 1, 'api considered internal in regex' ); + delete $ENV{'PSGI.UPPERCASE'}; + is( C4::Context->is_internal_PSGI_request, 0, 'api but no longer PSGI' ); +}; -- 2.39.5