File: sugar_match_nested.t

package info (click to toggle)
libmagpie-perl 1.141660-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 3,524 kB
  • ctags: 337
  • sloc: perl: 4,081; xml: 405; makefile: 26
file content (132 lines) | stat: -rw-r--r-- 4,971 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use strict;
use warnings;
use Test::More;
use FindBin;
use lib "$FindBin::Bin/../lib";

use Plack::Test;
use Plack::Builder;
use HTTP::Request::Common;
use Plack::Middleware::Magpie;

my $accept = [
    [ 'text_en' => 1.0, 'text/plain', undef, undef, 'en', undef ],
    [ 'text_de' => 1.0, 'text/plain', undef, undef, 'de', undef ],
];

my $handler = builder {
    enable "Magpie", context => {}, accept_matrix => $accept, pipeline => [
        machine {
            match( qr|^/myapp| => [
                'Magpie::Pipeline::Moe',
                match( qr|right| => [
                    'Magpie::Pipeline::CurlyArgs' => {simple_argument =>'RIGHT'}
                ]),
                match( qr|wrong| => [
                    'Magpie::Pipeline::CurlyArgs' =>{simple_argument =>'WRONG'},
                    match( qr|pernicious| => [
                        'Magpie::Pipeline::CurlyArgs' =>{simple_argument =>'TERRIBLE'},
                    ]),
                ]),
                'Magpie::Pipeline::Larry'
            ]);
            match( qr|^/store| => [
                match_template('^/store/{store_id}' => [
                    'Magpie::Pipeline::PathMadness',
                    match_template('^/store/{store_id}/{product_id}$' => [
                        'Magpie::Pipeline::PathMadness'
                    ]),
                ]),
            ]);
#
            match( qr|^/env| => [
                match_env({ SERVER_NAME => qr|localhost| } => [
                    'Magpie::Pipeline::Moe',
                    match_env( { HTTP_ACCEPT => 'text/plain' } => [
                        'Magpie::Pipeline::CurlyArgs' =>
                            {simple_argument =>'PLAIN'}
                    ]),
                    match_env( { HTTP_ACCEPT => 'text/xml' } => [
                        'Magpie::Pipeline::CurlyArgs' =>
                            {simple_argument =>'XML'}
                    ]),
                ]),
                'Magpie::Pipeline::Larry'
            ]);
#
            match( qr|^/accept| => [
                'Magpie::Pipeline::Moe',
                match_accept( 'text_de' => [
                    'Magpie::Pipeline::CurlyArgs' =>
                    {simple_argument =>'DANKE'}
                ]),
                match_accept( 'text_en' => [
                    'Magpie::Pipeline::CurlyArgs' =>
                    {simple_argument =>'THANKS'}
                ]),
                'Magpie::Pipeline::Larry'
            ]),
        },
    ];
};

test_psgi
    app    => $handler,
    client => sub {
        my $cb = shift;
        {
            my $res = $cb->(GET "http://localhost/myapp");
            like $res->content, qr/_moebaz__moebar__larryfoo__larrybar_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/myapp/right");
            like $res->content, qr/_moebaz__moebar__curlyfoo_RIGHT_larryfoo__larrybar_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/myapp/wrong");
            like $res->content, qr/_moebaz__moebar__curlyfoo_WRONG_larryfoo__larrybar_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/myapp/pernicious");
            like $res->content, qr/_moebaz__moebar__larryfoo__larrybar_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/myapp/wrong/pernicious");
            like $res->content, qr/_moebaz__moebar__curlyfoo_WRONG_curlyfoo_TERRIBLE_larryfoo__larrybar_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/store/aabbcc");
            like $res->content, qr/^pathmadness__store_id::aabbcc_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/store/ddeeff/1234");
            like $res->content, qr/^pathmadness__store_id::ddeeff_pathmadness__product_id::1234__store_id::ddeeff_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/env",  Accept => 'text/plain' );
            like $res->content, qr/_moebaz__moebar__curlyfoo_PLAIN_larryfoo__larrybar_/;
        }
        {
            my $res = $cb->(GET "http://localhost/env",  Accept => 'text/xml' );
            like $res->content, qr/_moebaz__moebar__curlyfoo_XML_larryfoo__larrybar_/;
        }
        {
            my $res = $cb->(GET "http://localhost/env");
            like $res->content, qr/_moebaz__moebar__larryfoo__larrybar_/;
        }
        {
            my $res = $cb->(GET "http://faketld/env");
            like $res->content, qr/_larryfoo__larrybar_/;
        }
        {
            my $res = $cb->(GET "http://localhost/accept/", Accept => 'application/x-xml', 'Accept-Language' => 'de; q=1.0, en; q=0.5');
            like $res->content, qr/_moebaz__moebar__larryfoo__larrybar_\b/;
        }
        {
            my $res = $cb->(GET "http://localhost/accept/", Accept => 'text/plain', 'Accept-Language' => 'de; q=1.0, en; q=0.5');
            like $res->content, qr/_moebaz__moebar__curlyfoo_DANKE_larryfoo__larrybar_\b/;
        }

    };

done_testing();