Dancer2

 view release on metacpan or  search on metacpan

t/forward.t  view on Meta::CPAN

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';



( run in 1.680 second using v1.01-cache-2.11-cpan-39bf76dae61 )