3fd4c092e504ad03935baee527000f33168153bd
[catagits/Catalyst-Action-REST.git] / t / catalyst-request-rest.t
1 use strict;
2 use warnings;
3 use Test::More tests => 21;
4 use FindBin;
5 use lib ( "$FindBin::Bin/../lib" );
6
7 use Catalyst::Request::REST;
8 use HTTP::Headers;
9
10 {
11     my $request = Catalyst::Request::REST->new;
12     $request->{_context} = 'MockContext';
13     $request->headers( HTTP::Headers->new );
14     $request->parameters( {} );
15     $request->method('GET');
16     $request->content_type('text/foobar');
17
18     is_deeply( $request->accepted_content_types, [ 'text/foobar' ],
19                'content-type set in request headers is found' );
20     is( $request->preferred_content_type, 'text/foobar',
21         'preferred content type is text/foobar' );
22     ok( ! $request->accept_only, 'accept_only is false' );
23     ok( $request->accepts('text/foobar'), 'accepts text/foobar' );
24     ok( ! $request->accepts('text/html'), 'does not accept text/html' );
25 }
26
27 {
28     my $request = Catalyst::Request::REST->new;
29     $request->{_context} = 'MockContext';
30     $request->headers( HTTP::Headers->new );
31     $request->parameters( { 'content-type' => 'text/fudge' } );
32     $request->method('GET');
33     $request->content_type('text/foobar');
34
35     is_deeply( $request->accepted_content_types, [ 'text/foobar', 'text/fudge' ],
36                'content-type set in request headers and type in parameters is found' );
37     is( $request->preferred_content_type, 'text/foobar',
38         'preferred content type is text/foobar' );
39     ok( ! $request->accept_only, 'accept_only is false' );
40     ok( $request->accepts('text/foobar'), 'accepts text/foobar' );
41     ok( $request->accepts('text/fudge'), 'accepts text/fudge' );
42     ok( ! $request->accepts('text/html'), 'does not accept text/html' );
43 }
44
45 {
46     my $request = Catalyst::Request::REST->new;
47     $request->{_context} = 'MockContext';
48     $request->headers( HTTP::Headers->new );
49     $request->parameters( { 'content-type' => 'text/fudge' } );
50     $request->method('POST');
51     $request->content_type('text/foobar');
52
53     ok( ! $request->accepts('text/fudge'), 'content type in parameters is ignored for POST' );
54 }
55
56 {
57     my $request = Catalyst::Request::REST->new;
58     $request->{_context} = 'MockContext';
59     $request->headers( HTTP::Headers->new );
60     $request->parameters( {} );
61     $request->method('GET');
62     $request->headers->header(
63         'Accept' =>
64         # From Firefox 2.0 when it requests an html page
65         'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
66     );
67
68     is_deeply( $request->accepted_content_types,
69                [ qw( text/xml application/xml application/xhtml+xml
70                      image/png
71                      text/html
72                      text/plain
73                      */*
74                    ) ],
75                'accept header is parsed properly' );
76     is( $request->preferred_content_type, 'text/xml',
77         'preferred content type is text/xml' );
78     ok( $request->accept_only, 'accept_only is true' );
79     ok( $request->accepts('text/html'), 'accepts text/html' );
80     ok( $request->accepts('image/png'), 'accepts image/png' );
81     ok( ! $request->accepts('image/svg'), 'does not accept image/svg' );
82 }
83
84 {
85     my $request = Catalyst::Request::REST->new;
86     $request->{_context} = 'MockContext';
87     $request->headers( HTTP::Headers->new );
88     $request->parameters( {} );
89     $request->method('GET');
90     $request->content_type('text/x-json');
91     $request->headers->header(
92         'Accept' =>
93         # From Firefox 2.0 when it requests an html page
94         'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
95     );
96
97     is_deeply( $request->accepted_content_types,
98                [ qw( text/x-json
99                      text/xml application/xml application/xhtml+xml
100                      image/png
101                      text/html
102                      text/plain
103                      */*
104                    ) ],
105                'accept header is parsed properly, and content-type header has precedence over accept' );
106     ok( ! $request->accept_only, 'accept_only is false' );
107 }
108
109 {
110     my $request = Catalyst::Request::REST->new;
111     $request->{_context} = 'MockContext';
112     $request->headers( HTTP::Headers->new );
113     $request->parameters( {} );
114     $request->method('GET');
115     $request->content_type('text/x-json');
116     $request->headers->header(
117         'Accept' => 'text/plain,text/x-json',
118     );
119
120     is_deeply( $request->accepted_content_types,
121                [ qw( text/x-json
122                      text/plain
123                    ) ],
124                'each type appears only once' );
125 }
126
127
128 package MockContext;
129
130 sub prepare_body { }