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
71sub run_request {
30e2c525 72 my $request = shift;
73 return test_psgi $app->to_psgi_app, sub { shift->($request) };
4ed4fb42 74}
75
76use HTTP::Request::Common qw(GET POST);
77
78ok my $get_landing = run_request(GET 'http://localhost/' ),
79 'got landing';
80
81cmp_ok $get_landing->code, '==', 200,
82 '200 on GET';
83
30e2c525 84no strict 'refs';
85
4ed4fb42 86{
30e2c525 87 my ($self, $env, @noextra) = @{eval($get_landing->content)||[]};
69aaa28a 88 die $@ if $@;
4ed4fb42 89 is scalar(@noextra), 0, 'No extra stuff';
90 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
91 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 92}
93
94ok my $get_users = run_request(GET 'http://localhost/user'),
95 'got user';
96
97cmp_ok $get_users->code, '==', 200,
98 '200 on GET';
99
100{
69aaa28a 101 my ($self, $env, @noextra) = @{eval $get_users->content};
4ed4fb42 102 is scalar(@noextra), 0, 'No extra stuff';
103 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
104 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 105}
106
107ok my $get_user = run_request(GET 'http://localhost/user/42'),
108 'got user';
109
110cmp_ok $get_user->code, '==', 200,
111 '200 on GET';
112
113{
69aaa28a 114 my ($self, $env, @noextra) = @{eval $get_user->content};
4ed4fb42 115 is scalar(@noextra), 0, 'No extra stuff';
116 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
117 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 118}
119
120ok my $post_user = run_request(POST 'http://localhost/user/42', [id => '99'] ),
121 'post user';
122
123cmp_ok $post_user->code, '==', 200,
124 '200 on POST';
125
126{
69aaa28a 127 my ($self, $params, $env, @noextra) = @{eval $post_user->content};
4ed4fb42 128 is scalar(@noextra), 0, 'No extra stuff';
129 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
130 is ref($params), 'HASH', 'Got POST hashref';
131 is $params->{id}, 99, 'got expected value for id';
132 is ref($env), 'HASH', 'Got hashref';
4ed4fb42 133}
134