Skip to content

Commit

Permalink
First cut of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsibley committed Jan 15, 2011
0 parents commit 2d5eced
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Makefile.PL
@@ -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;
60 changes: 60 additions & 0 deletions lib/Plack/Middleware/ForceEnv.pm
@@ -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
49 changes: 49 additions & 0 deletions t/basic.t
@@ -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;

0 comments on commit 2d5eced

Please sign in to comment.