typo correction
[catagits/Web-Simple.git] / t / sub-dispatch-args.t
CommitLineData
4ed4fb42 1use strict;
2use warnings FATAL => 'all';
3
4use Data::Dump qw(dump);
5use Test::More (
6 eval { require HTTP::Request::AsCGI }
7 ? 'no_plan'
8 : (skip_all => 'No HTTP::Request::AsCGI')
9);
10
11{
12 use Web::Simple 't::Web::Simple::SubDispatchArgs';
13 package t::Web::Simple::SubDispatchArgs;
14
15 sub dispatch_request {
16 sub (/) {
6e42ffde 17 $_[0]->show_landing(@_);
4ed4fb42 18 },
19 sub(/...) {
20 sub (GET + /user) {
6e42ffde 21 $_[0]->show_users(@_);
4ed4fb42 22 },
23 sub (/user/*) {
24 sub (GET) {
6e42ffde 25 $_[0]->show_user(@_);
4ed4fb42 26 },
27 sub (POST + %:id=&:@roles~) {
6e42ffde 28 $_[0]->process_post(@_);
4ed4fb42 29 }
30 },
31 }
32 };
33
34 sub show_landing {
35 my ($self, @args) = @_;
36 return [
37 200, ['Content-Type' => 'application/perl' ],
38 [Data::Dump::dump @args],
39 ];
40 }
41 sub show_users {
42 my ($self, @args) = @_;
43 return [
44 200, ['Content-Type' => 'application/perl' ],
45 [Data::Dump::dump @args],
46 ];
47 }
48 sub show_user {
49 my ($self, @args) = @_;
50 return [
51 200, ['Content-Type' => 'application/perl' ],
52 [Data::Dump::dump @args],
53 ];
54 }
55 sub process_post {
56 my ($self, @args) = @_;
57 return [
58 200, ['Content-Type' => 'application/perl' ],
59 [Data::Dump::dump @args],
60 ];
61 }
62}
63
64ok my $app = t::Web::Simple::SubDispatchArgs->new,
65 'made app';
66
67sub run_request {
68 my @args = (shift, SCRIPT_NAME=> $0);
69 my $c = HTTP::Request::AsCGI->new(@args)->setup;
70 $app->run;
71 $c->restore;
72 return $c->response;
73}
74
75use HTTP::Request::Common qw(GET POST);
76
77ok my $get_landing = run_request(GET 'http://localhost/' ),
78 'got landing';
79
80cmp_ok $get_landing->code, '==', 200,
81 '200 on GET';
82
83{
84 my ($self, $env, @noextra) = eval $get_landing->content;
85 is scalar(@noextra), 0, 'No extra stuff';
86 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
87 is ref($env), 'HASH', 'Got hashref';
88 is $env->{SCRIPT_NAME}, $0, 'correct scriptname';
89}
90
91ok my $get_users = run_request(GET 'http://localhost/user'),
92 'got user';
93
94cmp_ok $get_users->code, '==', 200,
95 '200 on GET';
96
97{
98 my ($self, $env, @noextra) = eval $get_users->content;
99 is scalar(@noextra), 0, 'No extra stuff';
100 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
101 is ref($env), 'HASH', 'Got hashref';
102 is $env->{SCRIPT_NAME}, $0, 'correct scriptname';
103}
104
105ok my $get_user = run_request(GET 'http://localhost/user/42'),
106 'got user';
107
108cmp_ok $get_user->code, '==', 200,
109 '200 on GET';
110
111{
112 my ($self, $env, @noextra) = eval $get_user->content;
113 is scalar(@noextra), 0, 'No extra stuff';
114 is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
115 is ref($env), 'HASH', 'Got hashref';
116 is $env->{SCRIPT_NAME}, $0, 'correct scriptname';
117}
118
119ok my $post_user = run_request(POST 'http://localhost/user/42', [id => '99'] ),
120 'post user';
121
122cmp_ok $post_user->code, '==', 200,
123 '200 on POST';
124
125{
126 my ($self, $params, $env, @noextra) = eval $post_user->content;
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';
132 is $env->{SCRIPT_NAME}, $0, 'correct scriptname';
133}
134