Web::Simple::Role
[catagits/Web-Simple.git] / t / sub-dispatch-args.t
CommitLineData
4ed4fb42 1use strict;
2use warnings FATAL => 'all';
3
69aaa28a 4use Data::Dumper::Concise;
30e2c525 5use Test::More 'no_plan';
6use Plack::Test;
4ed4fb42 7
8{
9 use Web::Simple 't::Web::Simple::SubDispatchArgs';
10 package t::Web::Simple::SubDispatchArgs;
11
6a4808bf 12 has 'attr' => (is=>'ro');
13
4ed4fb42 14 sub dispatch_request {
48cab33d 15 my $self = shift;
4ed4fb42 16 sub (/) {
48cab33d 17 $self->show_landing(@_);
4ed4fb42 18 },
19 sub(/...) {
69aaa28a 20 q(GET + /user) => sub {
48cab33d 21 $self->show_users(@_);
4ed4fb42 22 },
23 sub (/user/*) {
24 sub (GET) {
48cab33d 25 $self->show_user(@_);
4ed4fb42 26 },
27 sub (POST + %:id=&:@roles~) {
48cab33d 28 $self->process_post(@_);
4ed4fb42 29 }
30 },
31 }
32 };
33
34 sub show_landing {
35 my ($self, @args) = @_;
69aaa28a 36 local $self->{_dispatcher};
d96756e8 37 local $args[-1]->{'Web::Dispatch.original_env'};
4ed4fb42 38 return [
39 200, ['Content-Type' => 'application/perl' ],
69aaa28a 40 [::Dumper \@args],
4ed4fb42 41 ];
42 }
43 sub show_users {
44 my ($self, @args) = @_;
69aaa28a 45 local $self->{_dispatcher};
d96756e8 46 local $args[-1]->{'Web::Dispatch.original_env'};
4ed4fb42 47 return [
48 200, ['Content-Type' => 'application/perl' ],
69aaa28a 49 [::Dumper \@args],
4ed4fb42 50 ];
51 }
52 sub show_user {
53 my ($self, @args) = @_;
69aaa28a 54 local $self->{_dispatcher};
d96756e8 55 local $args[-1]->{'Web::Dispatch.original_env'};
4ed4fb42 56 return [
57 200, ['Content-Type' => 'application/perl' ],
69aaa28a 58 [::Dumper \@args],
4ed4fb42 59 ];
60 }
61 sub process_post {
62 my ($self, @args) = @_;
69aaa28a 63 local $self->{_dispatcher};
d96756e8 64 local $args[-1]->{'Web::Dispatch.original_env'};
4ed4fb42 65 return [
66 200, ['Content-Type' => 'application/perl' ],
69aaa28a 67 [::Dumper \@args],
4ed4fb42 68 ];
69 }
70}
71
72ok my $app = t::Web::Simple::SubDispatchArgs->new,
73 'made app';
74
1bba6f88 75sub run_request { $app->run_test_request(@_); }
4ed4fb42 76
1bba6f88 77ok my $get_landing = run_request(GET => 'http://localhost/' ),
4ed4fb42 78 'got landing';
79
9c7c20f6 80cmp_ok $get_landing->code, '==', 200,
4ed4fb42 81 '200 on GET';
82
30e2c525 83no strict 'refs';
84
4ed4fb42 85{
30e2c525 86 my ($self, $env, @noextra) = @{eval($get_landing->content)||[]};
69aaa28a 87 die $@ if $@;
4ed4fb42 88 is scalar(@noextra), 0, 'No extra stuff';
89 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
90 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 91}
92
1bba6f88 93ok my $get_users = run_request(GET => 'http://localhost/user'),
4ed4fb42 94 'got user';
95
9c7c20f6 96cmp_ok $get_users->code, '==', 200,
4ed4fb42 97 '200 on GET';
98
99{
69aaa28a 100 my ($self, $env, @noextra) = @{eval $get_users->content};
4ed4fb42 101 is scalar(@noextra), 0, 'No extra stuff';
102 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
103 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 104}
105
1bba6f88 106ok my $get_user = run_request(GET => 'http://localhost/user/42'),
4ed4fb42 107 'got user';
108
9c7c20f6 109cmp_ok $get_user->code, '==', 200,
4ed4fb42 110 '200 on GET';
111
112{
69aaa28a 113 my ($self, $env, @noextra) = @{eval $get_user->content};
4ed4fb42 114 is scalar(@noextra), 0, 'No extra stuff';
115 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
116 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 117}
118
1bba6f88 119ok my $post_user = run_request(POST => 'http://localhost/user/42', [id => '99'] ),
4ed4fb42 120 'post user';
121
9c7c20f6 122cmp_ok $post_user->code, '==', 200,
4ed4fb42 123 '200 on POST';
124
125{
69aaa28a 126 my ($self, $params, $env, @noextra) = @{eval $post_user->content};
4ed4fb42 127 is scalar(@noextra), 0, 'No extra stuff';
128 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
129 is ref($params), 'HASH', 'Got POST hashref';
130 is $params->{id}, 99, 'got expected value for id';
131 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 132}