Test fix
[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 )) {
40 my $req = $class->new();
41 $req->method('POST');
42 $req->{_context} = 'MockContext';
43 $req->parameters( { 'x-tunneled-method' => $method } );
44
45 is(
46 $req->method(), $method,
47 "$method - tunneled with x-tunneled-method param"
48 );
49 }
50 }
51
52 {
53 for my $method (qw( PUT DELETE )) {
54 my $req = $class->new();
55 $req->method('POST');
56 $req->{_context} = 'MockContext';
57 $req->header( 'x-http-method-override' => $method );
58
59 is(
60 $req->method(), $method,
61 "$method - tunneled with x-http-method-override header"
62 );
63 }
64 }
65
66 {
67 for my $method (qw( PUT DELETE )) {
68 my $req = $class->new();
69 $req->method('GET');
70 $req->{_context} = 'MockContext';
71 $req->parameters( { 'x-tunneled-method' => $method } );
72
73 is(
74 $req->method(), 'GET',
75 'x-tunneled-method is ignore with a GET'
76 );
77 }
78 }
79
80 {
81 my $req = $class->new();
82 $req->{_context} = 'MockContext';
83 $req->method('GET');
84 $req->parameters( {} );
85 $req->headers( HTTP::Headers->new() );
86
87 ok(
88 $req->looks_like_browser(),
89 'default is a browser'
90 );
91 }
92
93 {
94 for my $with (qw( HTTP.Request XMLHttpRequest )) {
95 my $req = $class->new();
96 $req->{_context} = 'MockContext';
97 $req->headers(
98 HTTP::Headers->new( 'X-Requested-With' => $with ) );
99
100 ok(
101 !$req->looks_like_browser(),
102 "not a browser - X-Request-With = $with"
103 );
104 }
105 }
106
107 {
108 my $req = $class->new();
109 $req->{_context} = 'MockContext';
110 $req->method('GET');
111 $req->parameters( { 'content-type' => 'text/json' } );
112 $req->headers( HTTP::Headers->new() );
113
114 ok(
115 !$req->looks_like_browser(),
116 'forced non-HTML content-type is not a browser'
117 );
118 }
119
120 {
121 my $req = $class->new();
122 $req->{_context} = 'MockContext';
123 $req->method('GET');
124 $req->parameters( { 'content-type' => 'text/html' } );
125 $req->headers( HTTP::Headers->new() );
126
127 ok(
128 $req->looks_like_browser(),
129 'forced HTML content-type is not a browser'
130 );
131 }
132
133 {
134 my $req = $class->new();
135 $req->{_context} = 'MockContext';
136 $req->method('GET');
137 $req->parameters( {} );
138 $req->headers(
139 HTTP::Headers->new( 'Accept' => 'text/xml; q=0.4, */*; q=0.2' ) );
140
141 ok(
142 $req->looks_like_browser(),
143 'if it accepts */* it is a browser'
144 );
145 }
146
147 {
148 my $req = $class->new();
149 $req->{_context} = 'MockContext';
150 $req->method('GET');
151 $req->parameters( {} );
152 $req->headers(
153 HTTP::Headers->new(
154 'Accept' => 'text/html; q=0.4, text/xml; q=0.2'
155 )
156 );
157
158 ok(
159 $req->looks_like_browser(),
160 'if it accepts text/html it is a browser'
161 );
162 }
163
164 {
165 my $req = $class->new();
166 $req->{_context} = 'MockContext';
167 $req->method('GET');
168 $req->parameters( {} );
169 $req->headers(
170 HTTP::Headers->new(
171 'Accept' => 'application/xhtml+xml; q=0.4, text/xml; q=0.2'
172 )
173 );
174
175 ok(
176 $req->looks_like_browser(),
177 'if it accepts application/xhtml+xml it is a browser'
178 );
179 }
180
181 {
182 my $req = $class->new();
183 $req->{_context} = 'MockContext';
184 $req->method('GET');
185 $req->parameters( {} );
186 $req->headers(
187 HTTP::Headers->new(
188 'Accept' => 'text/json; q=0.4, text/xml; q=0.2'
189 )
190 );
191
192 ok(
193 !$req->looks_like_browser(),
194 'provided an Accept header but does not accept html, is not a browser'
195 );
196 }
197}
198
199done_testing;
200
201package MockContext;
202
203sub prepare_body { }