2226793dad96cf9da0ed5ad60286be8e9db3ae06
[catagits/Catalyst-Action-REST.git] / t / catalyst-request-rest.t
1 use strict;
2 use warnings;
3 use Test::More tests => 28;
4 use FindBin;
5 use lib ( "$FindBin::Bin/../lib", "$FindBin::Bin/../t/lib" );
6
7 use Catalyst::Request::REST;
8 use 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');
90     $request->content_type('application/json');
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,
98                [ qw( application/json
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');
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');
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
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 }
169
170 {
171   local %ENV=%ENV;
172   $ENV{CATALYST_DEBUG} = 0;
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
178   $test->request_class('Some::Other::Class');
179   eval { $test->setup_finished(0); $test->setup };
180   like $@, qr/$test has a custom request class Some::Other::Class/;
181
182   {
183     package My::Request;
184     use base 'Catalyst::Request::REST';
185   }
186   $test->request_class('My::Request');
187   eval { $test->setup_finished(0); $test->setup };
188   is $@, '', 'no error from Request::REST subclass';
189 }
190
191 package MockContext;
192
193 sub prepare_body { }