From 2b8b1208ad357d3e8558e9b34cb99e5a299887b8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 29 Sep 2011 04:00:25 -0500 Subject: allow routing directly to the view --- t/route.t | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 t/route.t (limited to 't/route.t') diff --git a/t/route.t b/t/route.t new file mode 100644 index 0000000..c4507eb --- /dev/null +++ b/t/route.t @@ -0,0 +1,116 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use Plack::Test; +use FindBin; + +use HTTP::Request::Common; +use Path::Class (); + +{ + package Foo; + use OX; + + has template_root => ( + is => 'ro', + isa => 'Str', + block => sub { + Path::Class::dir($FindBin::Bin)->subdir('data', 'route', 'templates', 'foo')->stringify + }, + ); + + has view => ( + is => 'ro', + isa => 'OX::View::TT', + dependencies => ['template_root'], + ); + + router as { + route '/' => 'view.template', ( + template => 'index.tt', + ); + route '/foo' => 'view.template', ( + template => 'foo.tt', + ); + }; +} + +test_psgi + app => Foo->new->to_app, + client => sub { + my $cb = shift; + + { + my $res = $cb->(GET 'http://localhost/'); + is($res->code, 200, "right code"); + is($res->content, "Hello world\n", "right content"); + } + { + my $res = $cb->(GET 'http://localhost/foo'); + is($res->code, 200, "right code"); + is($res->content, "

/foo

\n", "right content"); + } + }; + +{ + package Bar::View; + use Moose; + + extends 'OX::View::TT'; + + around render => sub { + my $orig = shift; + my $self = shift; + my ($r, $template, $params) = @_; + $params->{my_thing} = 'BAR'; + return $self->$orig(@_); + }; +} + +{ + package Bar; + use OX; + + has template_root => ( + is => 'ro', + isa => 'Str', + block => sub { + Path::Class::dir($FindBin::Bin)->subdir('data', 'route', 'templates', 'bar')->stringify + }, + ); + + has view => ( + is => 'ro', + isa => 'Bar::View', + dependencies => ['template_root'], + ); + + router as { + route '/' => 'view.template', ( + template => 'index.tt', + ); + route '/foo' => 'view.template', ( + template => 'foo.tt', + ); + }; +} + +test_psgi + app => Bar->new->to_app, + client => sub { + my $cb = shift; + + { + my $res = $cb->(GET 'http://localhost/'); + is($res->code, 200, "right code"); + is($res->content, "Hello world: BAR\n", "right content"); + } + { + my $res = $cb->(GET 'http://localhost/foo'); + is($res->code, 200, "right code"); + is($res->content, "

/foo: BAR

\n", "right content"); + } + }; + +done_testing; -- cgit v1.2.3-54-g00ecf