Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 216 lines (190 sloc) 8.049 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
use strict;
use warnings;
use Test::More;
use FindBin;
use lib ( "$FindBin::Bin/../lib", "$FindBin::Bin/../t/lib" );

use Catalyst::Request::REST;
use Catalyst::TraitFor::Request::REST;
use Moose::Meta::Class;
use HTTP::Headers;

my $anon_class = Moose::Meta::Class->create_anon_class(
    superclasses => ['Catalyst::Request'],
    roles => ['Catalyst::TraitFor::Request::REST::ForBrowsers'],
    cache => 1,
)->name;

for my $class ( $anon_class, 'Catalyst::Request::REST' ) {
    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( {} );
        $request->method('GET');
        $request->content_type('text/foobar');

        is_deeply( $request->accepted_content_types, [ 'text/foobar' ],
                   'content-type set in request headers is found' );
        is( $request->preferred_content_type, 'text/foobar',
            'preferred content type is text/foobar' );
        ok( ! $request->accept_only, 'accept_only is false' );
        ok( $request->accepts('text/foobar'), 'accepts text/foobar' );
        ok( ! $request->accepts('text/html'), 'does not accept text/html' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( { 'content-type' => 'text/fudge' } );
        $request->method('GET');
        $request->content_type('text/foobar');

        is_deeply( $request->accepted_content_types, [ 'text/foobar', 'text/fudge' ],
                   'content-type set in request headers and type in parameters is found' );
        is( $request->preferred_content_type, 'text/foobar',
            'preferred content type is text/foobar' );
        ok( ! $request->accept_only, 'accept_only is false' );
        ok( $request->accepts('text/foobar'), 'accepts text/foobar' );
        ok( $request->accepts('text/fudge'), 'accepts text/fudge' );
        ok( ! $request->accepts('text/html'), 'does not accept text/html' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( { 'content-type' => 'text/fudge' } );
        $request->method('POST');
        $request->content_type('text/foobar');

        ok( ! $request->accepts('text/fudge'), 'content type in parameters is ignored for POST' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( {} );
        $request->method('GET');
        $request->headers->header(
            'Accept' =>
            # From Firefox 2.0 when it requests an html page
            'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
        );

        is_deeply( $request->accepted_content_types,
                   [ qw( text/xml application/xml application/xhtml+xml
image/png
text/html
text/plain
*/*
) ],
                   'accept header is parsed properly' );
        is( $request->preferred_content_type, 'text/xml',
            'preferred content type is text/xml' );
        ok( $request->accept_only, 'accept_only is true' );
        ok( $request->accepts('text/html'), 'accepts text/html' );
        ok( $request->accepts('image/png'), 'accepts image/png' );
        ok( ! $request->accepts('image/svg'), 'does not accept image/svg' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( {} );
        $request->method('GET');
        $request->content_type('application/json');
        $request->headers->header(
            'Accept' =>
            # From Firefox 2.0 when it requests an html page
            'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
        );

        is_deeply( $request->accepted_content_types,
                   [ qw( application/json
text/xml application/xml application/xhtml+xml
image/png
text/html
text/plain
*/*
) ],
                   'accept header is parsed properly, and content-type header has precedence over accept' );
        ok( ! $request->accept_only, 'accept_only is false' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( {} );
        $request->method('GET');
        $request->content_type('application/json');
        $request->headers->header(
            'Accept' =>
            # From Firefox 2.0 when it requests an html page
            'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
        );

        is_deeply( $request->accepted_content_types,
                   [ qw( application/json
text/xml application/xml application/xhtml+xml
image/png
text/html
text/plain
*/*
) ],
                   'accept header is parsed properly, and content-type header has precedence over accept' );
        ok( ! $request->accept_only, 'accept_only is false' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( {} );
        $request->method('GET');
        $request->content_type('text/x-json');
        $request->headers->header(
            'Accept' => 'text/plain,text/x-json',
        );

        is_deeply( $request->accepted_content_types,
                   [ qw( text/x-json
text/plain
) ],
                   'each type appears only once' );
    }

    {
        my $request = Catalyst::Request::REST->new;
        $request->{_context} = 'MockContext';
        $request->headers( HTTP::Headers->new );
        $request->parameters( {} );
        $request->method('GET');
        $request->content_type('application/json');
        $request->headers->header(
            'Accept' => 'text/plain,application/json',
        );

        is_deeply( $request->accepted_content_types,
                   [ qw( application/json
text/plain
) ],
                   'each type appears only once' );
    }
}

{
  local %ENV=%ENV;
  $ENV{CATALYST_DEBUG} = 0;
  my $test = 'Test::Catalyst::Action::REST';
  use_ok $test;
  ok($test->request_class->meta->does_role('Catalyst::TraitFor::Request::REST'),
    'request class does REST role');

  my $meta = Moose::Meta::Class->create_anon_class(
      superclasses => ['Catalyst::Request'],
  );
  $meta->add_method('__random_method' => sub { 42 });

  $test->request_class($meta->name);
  # FIXME - setup_finished(0) is evil!
  eval { $test->setup_finished(0); $test->setup };
  ok !$@, 'Can setup again';
  isnt $test->request_class, $meta->name, 'Different request class';
  ok $test->request_class->can('__random_method'), 'Is right class';
  ok($test->request_class->meta->does_role('Catalyst::TraitFor::Request::REST'),
    'request class still does REST role');

  {
    package My::Request;
    use base 'Catalyst::Request::REST';
  }
  $test->request_class('My::Request');
  eval { $test->setup_finished(0); $test->setup };
  is $@, '', 'no error from Request::REST subclass';
}

done_testing;

package MockContext;

sub prepare_body { }
Something went wrong with that request. Please try again.