Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2d5eced
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use inc::Module::Install; | ||
|
||
name 'Plack-Middleware-ForceEnv'; | ||
all_from 'lib/Plack/Middleware/ForceEnv.pm'; | ||
|
||
requires Plack => 0.9925; | ||
|
||
auto_install; | ||
WriteAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use strict; | ||
use warnings; | ||
|
||
package Plack::Middleware::ForceEnv; | ||
use parent 'Plack::Middleware'; | ||
|
||
our $VERSION = '0.01'; | ||
|
||
sub call { | ||
my ($self, $env) = @_; | ||
|
||
# Add to env whatever the user gave us | ||
$env = { | ||
%$env, | ||
map { $_ => $self->{$_} } grep { $_ ne 'app' } keys %$self | ||
}; | ||
|
||
return $self->app->($env); | ||
} | ||
|
||
1; | ||
__END__ | ||
=head1 NAME | ||
Plack::Middleware::ForceEnv - Force set environment variables for testing | ||
=head1 SYNOPSIS | ||
# in app.psgi | ||
use Plack::Builder; | ||
builder { | ||
enable 'ForceEnv' => | ||
REMOTE_ADDR => "127.0.0.1", | ||
REMOTE_USER => "trs"; | ||
$app; | ||
}; | ||
# with plackup | ||
plackup -e 'enable ForceEnv => REMOTE_USER => "trs"' app.psgi | ||
=head1 DESCRIPTION | ||
ForceEnv modifies the environment passed to the application by adding your | ||
specified key value pairs. | ||
This is primarily useful when testing apps under plackup (or similar) in a | ||
development environment. | ||
=head1 AUTHOR | ||
Thomas Sibley <tsibley@cpan.org> | ||
=head1 LICENSE | ||
This library is free software; you may redistribute it and/or modify it under | ||
the same terms as Perl itself. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use HTTP::Request::Common; | ||
use Plack::Test; | ||
use Plack::Builder; | ||
|
||
plan tests => 9; | ||
|
||
my $test = sub { | ||
my %options = (@_); | ||
my $app = builder { | ||
enable "ForceEnv" => %options; | ||
sub { | ||
my $env = shift; | ||
[ | ||
200, | ||
[ 'Content-Type' => 'text/plain' ], | ||
[ join '|', join '=', map { $_ => $env->{$_} } keys %$env ] | ||
] | ||
}; | ||
}; | ||
}; | ||
|
||
# Nothing | ||
test_psgi app => $test->(), client => sub { | ||
my $res = shift->(GET "/"); | ||
is $res->content_type, 'text/plain'; | ||
unlike $res->content, qr/app=/; | ||
}; | ||
|
||
# REMOTE_USER | ||
test_psgi app => $test->(REMOTE_USER => "trs"), client => sub { | ||
my $res = shift->(GET "/"); | ||
is $res->content_type, 'text/plain'; | ||
like $res->content, qr/REMOTE_USER=trs/; | ||
unlike $res->content, qr/app=/; | ||
}; | ||
|
||
# Multiple | ||
test_psgi app => $test->(REMOTE_ADDR => "10.0.0.1", foo => "bar"), client => sub { | ||
my $res = shift->(GET "/"); | ||
is $res->content_type, 'text/plain'; | ||
like $res->content, qr/REMOTE_ADDR=10\.0\.0\.1/; | ||
like $res->content, qr/foo=bar/; | ||
unlike $res->content, qr/app=/; | ||
}; | ||
|
||
done_testing; |