TWMC: Add white label (Host header) testing
[catagits/Test-WWW-Mechanize-Catalyst.git] / t / lib / Catty.pm
CommitLineData
6bc86362 1package Catty;
2
3use strict;
4
5#use Catalyst;
db8bd5bb 6use Catalyst;
6bc86362 7use Cwd;
8use MIME::Base64;
db8bd5bb 9use Encode qw//;
6bc86362 10
11our $VERSION = '0.01';
12
13Catty->config(
14 name => 'Catty',
15 root => cwd . '/t/root',
16);
6bc86362 17Catty->setup();
46377765 18Catty->log->levels("fatal");
6bc86362 19
20sub default : Private {
21 my ( $self, $context ) = @_;
22 my $html = html( "Root", "This is the root page" );
23 $context->response->content_type("text/html");
24 $context->response->output($html);
25}
26
27sub hello : Global {
28 my ( $self, $context ) = @_;
db8bd5bb 29 my $str = Encode::encode('utf-8', "\x{263A}"); # ☺
30 my $html = html( "Hello", "Hi there! $str" );
6bc86362 31 $context->response->content_type("text/html; charset=utf-8");
32 $context->response->output($html);
33}
34
35# absolute redirect
36sub hi : Global {
37 my ( $self, $context ) = @_;
38 my $where = $context->uri_for('hello');
39 $context->response->redirect($where);
40 return;
41}
42
43# partial (relative) redirect
44sub greetings : Global {
45 my ( $self, $context ) = @_;
46 $context->response->redirect("hello");
47 return;
48}
49
50# redirect to a redirect
51sub bonjour : Global {
52 my ( $self, $context ) = @_;
53 my $where = $context->uri_for('hi');
54 $context->response->redirect($where);
55 return;
56}
57
58sub check_auth_basic : Global {
59 my ( $self, $context ) = @_;
60
61 my $auth = $context->req->headers->authorization;
62 ($auth) = $auth =~ /Basic\s(.*)/i;
63 $auth = decode_base64($auth);
64
65 if ( $auth eq "user:pass" ) {
66 my $html = html( "Auth", "This is the auth page" );
67 $context->response->content_type("text/html");
68 $context->response->output($html);
69 return $context;
70 } else {
71 my $html = html( "Auth", "Auth Failed!" );
72 $context->response->content_type("text/html");
73 $context->response->output($html);
74 $context->response->status("401");
75 return $context;
76 }
77}
78
79sub die : Global {
80 my ( $self, $context ) = @_;
81 my $html = html( "Die", "This is the die page" );
82 $context->response->content_type("text/html");
83 $context->response->output($html);
84 die "erk!";
85}
86
254eca41 87sub name : Global {
88 my ($self, $c) = @_;
89
90 my $html = html( $c->config->{name}, "This is the die page" );
91 $c->response->content_type("text/html");
92 $c->response->output($html);
93}
94
1f8dbf85 95sub host : Global {
96 my ($self, $c) = @_;
97
98 my $host = $c->req->header('Host') || "<undef>";
99 my $html = html( $c->config->{name}, "Host: $host" );
100 $c->response->content_type("text/html");
101 $c->response->output($html);
102}
103
6bc86362 104sub html {
105 my ( $title, $body ) = @_;
106 return qq{
107<html>
108<head><title>$title</title></head>
109<body>
110$body
111<a href="/hello/">Hello</a>.
112</body></html>
113};
114}
115
116sub gzipped : Global {
117 my ( $self, $c ) = @_;
118
119 # If done properly this test should check the accept-encoding header, but we
120 # control both ends, so just always gzip the response.
121 require Compress::Zlib;
122
123 my $html = html( "Hello", "Hi there! ☺" );
124 $c->response->content_type("text/html; charset=utf-8");
125 $c->response->output( Compress::Zlib::memGzip($html) );
126 $c->response->content_encoding('gzip');
127 $c->response->headers->push_header( 'Vary', 'Accept-Encoding' );
128}
129
1301;
131