Commit | Line | Data |
456dc2bb |
1 | use strictures 1; |
2 | use Test::More; |
3 | |
4 | { |
5 | package t::Web::Simple::HTTPMethods; |
6 | |
7 | use Web::Simple; |
8 | use Web::Dispatch::HTTPMethods; |
9 | |
10 | sub as_text { |
11 | [200, ['Content-Type' => 'text/plain'], |
12 | [$_[0]->{REQUEST_METHOD}, $_[0]->{REQUEST_URI}] ] |
13 | } |
14 | |
15 | sub dispatch_request { |
16 | sub (/get) { |
17 | GET { as_text(pop) } |
18 | }, |
19 | sub (/get-head-options) { |
20 | GET { as_text(pop) } |
21 | HEAD { [204,[],[]] } |
22 | OPTIONS { [204,[],[]] }, |
23 | }, |
24 | sub (/get-post-put) { |
25 | GET { as_text(pop) } |
26 | POST { as_text(pop) } |
27 | PUT { as_text(pop) } |
28 | }, |
29 | } |
30 | } |
31 | |
32 | ok my $app = t::Web::Simple::HTTPMethods->new, |
33 | 'made app'; |
34 | |
35 | for my $uri ('http://localhost/get-post-put') { |
36 | |
37 | ## Check allowed methods and responses |
38 | for(ok my $res = $app->run_test_request(GET => $uri)) { |
39 | is $res->content, 'GET/get-post-put'; |
40 | } |
41 | |
42 | for(ok my $res = $app->run_test_request(POST => $uri)) { |
43 | is $res->content, 'POST/get-post-put'; |
44 | } |
45 | |
46 | for(ok my $res = $app->run_test_request(PUT => $uri)) { |
47 | is $res->content, 'PUT/get-post-put'; |
48 | } |
49 | |
50 | ## Since GET is allowed, check for implict HEAD |
51 | for(ok my $head = $app->run_test_request(HEAD => $uri)) { |
52 | is $head->code, 200; |
53 | is $head->content, ''; |
54 | } |
55 | |
56 | ## Check the implicit support for OPTIONS |
57 | for(ok my $options = $app->run_test_request(OPTIONS => $uri)) { |
58 | is $options->code, 200; |
59 | is $options->content, ''; |
60 | is $options->header('Allow'), 'GET,HEAD,POST,PUT,OPTIONS'; |
61 | } |
62 | |
63 | ## Check implicitly added not allowed |
64 | for(ok my $not_allowed = $app->run_test_request(DELETE => $uri)) { |
65 | is $not_allowed->code, 405; |
66 | is $not_allowed->content, 'Method Not Allowed'; |
67 | is $not_allowed->header('Allow'), 'GET,HEAD,POST,PUT,OPTIONS'; |
68 | } |
69 | |
70 | } |
71 | |
72 | for my $uri ('http://localhost/get-head-options') { |
73 | |
74 | ## Check allowed methods and responses |
75 | for(ok my $res = $app->run_test_request(GET => $uri)) { |
76 | is $res->content, 'GET/get-head-options'; |
77 | } |
78 | |
79 | for(ok my $head = $app->run_test_request(HEAD => $uri)) { |
80 | is $head->code, 204; |
81 | is $head->content, ''; |
82 | } |
83 | |
84 | for(ok my $options = $app->run_test_request(OPTIONS => $uri)) { |
85 | is $options->code, 204; |
86 | is $options->content, ''; |
87 | } |
88 | |
89 | ## Check implicitly added not allowed |
90 | for(ok my $not_allowed = $app->run_test_request(PUT => $uri)) { |
91 | is $not_allowed->code, 405; |
92 | is $not_allowed->content, 'Method Not Allowed'; |
93 | is $not_allowed->header('Allow'), 'GET,HEAD,OPTIONS'; |
94 | } |
95 | |
96 | } |
97 | |
98 | for my $uri ('http://localhost/get') { |
99 | |
100 | ## Check allowed methods and responses |
101 | for(ok my $res = $app->run_test_request(GET => $uri)) { |
102 | is $res->content, 'GET/get'; |
103 | } |
104 | |
105 | ## Check implicitly added not allowed |
106 | for(ok my $not_allowed = $app->run_test_request(PUT => $uri)) { |
107 | is $not_allowed->code, 405; |
108 | is $not_allowed->content, 'Method Not Allowed'; |
109 | is $not_allowed->header('Allow'), 'GET,HEAD,OPTIONS'; |
110 | } |
111 | |
112 | ## Since GET is allowed, check for implict HEAD |
113 | for(ok my $head = $app->run_test_request(HEAD => $uri)) { |
114 | is $head->code, 200; |
115 | is $head->content, ''; |
116 | } |
117 | |
118 | } |
119 | |
120 | done_testing; |