3 use Test::More tests => 28;
5 use lib ( "$FindBin::Bin/../lib", "$FindBin::Bin/../t/lib" );
7 use Catalyst::Request::REST;
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');
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' );
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');
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' );
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');
53 ok( ! $request->accepts('text/fudge'), 'content type in parameters is ignored for POST' );
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(
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',
68 is_deeply( $request->accepted_content_types,
69 [ qw( text/xml application/xml application/xhtml+xml
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' );
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(
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',
97 is_deeply( $request->accepted_content_types,
98 [ qw( application/json
99 text/xml application/xml application/xhtml+xml
105 'accept header is parsed properly, and content-type header has precedence over accept' );
106 ok( ! $request->accept_only, 'accept_only is false' );
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(
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',
122 is_deeply( $request->accepted_content_types,
123 [ qw( application/json
124 text/xml application/xml application/xhtml+xml
130 'accept header is parsed properly, and content-type header has precedence over accept' );
131 ok( ! $request->accept_only, 'accept_only is false' );
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',
145 is_deeply( $request->accepted_content_types,
149 'each type appears only once' );
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',
163 is_deeply( $request->accepted_content_types,
164 [ qw( application/json
167 'each type appears only once' );
171 my $test = 'Test::Catalyst::Action::REST';
173 is($test->request_class, 'Catalyst::Request::REST',
174 'Request::REST took over for Request');
176 $test->request_class('Some::Other::Class');
177 eval { $test->setup_finished(0); $test->setup };
178 like $@, qr/$test has a custom request class Some::Other::Class/;
182 use base 'Catalyst::Request::REST';
184 $test->request_class('My::Request');
185 eval { $test->setup_finished(0); $test->setup };
186 is $@, '', 'no error from Request::REST subclass';