File: 006_psgi.t

package info (click to toggle)
libtext-xslate-perl 3.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,108 kB
  • sloc: perl: 19,756; ansic: 214; pascal: 182; makefile: 9; cs: 8
file content (105 lines) | stat: -rw-r--r-- 2,557 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!perl -w

use strict;
use Test::Requires { 'Plack' => 0.99, 'Devel::StackTrace' => 1.30 };
use Test::More;

use HTTP::Request;
use Plack::Test;
use Plack::Response;
use Plack::Builder;

use Text::Xslate;
use Text::Xslate::Util qw(p);
use lib "t/lib";
use Util;


my $tx = Text::Xslate->new(
    path  => path,
    cache => 0,
);

my $n = 2;

test_psgi
    app => sub {
        my($env) = @_;
        my $res = Plack::Response->new(200);
        $res->body( eval { $tx->render('hello.tx', { lang => 'Xslate' }) } || $@ );
        return $res->finalize();
    },
    client => sub {
        my $cb = shift;
        my $req = HTTP::Request->new(GET => "http://localhost/hello");
        for(1 .. $n) {
            my $res = $cb->($req);
            is $res->content, "Hello, Xslate world!\n", 'render';
        }
    },
;

test_psgi
    app => sub {
        my($env) = @_;
        my $res = Plack::Response->new(200);
        $res->body( eval { $tx->render_string(':include "no_such_file.tx"') } || $@ );
        return $res->finalize();
    },
    client => sub {
        my $cb = shift;
        my $req = HTTP::Request->new(GET => "http://localhost/hello");
        for(1 .. $n) {
            my $res = $cb->($req);
            like $res->content, qr/\b no_such_file\.tx \b/xms, 'fatal';
        }
    },
;

note 'with error handlers';

$tx = Text::Xslate->new(
    warn_handler => \&Carp::croak,
);

test_psgi
    app => builder {
        return sub {
            my($env) = @_;
            my $res = Plack::Response->new(200);
            $res->body( $tx->render_string('<: $lang.foobar() :>', { lang => 'Xslate' })  );
            return $res->finalize();
        };
    },
    client => sub {
        my $cb = shift;
        my $req = HTTP::Request->new(GET => "http://localhost/hello");
        for(1 .. $n) {
            my $res = $cb->($req);
            like $res->content, qr/\b foobar \b/xms, 'error handler';
        }
    },
;

test_psgi
    app => builder {
        enable 'StackTrace', no_print_errors =>1;

        return sub {
            my($env) = @_;
            my $res = Plack::Response->new(200);
            $res->body( $tx->render_string('<: $lang.foobar() :>', { lang => 'Xslate' })  );
            return $res->finalize();
        };
    },
    client => sub {
        my $cb = shift;
        my $req = HTTP::Request->new(GET => "http://localhost/hello");
        for(1 .. $n) {
            my $res = $cb->($req);
            like $res->content, qr/\b foobar \b/xms, 'error handler + StackTrace';
        }
    },
;

done_testing;