From e56b14c9bebe9476541c0cb0f536fb4b70da4166 Mon Sep 17 00:00:00 2001 From: Andy Beverley Date: Sat, 18 Oct 2025 18:25:09 +0100 Subject: [PATCH] Enable custom PSGI variables to be used for testing This commit enables custom PSGI environment variables to be used during testing. For example, REMOTE_ADDR could be used to simulate an IP address for the client. --- lib/Plack/Test.pm | 5 ++++- lib/Plack/Test/MockHTTP.pm | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Plack/Test.pm b/lib/Plack/Test.pm index 11f9e6929..e7a77d438 100644 --- a/lib/Plack/Test.pm +++ b/lib/Plack/Test.pm @@ -100,7 +100,7 @@ to be a valid PSGI application code reference. =item request - $res = $test->request($request); + $res = $test->request($request, %options); takes an HTTP::Request object, runs it through the PSGI application to test and returns an HTTP::Response object. @@ -165,6 +165,9 @@ The available values for the backend are: (Default) Creates a PSGI env hash out of HTTP::Request object, runs the PSGI application in-process and returns HTTP::Response. +Custom PSGI environment variables can be passed using %options of the request() +method. + =item Server Runs one of Plack::Handler backends (C by default) and diff --git a/lib/Plack/Test/MockHTTP.pm b/lib/Plack/Test/MockHTTP.pm index f16e63d35..c9ac32b8f 100644 --- a/lib/Plack/Test/MockHTTP.pm +++ b/lib/Plack/Test/MockHTTP.pm @@ -14,11 +14,11 @@ sub new { } sub request { - my($self, $req) = @_; + my($self, $req) = (shift, shift); $req->uri->scheme('http') unless defined $req->uri->scheme; $req->uri->host('localhost') unless defined $req->uri->host; - my $env = $req->to_psgi; + my $env = $req->to_psgi(@_); my $res = try { HTTP::Response->from_psgi($self->{app}->($env));