Version 0.98
[catagits/Catalyst-Action-REST.git] / t / catalyst-traitfor-request-rest-forbrowsers.t
CommitLineData
85aa4e18 1use strict;
2use warnings;
3
4use Test::More;
5
6use Catalyst::Request;
7use Catalyst::Request::REST::ForBrowsers;
8use Catalyst::TraitFor::Request::REST::ForBrowsers;
9use Moose::Meta::Class;
10use HTTP::Headers;
ae0db5df 11use Catalyst::Log;
85aa4e18 12
13my $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# We run the tests twice to make sure Catalyst::Request::REST::ForBrowsers is
20# 100% back-compatible.
21for my $class ( $anon_class, 'Catalyst::Request::REST::ForBrowsers' ) {
22 {
23 for my $method (qw( GET POST PUT DELETE )) {
ae0db5df 24 my $req = $class->new(
25 _log => Catalyst::Log->new,
26 );
85aa4e18 27 $req->method($method);
28 $req->{_context} = 'MockContext';
29 $req->parameters( {} );
30
31 is(
32 $req->method(), $method,
33 "$method - not tunneled"
34 );
35 }
36 }
37
38 {
39 for my $method (qw( PUT DELETE )) {
351f252f 40 my $req = $class->new(
41 _log => Catalyst::Log->new,
42 );
85aa4e18 43 $req->method('POST');
44 $req->{_context} = 'MockContext';
45 $req->parameters( { 'x-tunneled-method' => $method } );
46
47 is(
48 $req->method(), $method,
49 "$method - tunneled with x-tunneled-method param"
50 );
51 }
52 }
53
54 {
55 for my $method (qw( PUT DELETE )) {
351f252f 56 my $req = $class->new(
57 _log => Catalyst::Log->new,
58 );
85aa4e18 59 $req->method('POST');
60 $req->{_context} = 'MockContext';
61 $req->header( 'x-http-method-override' => $method );
62
63 is(
64 $req->method(), $method,
65 "$method - tunneled with x-http-method-override header"
66 );
67 }
68 }
69
70 {
71 for my $method (qw( PUT DELETE )) {
351f252f 72 my $req = $class->new(
73 _log => Catalyst::Log->new,
74 );
85aa4e18 75 $req->method('GET');
76 $req->{_context} = 'MockContext';
77 $req->parameters( { 'x-tunneled-method' => $method } );
78
79 is(
80 $req->method(), 'GET',
81 'x-tunneled-method is ignore with a GET'
82 );
83 }
84 }
85
86 {
351f252f 87 my $req = $class->new(
88 _log => Catalyst::Log->new,
89 );
85aa4e18 90 $req->{_context} = 'MockContext';
91 $req->method('GET');
92 $req->parameters( {} );
93 $req->headers( HTTP::Headers->new() );
94
95 ok(
96 $req->looks_like_browser(),
97 'default is a browser'
98 );
99 }
100
101 {
102 for my $with (qw( HTTP.Request XMLHttpRequest )) {
351f252f 103 my $req = $class->new(
104 _log => Catalyst::Log->new,
105 );
85aa4e18 106 $req->{_context} = 'MockContext';
107 $req->headers(
108 HTTP::Headers->new( 'X-Requested-With' => $with ) );
109
110 ok(
111 !$req->looks_like_browser(),
112 "not a browser - X-Request-With = $with"
113 );
114 }
115 }
116
117 {
351f252f 118 my $req = $class->new(
119 _log => Catalyst::Log->new,
120 );
85aa4e18 121 $req->{_context} = 'MockContext';
122 $req->method('GET');
123 $req->parameters( { 'content-type' => 'text/json' } );
124 $req->headers( HTTP::Headers->new() );
125
126 ok(
127 !$req->looks_like_browser(),
128 'forced non-HTML content-type is not a browser'
129 );
130 }
131
132 {
351f252f 133 my $req = $class->new(
134 _log => Catalyst::Log->new,
135 );
85aa4e18 136 $req->{_context} = 'MockContext';
137 $req->method('GET');
138 $req->parameters( { 'content-type' => 'text/html' } );
139 $req->headers( HTTP::Headers->new() );
140
141 ok(
142 $req->looks_like_browser(),
143 'forced HTML content-type is not a browser'
144 );
145 }
146
147 {
351f252f 148 my $req = $class->new(
149 _log => Catalyst::Log->new,
150 );
85aa4e18 151 $req->{_context} = 'MockContext';
152 $req->method('GET');
153 $req->parameters( {} );
154 $req->headers(
155 HTTP::Headers->new( 'Accept' => 'text/xml; q=0.4, */*; q=0.2' ) );
156
157 ok(
158 $req->looks_like_browser(),
159 'if it accepts */* it is a browser'
160 );
161 }
162
163 {
351f252f 164 my $req = $class->new(
165 _log => Catalyst::Log->new,
166 );
85aa4e18 167 $req->{_context} = 'MockContext';
168 $req->method('GET');
169 $req->parameters( {} );
170 $req->headers(
171 HTTP::Headers->new(
172 'Accept' => 'text/html; q=0.4, text/xml; q=0.2'
173 )
174 );
175
176 ok(
177 $req->looks_like_browser(),
178 'if it accepts text/html it is a browser'
179 );
180 }
181
182 {
351f252f 183 my $req = $class->new(
184 _log => Catalyst::Log->new,
185 );
85aa4e18 186 $req->{_context} = 'MockContext';
187 $req->method('GET');
188 $req->parameters( {} );
189 $req->headers(
190 HTTP::Headers->new(
191 'Accept' => 'application/xhtml+xml; q=0.4, text/xml; q=0.2'
192 )
193 );
194
195 ok(
196 $req->looks_like_browser(),
197 'if it accepts application/xhtml+xml it is a browser'
198 );
199 }
200
201 {
351f252f 202 my $req = $class->new(
203 _log => Catalyst::Log->new,
204 );
85aa4e18 205 $req->{_context} = 'MockContext';
206 $req->method('GET');
207 $req->parameters( {} );
208 $req->headers(
209 HTTP::Headers->new(
210 'Accept' => 'text/json; q=0.4, text/xml; q=0.2'
211 )
212 );
213
214 ok(
215 !$req->looks_like_browser(),
216 'provided an Accept header but does not accept html, is not a browser'
217 );
218 }
219}
220
221done_testing;
222
223package MockContext;
224
225sub prepare_body { }