File: forward.t

package info (click to toggle)
libdancer2-perl 0.400001%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,580 kB
  • sloc: perl: 8,461; makefile: 9
file content (183 lines) | stat: -rw-r--r-- 4,402 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use strict;
use warnings;
use Test::More import => ['!pass'];
use Plack::Test;
use HTTP::Request::Common;
use Ref::Util qw<is_coderef>;

use Dancer2;

set behind_proxy => 1;

get '/' => sub {
    'home:' . join( ',', params );
};
get '/bounce/' => sub {
    return forward '/';
};
get '/bounce/:withparams/' => sub {
    return forward '/';
};
get '/bounce2/adding_params/' => sub {
    return forward '/', { withparams => 'foo' };
};
post '/simple_post_route/' => sub {
    'post:' . join( ',', params );
};
get '/go_to_post/' => sub {
    return forward '/simple_post_route/', { foo => 'bar' },
      { method => 'post' };
};
get '/proxy/' => sub {
    return uri_for('/');
};
get '/forward_with_proxy/' => sub {
    forward '/proxy/';
};

# NOT SUPPORTED IN DANCER2
# In dancer2, vars are alive for only one request flow, a forward initiate a
# new request flow, then the vars HashRef is destroyed.
#
# get '/b' => sub { vars->{test} = 1;  forward '/a'; };
# get '/a' => sub { return "test is " . var('test'); };

my $app = __PACKAGE__->to_app;
ok( is_coderef($app), 'Got app' );

test_psgi $app, sub {
    my $cb = shift;
    is( $cb->( GET '/' )->code, 200, '[GET /] Correct code' );
    is( $cb->( GET '/' )->content, 'home:', '[GET /] Correct content' );

    is( $cb->( GET '/bounce/' )->code, 200, '[GET /bounce] Correct code' );
    is(
        $cb->( GET '/bounce/' )->content,
        'home:',
        '[GET /bounce] Correct content',
    );

    is(
        $cb->( GET '/bounce/thesethings/' )->code,
        200,
        '[GET /bounce/thesethings/] Correct code',
    );

    is(
        $cb->( GET '/bounce/thesethings/' )->content,
        'home:withparams,thesethings',
        '[GET /bounce/thesethings/] Correct content',
    );

    is(
        $cb->( GET '/bounce2/adding_params/' )->code,
        200,
        '[GET /bounce2/adding_params/] Correct code',
    );

    is(
        $cb->( GET '/bounce2/adding_params/' )->content,
        'home:withparams,foo',
        '[GET /bounce2/adding_params/] Correct content',
    );

    is(
        $cb->( GET '/go_to_post/' )->code,
        200,
        '[GET /go_to_post/] Correct code',
    );

    is(
        $cb->( GET '/go_to_post/' )->content,
        'post:foo,bar',
        '[GET /go_to_post/] Correct content',
    );

    # NOT SUPPORTED
    # response_status_is  [ GET => '/b' ] => 200;
    # response_content_is [ GET => '/b' ] => 'test is 1';

    {
        my $res = $cb->( GET '/bounce/' );

        is(
            $res->headers->content_length,
            5,
            '[GET /bounce/] Correct content length',
        );

        is(
            $res->headers->content_type,
            'text/html',
            '[GET /bounce/] Correct content type',
        );

        is(
            $res->headers->content_type_charset,
            'UTF-8',
            '[GET /bounce/] Correct content type charset',
        );

        is(
            $res->headers->server,
            "Perl Dancer2 " . Dancer2->VERSION,
            '[GET /bounce/] Correct Server',
        );

    }

    # checking post
    post '/'        => sub {'post-home'};
    post '/bounce/' => sub { forward('/') };

    is( $cb->( POST '/' )->code, 200, '[POST /] Correct code' );
    is( $cb->( POST '/' )->content, 'post-home', '[POST /] Correct content' );

    is(
        $cb->( POST '/bounce/' )->code,
        200,
        '[POST /bounce/] Correct code',
    );

    is(
        $cb->( POST '/bounce/' )->content,
        'post-home',
        '[POST /bounce/] Correct content',
    );

    {
        my $res = $cb->( POST '/bounce/' );

        is(
            $res->headers->content_length,
            9,
            '[POST /bounce/] Correct content length',
        );

        is(
            $res->headers->content_type,
            'text/html',
            '[POST /bounce/] Correct content type',
        );

        is(
            $res->headers->content_type_charset,
            'UTF-8',
            '[POST /bounce/] Correct content type charset',
        );

        is(
            $res->headers->server,
            "Perl Dancer2 " . Dancer2->VERSION,
            '[POST /bounce/] Correct Server',
        );
    }

    is(
        $cb->( GET '/forward_with_proxy/', 'X-Forwarded-Proto' => 'https' )->content,
        'https://localhost/',
        '[GET /forward_with_proxy/] maintained is_behind_proxy',
    );
};

done_testing;