convert to Distar
[catagits/Catalyst-Runtime.git] / t / dispatch_on_scheme.t
CommitLineData
342d2169 1use warnings;
2use strict;
3use Test::More;
4use HTTP::Request::Common;
5
6# Test cases for dispatching on URI Scheme
7
8{
9 package MyApp::Controller::Root;
10 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
11
12 use base 'Catalyst::Controller';
13
14 sub is_http :Path(scheme) Scheme(http) Args(0) {
15 my ($self, $c) = @_;
16 Test::More::is $c->action->scheme, 'http';
17 $c->response->body("is_http");
18 }
19
20 sub is_https :Path(scheme) Scheme(https) Args(0) {
21 my ($self, $c) = @_;
22 Test::More::is $c->action->scheme, 'https';
23 $c->response->body("is_https");
24 }
25
26 sub base :Chained('/') CaptureArgs(0) { }
27
28 sub is_http_chain :GET Chained('base') PathPart(scheme) Scheme(http) Args(0) {
29 my ($self, $c) = @_;
30 Test::More::is $c->action->scheme, 'http';
31 $c->response->body("base/is_http");
32 }
33
34 sub is_https_chain :Chained('base') PathPart(scheme) Scheme(https) Args(0) {
35 my ($self, $c) = @_;
36 Test::More::is $c->action->scheme, 'https';
37 $c->response->body("base/is_https");
38 }
39
40 sub uri_for1 :Chained('base') Scheme(https) Args(0) {
41 my ($self, $c) = @_;
42 Test::More::is $c->action->scheme, 'https';
43 $c->response->body($c->uri_for($c->action)->as_string);
44 }
45
46 sub uri_for2 :Chained('base') Scheme(https) Args(0) {
47 my ($self, $c) = @_;
48 Test::More::is $c->action->scheme, 'https';
49 $c->response->body($c->uri_for($self->action_for('is_http'))->as_string);
50 }
51
52 sub uri_for3 :Chained('base') Scheme(http) Args(0) {
53 my ($self, $c) = @_;
54 Test::More::is $c->action->scheme, 'http';
55 $c->response->body($c->uri_for($self->action_for('endpoint'))->as_string);
56 }
57
58 sub base2 :Chained('/') CaptureArgs(0) { }
59 sub link :Chained(base2) Scheme(https) CaptureArgs(0) { }
60 sub endpoint :Chained(link) Args(0) {
61 my ($self, $c) = @_;
62 Test::More::is $c->action->scheme, 'https';
63 $c->response->body("end");
64 }
65
66
67 package MyApp;
68 use Catalyst;
69
70 Test::More::ok(MyApp->setup, 'setup app');
71}
72
73use Catalyst::Test 'MyApp';
74
75{
76 my $res = request "/root/scheme";
77 is $res->code, 200, 'OK';
78 is $res->content, 'is_http', 'correct body';
79}
80
81{
82 my $res = request "https://localhost/root/scheme";
83 is $res->code, 200, 'OK';
84 is $res->content, 'is_https', 'correct body';
85}
86
87{
88 my $res = request "/base/scheme";
89 is $res->code, 200, 'OK';
90 is $res->content, 'base/is_http', 'correct body';
91}
92
93{
94 my $res = request "https://localhost/base/scheme";
95 is $res->code, 200, 'OK';
96 is $res->content, 'base/is_https', 'correct body';
97}
98
99{
100 my $res = request "https://localhost/base/uri_for1";
101 is $res->code, 200, 'OK';
102 is $res->content, 'https://localhost/base/uri_for1', 'correct body';
103}
104
105{
106 my $res = request "https://localhost/base/uri_for2";
107 is $res->code, 200, 'OK';
108 is $res->content, 'http://localhost/root/scheme', 'correct body';
109}
110
111{
112 my $res = request "/base/uri_for3";
113 is $res->code, 200, 'OK';
114 is $res->content, 'https://localhost/base2/link/endpoint', 'correct body';
115}
116
117{
118 my $res = request "https://localhost/base2/link/endpoint";
119 is $res->code, 200, 'OK';
120 is $res->content, 'end', 'correct body';
121}
122
123done_testing;