From b7d394401b3cd70878e693ec3a589d1d28b73c73 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 9 Jun 2023 06:46:43 +0000 Subject: [PATCH] Bug 33967: Add unit test to ensure SetEnv doesn't compromise $env This patch includes a unit test which checks the memory address of the $env hashref in middlewares before and after Koha::Middleware::Env is applied. It succeeds when the same $env is maintained before and after this middleware. It fails if the $env has been overwritten with a new hashref (as evidenced by the new memory address for the hash). Signed-off-by: Kyle M Hall --- t/Koha/Middleware/SetEnv.t | 85 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 t/Koha/Middleware/SetEnv.t diff --git a/t/Koha/Middleware/SetEnv.t b/t/Koha/Middleware/SetEnv.t new file mode 100755 index 0000000000..6872c2c3a5 --- /dev/null +++ b/t/Koha/Middleware/SetEnv.t @@ -0,0 +1,85 @@ +#!/usr/bin/perl + +# +# Copyright 2023 Prosentient Systems +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use strict; +use warnings; +use Test::More tests => 2; +use Test::Warn; + +use t::lib::Mocks; +use_ok("Koha::Middleware::SetEnv"); +use Plack::Builder; +use Plack::Util; + +subtest 'Test $env integrity' => sub { + plan tests => 2; + + my $app = sub { + my $resp = [ + 200, + [ + 'Content-Type', + 'text/plain', + 'Content-Length', + 12 + ], + ['Koha is cool'] + ]; + return $resp; + }; + my $env = {}; + my $correct_hash_addr = "$env"; + my @ref_to_test = (); + + $app = builder { + enable sub { + my $app = shift; + sub { + my $env = shift; + push( @ref_to_test, "$env" ); + + # do preprocessing + my $res = $app->($env); + + # do postprocessing + return $res; + }; + }; + enable "+Koha::Middleware::SetEnv"; + enable sub { + my $app = shift; + sub { + my $env = shift; + push( @ref_to_test, "$env" ); + + # do preprocessing + my $res = $app->($env); + + # do postprocessing + return $res; + }; + }; + $app; + }; + + my $res = Plack::Util::run_app( $app, $env ); + is( $correct_hash_addr, $ref_to_test[0], "First hash ref address correct before middleware applied" ); + is( $correct_hash_addr, $ref_to_test[1], "Second hash ref address correct after middleware applied" ); +}; -- 2.30.2