look a failing test case...
John Napiorkowski [Tue, 17 Dec 2013 23:37:20 +0000 (17:37 -0600)]
t/lib/TestMiddlewareFromConfig.pm [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/Controller/Root.pm [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/Custom.pm [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/share/static/forced.txt [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/share/static/message.txt [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/share/static2/message2.txt [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/share/static3/message3.txt [new file with mode: 0644]
t/lib/TestMiddlewareFromConfig/testmiddlewarefromconfig.pl [new file with mode: 0644]
t/plack-middleware-config.t [new file with mode: 0644]

diff --git a/t/lib/TestMiddlewareFromConfig.pm b/t/lib/TestMiddlewareFromConfig.pm
new file mode 100644 (file)
index 0000000..ca18208
--- /dev/null
@@ -0,0 +1,9 @@
+package TestMiddlewareFromConfig;
+
+use Catalyst qw/ConfigLoader/;
+
+## Proof this is good config
+##__PACKAGE__->config( do TestMiddlewareFromConfig->path_to('testmiddlewarefromconfig.pl') );
+
+__PACKAGE__->setup;
+
diff --git a/t/lib/TestMiddlewareFromConfig/Controller/Root.pm b/t/lib/TestMiddlewareFromConfig/Controller/Root.pm
new file mode 100644 (file)
index 0000000..27be020
--- /dev/null
@@ -0,0 +1,13 @@
+package TestMiddlewareFromConfig::Controller::Root;
+
+use Moose;
+use MooseX::MethodAttributes;
+
+extends 'Catalyst::Controller';
+
+sub default : Path { }
+sub welcome : Path(welcome) {
+  pop->res->body('Welcome to Catalyst');
+}
+
+__PACKAGE__->meta->make_immutable;
diff --git a/t/lib/TestMiddlewareFromConfig/Custom.pm b/t/lib/TestMiddlewareFromConfig/Custom.pm
new file mode 100644 (file)
index 0000000..dde1613
--- /dev/null
@@ -0,0 +1,8 @@
+package TestMiddlewareFromConfig::Custom;
+
+use strict;
+use warnings;
+
+use parent qw/Plack::Middleware::Static/;
+
+1;
diff --git a/t/lib/TestMiddlewareFromConfig/share/static/forced.txt b/t/lib/TestMiddlewareFromConfig/share/static/forced.txt
new file mode 100644 (file)
index 0000000..47b6118
--- /dev/null
@@ -0,0 +1 @@
+forced message
diff --git a/t/lib/TestMiddlewareFromConfig/share/static/message.txt b/t/lib/TestMiddlewareFromConfig/share/static/message.txt
new file mode 100644 (file)
index 0000000..9bdf9d1
--- /dev/null
@@ -0,0 +1 @@
+static message
diff --git a/t/lib/TestMiddlewareFromConfig/share/static2/message2.txt b/t/lib/TestMiddlewareFromConfig/share/static2/message2.txt
new file mode 100644 (file)
index 0000000..9bdf9d1
--- /dev/null
@@ -0,0 +1 @@
+static message
diff --git a/t/lib/TestMiddlewareFromConfig/share/static3/message3.txt b/t/lib/TestMiddlewareFromConfig/share/static3/message3.txt
new file mode 100644 (file)
index 0000000..9bdf9d1
--- /dev/null
@@ -0,0 +1 @@
+static message
diff --git a/t/lib/TestMiddlewareFromConfig/testmiddlewarefromconfig.pl b/t/lib/TestMiddlewareFromConfig/testmiddlewarefromconfig.pl
new file mode 100644 (file)
index 0000000..4df352d
--- /dev/null
@@ -0,0 +1,28 @@
+use Plack::Middleware::Static;
+
+my $static = Plack::Middleware::Static->new(
+  path => qr{^/static/}, root => TestMiddlewareFromConfig->path_to('share'));
+
+my $conf = +{
+  'Controller::Root', { namespace => '' },
+  'psgi_middleware', [
+    'Head',
+    $static,
+    'Static', { path => qr{^/static2/}, root => TestMiddlewareFromConfig->path_to('share') },
+    'Runtime',
+    '+TestMiddleware::Custom', { path => qr{^/static3/}, root => TestMiddlewareFromConfig->path_to('share') },
+    sub {
+      my $app = shift;
+      return sub {
+        my $env = shift;
+        if($env->{PATH_INFO} =~m/forced/) {
+          Plack::App::File->new(file=>TestMiddlewareFromConfig->path_to(qw/share static forced.txt/))
+            ->call($env);
+        } else {
+          return $app->($env);
+        }
+      },
+    },
+
+  ],
+};
diff --git a/t/plack-middleware-config.t b/t/plack-middleware-config.t
new file mode 100644 (file)
index 0000000..728bff6
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use FindBin;
+use Test::More;
+use HTTP::Request::Common;
+
+BEGIN { eval { require Catalyst::Plugin::ConfigLoader; 1; } ||
+    plan skip_all => 'Need Catalyst::Plugin::ConfigLoader' }
+
+use lib "$FindBin::Bin/lib";
+use Catalyst::Test 'TestMiddlewareFromConfig';
+
+ok my($res, $c) = ctx_request('/');
+
+{
+  ok my $response = request GET $c->uri_for_action('/welcome'),
+    'got welcome from a catalyst controller';
+
+  is $response->content, 'Welcome to Catalyst',
+    'expected content body';
+}
+
+{
+  ok my $response = request GET $c->uri_for('/static/message.txt'),
+    'got welcome from a catalyst controller';
+
+  like $response->content, qr'static message',
+    'expected content body';
+}
+
+{
+  ok my $response = request GET $c->uri_for('/static2/message2.txt'),
+    'got welcome from a catalyst controller';
+
+  like $response->content, qr'static message',
+    'expected content body';
+}
+
+{
+  ok my $response = request GET $c->uri_for('/static3/message3.txt'),
+    'got welcome from a catalyst controller';
+
+  like $response->content, qr'static message',
+    'expected content body';
+}
+
+{
+  ok my $response = request GET $c->uri_for('/forced'),
+    'got welcome from a catalyst controller';
+
+  like $response->content, qr'forced message',
+    'expected content body';
+
+  ok $response->headers->{"x-runtime"}, "Got value for expected middleware";
+}
+
+done_testing;