trimmed some trailing spaces
[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};
4ed4fb42 37 return [
38 200, ['Content-Type' => 'application/perl' ],
69aaa28a 39 [::Dumper \@args],
4ed4fb42 40 ];
41 }
42 sub show_users {
43 my ($self, @args) = @_;
69aaa28a 44 local $self->{_dispatcher};
4ed4fb42 45 return [
46 200, ['Content-Type' => 'application/perl' ],
69aaa28a 47 [::Dumper \@args],
4ed4fb42 48 ];
49 }
50 sub show_user {
51 my ($self, @args) = @_;
69aaa28a 52 local $self->{_dispatcher};
4ed4fb42 53 return [
54 200, ['Content-Type' => 'application/perl' ],
69aaa28a 55 [::Dumper \@args],
4ed4fb42 56 ];
57 }
58 sub process_post {
59 my ($self, @args) = @_;
69aaa28a 60 local $self->{_dispatcher};
4ed4fb42 61 return [
62 200, ['Content-Type' => 'application/perl' ],
69aaa28a 63 [::Dumper \@args],
4ed4fb42 64 ];
65 }
66}
67
68ok my $app = t::Web::Simple::SubDispatchArgs->new,
69 'made app';
70
1bba6f88 71sub run_request { $app->run_test_request(@_); }
4ed4fb42 72
1bba6f88 73ok my $get_landing = run_request(GET => 'http://localhost/' ),
4ed4fb42 74 'got landing';
75
9c7c20f6 76cmp_ok $get_landing->code, '==', 200,
4ed4fb42 77 '200 on GET';
78
30e2c525 79no strict 'refs';
80
4ed4fb42 81{
30e2c525 82 my ($self, $env, @noextra) = @{eval($get_landing->content)||[]};
69aaa28a 83 die $@ if $@;
4ed4fb42 84 is scalar(@noextra), 0, 'No extra stuff';
85 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
86 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 87}
88
1bba6f88 89ok my $get_users = run_request(GET => 'http://localhost/user'),
4ed4fb42 90 'got user';
91
9c7c20f6 92cmp_ok $get_users->code, '==', 200,
4ed4fb42 93 '200 on GET';
94
95{
69aaa28a 96 my ($self, $env, @noextra) = @{eval $get_users->content};
4ed4fb42 97 is scalar(@noextra), 0, 'No extra stuff';
98 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
99 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 100}
101
1bba6f88 102ok my $get_user = run_request(GET => 'http://localhost/user/42'),
4ed4fb42 103 'got user';
104
9c7c20f6 105cmp_ok $get_user->code, '==', 200,
4ed4fb42 106 '200 on GET';
107
108{
69aaa28a 109 my ($self, $env, @noextra) = @{eval $get_user->content};
4ed4fb42 110 is scalar(@noextra), 0, 'No extra stuff';
111 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
112 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 113}
114
1bba6f88 115ok my $post_user = run_request(POST => 'http://localhost/user/42', [id => '99'] ),
4ed4fb42 116 'post user';
117
9c7c20f6 118cmp_ok $post_user->code, '==', 200,
4ed4fb42 119 '200 on POST';
120
121{
69aaa28a 122 my ($self, $params, $env, @noextra) = @{eval $post_user->content};
4ed4fb42 123 is scalar(@noextra), 0, 'No extra stuff';
124 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
125 is ref($params), 'HASH', 'Got POST hashref';
126 is $params->{id}, 99, 'got expected value for id';
127 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 128}