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
|
use strict;
use warnings FATAL => 'all';
use Data::Dumper::Concise;
use Test::More 'no_plan';
use Plack::Test;
{
use Web::Simple 't::Web::Simple::SubDispatchArgs';
package t::Web::Simple::SubDispatchArgs;
use Web::Dispatch::Predicates;
has 'attr' => (is=>'ro');
sub dispatch_request {
my $self = shift;
## sub(/) {
match_path(qr/(?-xism:^(\/)$)/), sub {
$self->show_landing(@_);
},
## sub(/...) {
match_path_strip(qr/(?-xism:^()(\/.*)$)/) => sub {
match_and
(
match_method('GET'),
match_path(qr/(?-xism:^(\/user(?:\.\w+)?)$)/)
) => sub {
$self->show_users(@_);
},
match_path(qr/(?-xism:^(\/user\/([^\/]+?)(?:\.\w+)?)$)/), sub {
match_method('GET') => sub {
$self->show_user(@_);
},
match_and
(
match_method('POST'),
match_body
({
named => [
{
multi => "",
name => "id"
},
{
multi => 1,
name => "roles"
}
],
required => ["id"]
})
) => sub {
$self->process_post(@_);
}
},
}
};
sub show_landing {
my ($self, @args) = @_;
local $self->{_dispatcher};
local $args[-1]->{'Web::Dispatch.original_env'};
return [
200, ['Content-Type' => 'application/perl' ],
[::Dumper \@args],
];
}
sub show_users {
my ($self, @args) = @_;
local $self->{_dispatcher};
local $args[-1]->{'Web::Dispatch.original_env'};
return [
200, ['Content-Type' => 'application/perl' ],
[::Dumper \@args],
];
}
sub show_user {
my ($self, @args) = @_;
local $self->{_dispatcher};
local $args[-1]->{'Web::Dispatch.original_env'};
return [
200, ['Content-Type' => 'application/perl' ],
[::Dumper \@args],
];
}
sub process_post {
my ($self, @args) = @_;
local $self->{_dispatcher};
local $args[-1]->{'Web::Dispatch.original_env'};
return [
200, ['Content-Type' => 'application/perl' ],
[::Dumper \@args],
];
}
}
ok my $app = t::Web::Simple::SubDispatchArgs->new,
'made app';
sub run_request { $app->run_test_request(@_); }
ok my $get_landing = run_request(GET => 'http://localhost/' ),
'got landing';
cmp_ok $get_landing->code, '==', 200,
'200 on GET';
no strict 'refs';
{
my ($self, $env, @noextra) = @{eval($get_landing->content)||[]};
die $@ if $@;
is scalar(@noextra), 0, 'No extra stuff';
is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
is ref($env), 'HASH', 'Got hashref';
}
ok my $get_users = run_request(GET => 'http://localhost/user'),
'got user';
cmp_ok $get_users->code, '==', 200,
'200 on GET';
{
my ($self, $env, @noextra) = @{eval $get_users->content};
is scalar(@noextra), 0, 'No extra stuff';
is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
is ref($env), 'HASH', 'Got hashref';
}
ok my $get_user = run_request(GET => 'http://localhost/user/42'),
'got user';
cmp_ok $get_user->code, '==', 200,
'200 on GET';
{
my ($self, $env, @noextra) = @{eval $get_user->content};
is scalar(@noextra), 0, 'No extra stuff';
is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
is ref($env), 'HASH', 'Got hashref';
}
ok my $post_user = run_request(POST => 'http://localhost/user/42', id => '99' ),
'post user';
cmp_ok $post_user->code, '==', 200,
'200 on POST';
{
my ($self, $params, $env, @noextra) = @{eval $post_user->content or die $@};
is scalar(@noextra), 0, 'No extra stuff';
is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
is ref($params), 'HASH', 'Got POST hashref';
is $params->{id}, 99, 'got expected value for id';
is ref($env), 'HASH', 'Got hashref';
}
|