Moosify TWMC
[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
6bc86362 95sub html {
96 my ( $title, $body ) = @_;
97 return qq{
98<html>
99<head><title>$title</title></head>
100<body>
101$body
102<a href="/hello/">Hello</a>.
103</body></html>
104};
105}
106
107sub gzipped : Global {
108 my ( $self, $c ) = @_;
109
110 # If done properly this test should check the accept-encoding header, but we
111 # control both ends, so just always gzip the response.
112 require Compress::Zlib;
113
114 my $html = html( "Hello", "Hi there! ☺" );
115 $c->response->content_type("text/html; charset=utf-8");
116 $c->response->output( Compress::Zlib::memGzip($html) );
117 $c->response->content_encoding('gzip');
118 $c->response->headers->push_header( 'Vary', 'Accept-Encoding' );
119}
120
1211;
122