Commit | Line | Data |
9c7b6768 |
1 | use warnings; |
2 | use strict; |
2bf65524 |
3 | use Test::More; |
4 | |
5 | plan skip_all => "Test Cases are Sketch for next release"; |
6 | |
7 | __END__ |
9c7b6768 |
8 | |
9 | # Test case to check that we now send scalar and filehandle like |
10 | # bodys directly to the PSGI engine, rather than call $writer->write |
11 | # or unroll the filehandle ourselves. |
12 | |
13 | { |
14 | package MyApp::Controller::User; |
15 | |
16 | use base 'Catalyst::Controller'; |
17 | use JSON::MaybeXS; |
18 | |
19 | my %user = ( |
20 | name => 'John', |
21 | age => 44, |
22 | ); |
23 | |
24 | |
25 | sub get_user :Chained(/) PathPrefix CaptureArgs(0) |
26 | { |
27 | pop->stash(user=>\%user); |
28 | } |
29 | |
30 | sub show :GET Chained(get_user) PathPart('') Args(0) { |
31 | my ($self, $c) = @_; |
32 | my $user = $c->stash->{user}; |
33 | $c->res->format( |
34 | 'application/json' => sub { encode_json $user }, |
35 | 'text/html' => sub { "<p>Hi I'm $user->{name} and my age is $user->{age}</p>" } |
36 | ); |
37 | } |
38 | |
39 | sub post_user :POST Chained(root) PathPart('') Args(0) Consumes(HTMLForm,JSON) |
40 | { |
41 | my ($self, $c) = @_; |
42 | %user = (%user, %{$c->req->body_data}); |
43 | $c->res->status(201); |
44 | $c->res->location($c->uri_for( $self->action_for('show'))); |
45 | } |
46 | |
2bf65524 |
47 | $INC{'MyApp/Controller/User.pm'} = __FILE__; |
9c7b6768 |
48 | |
49 | package MyApp; |
50 | use Catalyst; |
51 | |
52 | use HTTP::Headers::ActionPack; |
53 | |
54 | my $cn = HTTP::Headers::ActionPack->new |
55 | ->get_content_negotiator; |
56 | |
57 | sub Catalyst::Response::format |
58 | { |
59 | my $self = shift; |
60 | my %formats = @_; |
61 | my @formats = keys %formats; |
62 | |
63 | my $accept = $self->_context->req->header('Accept') || |
64 | $format{default} || |
65 | $_[0]; |
66 | |
67 | $self->headers->header('Vary' => 'Accept'); |
68 | $self->headers->header('Accepts' => (join ',', @formats)); |
69 | |
70 | if(my $which = $cn->choose_media_type(\@formats, $accept)) { |
71 | $self->content_type($which); |
72 | if(my $possible_body = $formats{$which}->($self)) { |
73 | $self->body($possible_body) unless $self->has_body || $self->has_write_fh; |
74 | } |
75 | } else { |
76 | $self->status(406); |
77 | $self->body("Method Not Acceptable"); |
78 | } |
79 | } |
80 | |
81 | |
82 | MyApp->setup; |
83 | } |
84 | |
85 | |
86 | |
2bf65524 |
87 | |
9c7b6768 |
88 | use HTTP::Request::Common; |
89 | use Catalyst::Test 'MyApp'; |
90 | |
91 | ok my($res, $c) = ctx_request('/'); |
92 | |
9c7b6768 |
93 | done_testing(); |