Mangle the request class mangling stuff. It's still fugly, but it's no longer totally...
[catagits/Catalyst-Action-REST.git] / t / catalyst-request-rest.t
CommitLineData
9a76221e 1use strict;
2use warnings;
149182b3 3use Test::More;
9a76221e 4use FindBin;
5132f5e4 5use lib ( "$FindBin::Bin/../lib", "$FindBin::Bin/../t/lib" );
9a76221e 6
7use Catalyst::Request::REST;
8use HTTP::Headers;
9
10{
11 my $request = Catalyst::Request::REST->new;
12 $request->{_context} = 'MockContext';
13 $request->headers( HTTP::Headers->new );
14 $request->parameters( {} );
15 $request->method('GET');
16 $request->content_type('text/foobar');
17
18 is_deeply( $request->accepted_content_types, [ 'text/foobar' ],
19 'content-type set in request headers is found' );
20 is( $request->preferred_content_type, 'text/foobar',
21 'preferred content type is text/foobar' );
22 ok( ! $request->accept_only, 'accept_only is false' );
23 ok( $request->accepts('text/foobar'), 'accepts text/foobar' );
24 ok( ! $request->accepts('text/html'), 'does not accept text/html' );
25}
26
27{
28 my $request = Catalyst::Request::REST->new;
29 $request->{_context} = 'MockContext';
30 $request->headers( HTTP::Headers->new );
31 $request->parameters( { 'content-type' => 'text/fudge' } );
32 $request->method('GET');
33 $request->content_type('text/foobar');
34
35 is_deeply( $request->accepted_content_types, [ 'text/foobar', 'text/fudge' ],
36 'content-type set in request headers and type in parameters is found' );
37 is( $request->preferred_content_type, 'text/foobar',
38 'preferred content type is text/foobar' );
39 ok( ! $request->accept_only, 'accept_only is false' );
40 ok( $request->accepts('text/foobar'), 'accepts text/foobar' );
41 ok( $request->accepts('text/fudge'), 'accepts text/fudge' );
42 ok( ! $request->accepts('text/html'), 'does not accept text/html' );
43}
44
45{
46 my $request = Catalyst::Request::REST->new;
47 $request->{_context} = 'MockContext';
48 $request->headers( HTTP::Headers->new );
49 $request->parameters( { 'content-type' => 'text/fudge' } );
50 $request->method('POST');
51 $request->content_type('text/foobar');
52
53 ok( ! $request->accepts('text/fudge'), 'content type in parameters is ignored for POST' );
54}
55
56{
57 my $request = Catalyst::Request::REST->new;
58 $request->{_context} = 'MockContext';
59 $request->headers( HTTP::Headers->new );
60 $request->parameters( {} );
61 $request->method('GET');
62 $request->headers->header(
63 'Accept' =>
64 # From Firefox 2.0 when it requests an html page
65 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
66 );
67
68 is_deeply( $request->accepted_content_types,
69 [ qw( text/xml application/xml application/xhtml+xml
70 image/png
71 text/html
72 text/plain
73 */*
74 ) ],
75 'accept header is parsed properly' );
76 is( $request->preferred_content_type, 'text/xml',
77 'preferred content type is text/xml' );
78 ok( $request->accept_only, 'accept_only is true' );
79 ok( $request->accepts('text/html'), 'accepts text/html' );
80 ok( $request->accepts('image/png'), 'accepts image/png' );
81 ok( ! $request->accepts('image/svg'), 'does not accept image/svg' );
82}
83
84{
85 my $request = Catalyst::Request::REST->new;
86 $request->{_context} = 'MockContext';
87 $request->headers( HTTP::Headers->new );
88 $request->parameters( {} );
89 $request->method('GET');
2f7533ed 90 $request->content_type('application/json');
9a76221e 91 $request->headers->header(
92 'Accept' =>
93 # From Firefox 2.0 when it requests an html page
94 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
95 );
96
97 is_deeply( $request->accepted_content_types,
2f7533ed 98 [ qw( application/json
9a76221e 99 text/xml application/xml application/xhtml+xml
100 image/png
101 text/html
102 text/plain
103 */*
104 ) ],
105 'accept header is parsed properly, and content-type header has precedence over accept' );
106 ok( ! $request->accept_only, 'accept_only is false' );
107}
108
109{
110 my $request = Catalyst::Request::REST->new;
111 $request->{_context} = 'MockContext';
112 $request->headers( HTTP::Headers->new );
113 $request->parameters( {} );
114 $request->method('GET');
d6fb033c 115 $request->content_type('application/json');
116 $request->headers->header(
117 'Accept' =>
118 # From Firefox 2.0 when it requests an html page
119 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
120 );
121
122 is_deeply( $request->accepted_content_types,
123 [ qw( application/json
124 text/xml application/xml application/xhtml+xml
125 image/png
126 text/html
127 text/plain
128 */*
129 ) ],
130 'accept header is parsed properly, and content-type header has precedence over accept' );
131 ok( ! $request->accept_only, 'accept_only is false' );
132}
133
134{
135 my $request = Catalyst::Request::REST->new;
136 $request->{_context} = 'MockContext';
137 $request->headers( HTTP::Headers->new );
138 $request->parameters( {} );
139 $request->method('GET');
9a76221e 140 $request->content_type('text/x-json');
141 $request->headers->header(
142 'Accept' => 'text/plain,text/x-json',
143 );
144
145 is_deeply( $request->accepted_content_types,
146 [ qw( text/x-json
147 text/plain
148 ) ],
149 'each type appears only once' );
150}
151
d6fb033c 152{
153 my $request = Catalyst::Request::REST->new;
154 $request->{_context} = 'MockContext';
155 $request->headers( HTTP::Headers->new );
156 $request->parameters( {} );
157 $request->method('GET');
158 $request->content_type('application/json');
159 $request->headers->header(
160 'Accept' => 'text/plain,application/json',
161 );
162
163 is_deeply( $request->accepted_content_types,
164 [ qw( application/json
165 text/plain
166 ) ],
167 'each type appears only once' );
168}
9a76221e 169
5132f5e4 170{
a56ba22a 171 local %ENV=%ENV;
172 $ENV{CATALYST_DEBUG} = 0;
5132f5e4 173 my $test = 'Test::Catalyst::Action::REST';
174 use_ok $test;
175 is($test->request_class, 'Catalyst::Request::REST',
176 'Request::REST took over for Request');
177
149182b3 178 my $meta = Moose::Meta::Class->create_anon_class(
179 superclasses => ['Catalyst::Request'],
180 );
181 $meta->add_method('__random_method' => sub { 42 });
182
183 $test->request_class($meta->name);
184 # FIXME - setup_finished(0) is evil!
ff3c00d9 185 eval { $test->setup_finished(0); $test->setup };
149182b3 186 ok !$@, 'Can setup again';
187 isnt $test->request_class, $meta->name, 'Different request class';
188 ok $test->request_class->can('__random_method'), 'Is right class';
189 ok $test->request_class->can('data'), 'Also smells like REST subclass';
5132f5e4 190
191 {
192 package My::Request;
193 use base 'Catalyst::Request::REST';
194 }
195 $test->request_class('My::Request');
ff3c00d9 196 eval { $test->setup_finished(0); $test->setup };
5132f5e4 197 is $@, '', 'no error from Request::REST subclass';
198}
199
149182b3 200done_testing;
201
9a76221e 202package MockContext;
203
204sub prepare_body { }