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