donot encode write unless the content type is one of the encodable types
[catagits/Catalyst-Runtime.git] / t / utf_incoming.t
1 use utf8;
2 use warnings;
3 use strict;
4 use Test::More;
5 use HTTP::Request::Common;
6
7 # Test cases for incoming utf8 
8
9 {
10   package MyApp::Controller::Root;
11   $INC{'MyApp/Controller/Root.pm'} = __FILE__;
12
13   use base 'Catalyst::Controller';
14
15   sub heart :Path('♥') {
16     my ($self, $c) = @_;
17     $c->response->content_type('text/html');
18     $c->response->body("<p>This is path-heart action ♥</p>");
19     # We let the content length middleware find the length...
20   }
21
22   sub hat :Path('^') {
23     my ($self, $c) = @_;
24     $c->response->content_type('text/html');
25     $c->response->body("<p>This is path-hat action ^</p>");
26   }
27
28   sub uri_for :Path('uri_for') {
29     my ($self, $c) = @_;
30     $c->response->content_type('text/html');
31     $c->response->body("${\$c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'})}");
32   }
33
34   sub heart_with_arg :Path('a♥') Args(1)  {
35     my ($self, $c, $arg) = @_;
36     $c->response->content_type('text/html');
37     $c->response->body("<p>This is path-heart-arg action $arg</p>");
38     Test::More::is $c->req->args->[0], '♥';
39   }
40
41   sub base :Chained('/') CaptureArgs(0) { }
42     sub link :Chained('base') PathPart('♥') Args(0) {
43       my ($self, $c) = @_;
44       $c->response->content_type('text/html');
45       $c->response->body("<p>This is base-link action ♥</p>");
46     }
47     sub arg :Chained('base') PathPart('♥') Args(1) {
48       my ($self, $c, $arg) = @_;
49       $c->response->content_type('text/html');
50       $c->response->body("<p>This is base-link action ♥ $arg</p>");
51     }
52     sub capture :Chained('base') PathPart('♥') CaptureArgs(1) {
53       my ($self, $c, $arg) = @_;
54       $c->stash(capture=>$arg);
55     }
56       sub argend :Chained('capture') PathPart('♥') Args(1) {
57         my ($self, $c, $arg) = @_;
58         $c->response->content_type('text/html');
59
60         Test::More::is $c->req->args->[0], '♥';
61         Test::More::is $c->req->captures->[0], '♥';
62
63         $c->response->body("<p>This is base-link action ♥ ${\$c->req->args->[0]}</p>");
64
65         # Test to make sure redirect can now take an object (sorry don't have a better place for it
66         # but wanted test coverage.
67         my $location = $c->res->redirect( $c->uri_for($c->controller('Root')->action_for('uri_for')) );
68         Test::More::ok !ref $location; 
69       }
70
71   sub stream_write :Local {
72     my ($self, $c) = @_;
73     $c->response->content_type('text/html');
74     $c->response->write("<p>This is stream_write action ♥</p>");
75   }
76
77   package MyApp;
78   use Catalyst;
79
80   MyApp->config(encoding=>'UTF-8');
81
82   Test::More::ok(MyApp->setup, 'setup app');
83 }
84
85 ok my $psgi = MyApp->psgi_app, 'build psgi app';
86
87 use Catalyst::Test 'MyApp';
88 use Encode 2.21 'decode_utf8', 'encode_utf8';
89
90 {
91   my $res = request "/root/♥";
92
93   is $res->code, 200, 'OK';
94   is decode_utf8($res->content), '<p>This is path-heart action ♥</p>', 'correct body';
95   is $res->content_length, 36, 'correct length';
96 }
97
98 {
99   my $res = request "/root/a♥/♥";
100
101   is $res->code, 200, 'OK';
102   is decode_utf8($res->content), '<p>This is path-heart-arg action ♥</p>', 'correct body';
103   is $res->content_length, 40, 'correct length';
104 }
105
106 {
107   my $res = request "/root/^";
108
109   is $res->code, 200, 'OK';
110   is decode_utf8($res->content), '<p>This is path-hat action ^</p>', 'correct body';
111   is $res->content_length, 32, 'correct length';
112 }
113
114 {
115   my $res = request "/base/♥";
116
117   is $res->code, 200, 'OK';
118   is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
119   is $res->content_length, 35, 'correct length';
120 }
121
122 {
123   my ($res, $c) = ctx_request POST "/base/♥?♥=♥&♥=♥♥", [a=>1, b=>'', '♥'=>'♥', '♥'=>'♥♥'];
124
125   is $res->code, 200, 'OK';
126   is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
127   is $res->content_length, 35, 'correct length';
128   is $c->req->parameters->{'♥'}[0], '♥';
129   is $c->req->query_parameters->{'♥'}[0], '♥';
130   is $c->req->body_parameters->{'♥'}[0], '♥';
131   is $c->req->parameters->{'♥'}[0], '♥';
132   is $c->req->parameters->{a}, 1;
133   is $c->req->body_parameters->{a}, 1;
134 }
135
136 {
137   my ($res, $c) = ctx_request GET "/base/♥?♥♥♥";
138
139   is $res->code, 200, 'OK';
140   is decode_utf8($res->content), '<p>This is base-link action ♥</p>', 'correct body';
141   is $res->content_length, 35, 'correct length';
142   is $c->req->query_keywords, '♥♥♥';
143 }
144
145 {
146   my $res = request "/base/♥/♥";
147
148   is $res->code, 200, 'OK';
149   is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
150   is $res->content_length, 39, 'correct length';
151 }
152
153 {
154   my $res = request "/base/♥/♥/♥/♥";
155
156   is decode_utf8($res->content), '<p>This is base-link action ♥ ♥</p>', 'correct body';
157   is $res->content_length, 39, 'correct length';
158 }
159
160 {
161   my ($res, $c) = ctx_request POST "/base/♥/♥/♥/♥?♥=♥♥", [a=>1, b=>'2', '♥'=>'♥♥'];
162
163   ## Make sure that the urls we generate work the same
164   my $uri_for = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
165   my $uri = $c->req->uri;
166
167   is "$uri", "$uri_for";
168
169   {
170     my ($res, $c) = ctx_request POST "$uri_for", [a=>1, b=>'2', '♥'=>'♥♥'];
171     is $c->req->query_parameters->{'♥'}, '♥♥';
172     is $c->req->body_parameters->{'♥'}, '♥♥';
173     is $c->req->parameters->{'♥'}[0], '♥♥'; #combined with query and body
174   }
175 }
176
177 {
178   my ($res, $c) = ctx_request "/root/uri_for";
179   my $url = $c->uri_for($c->controller('Root')->action_for('argend'), ['♥'], '♥', {'♥'=>'♥♥'});
180
181   is $res->code, 200, 'OK';
182   is decode_utf8($res->content), "$url", 'correct body'; #should do nothing
183   is $res->content, "$url", 'correct body';
184   is $res->content_length, 90, 'correct length';
185 }
186
187 {
188   my $res = request "/root/stream_write";
189
190   is $res->code, 200, 'OK';
191   is decode_utf8($res->content), '<p>This is stream_write action ♥</p>', 'correct body';
192 }
193
194
195 done_testing;