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