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