7 use Catalyst::Request::REST::ForBrowsers;
8 use Catalyst::TraitFor::Request::REST::ForBrowsers;
9 use Moose::Meta::Class;
13 my $anon_class = Moose::Meta::Class->create_anon_class(
14 superclasses => ['Catalyst::Request'],
15 roles => ['Catalyst::TraitFor::Request::REST::ForBrowsers'],
19 # We run the tests twice to make sure Catalyst::Request::REST::ForBrowsers is
20 # 100% back-compatible.
21 for my $class ( $anon_class, 'Catalyst::Request::REST::ForBrowsers' ) {
23 for my $method (qw( GET POST PUT DELETE )) {
24 my $req = $class->new(
25 _log => Catalyst::Log->new,
27 $req->method($method);
28 $req->{_context} = 'MockContext';
29 $req->parameters( {} );
32 $req->method(), $method,
33 "$method - not tunneled"
39 for my $method (qw( PUT DELETE )) {
40 my $req = $class->new(
41 _log => Catalyst::Log->new,
44 $req->{_context} = 'MockContext';
45 $req->parameters( { 'x-tunneled-method' => $method } );
48 $req->method(), $method,
49 "$method - tunneled with x-tunneled-method param"
55 for my $method (qw( PUT DELETE )) {
56 my $req = $class->new(
57 _log => Catalyst::Log->new,
60 $req->{_context} = 'MockContext';
61 $req->header( 'x-http-method-override' => $method );
64 $req->method(), $method,
65 "$method - tunneled with x-http-method-override header"
71 for my $method (qw( PUT DELETE )) {
72 my $req = $class->new(
73 _log => Catalyst::Log->new,
76 $req->{_context} = 'MockContext';
77 $req->parameters( { 'x-tunneled-method' => $method } );
80 $req->method(), 'GET',
81 'x-tunneled-method is ignore with a GET'
87 my $req = $class->new(
88 _log => Catalyst::Log->new,
90 $req->{_context} = 'MockContext';
92 $req->parameters( {} );
93 $req->headers( HTTP::Headers->new() );
96 $req->looks_like_browser(),
97 'default is a browser'
102 for my $with (qw( HTTP.Request XMLHttpRequest )) {
103 my $req = $class->new(
104 _log => Catalyst::Log->new,
106 $req->{_context} = 'MockContext';
108 HTTP::Headers->new( 'X-Requested-With' => $with ) );
111 !$req->looks_like_browser(),
112 "not a browser - X-Request-With = $with"
118 my $req = $class->new(
119 _log => Catalyst::Log->new,
121 $req->{_context} = 'MockContext';
123 $req->parameters( { 'content-type' => 'text/json' } );
124 $req->headers( HTTP::Headers->new() );
127 !$req->looks_like_browser(),
128 'forced non-HTML content-type is not a browser'
133 my $req = $class->new(
134 _log => Catalyst::Log->new,
136 $req->{_context} = 'MockContext';
138 $req->parameters( { 'content-type' => 'text/html' } );
139 $req->headers( HTTP::Headers->new() );
142 $req->looks_like_browser(),
143 'forced HTML content-type is not a browser'
148 my $req = $class->new(
149 _log => Catalyst::Log->new,
151 $req->{_context} = 'MockContext';
153 $req->parameters( {} );
155 HTTP::Headers->new( 'Accept' => 'text/xml; q=0.4, */*; q=0.2' ) );
158 $req->looks_like_browser(),
159 'if it accepts */* it is a browser'
164 my $req = $class->new(
165 _log => Catalyst::Log->new,
167 $req->{_context} = 'MockContext';
169 $req->parameters( {} );
172 'Accept' => 'text/html; q=0.4, text/xml; q=0.2'
177 $req->looks_like_browser(),
178 'if it accepts text/html it is a browser'
183 my $req = $class->new(
184 _log => Catalyst::Log->new,
186 $req->{_context} = 'MockContext';
188 $req->parameters( {} );
191 'Accept' => 'application/xhtml+xml; q=0.4, text/xml; q=0.2'
196 $req->looks_like_browser(),
197 'if it accepts application/xhtml+xml it is a browser'
202 my $req = $class->new(
203 _log => Catalyst::Log->new,
205 $req->{_context} = 'MockContext';
207 $req->parameters( {} );
210 'Accept' => 'text/json; q=0.4, text/xml; q=0.2'
215 !$req->looks_like_browser(),
216 'provided an Accept header but does not accept html, is not a browser'