make the missing data handler exception slightly more useful
[catagits/Catalyst-Runtime.git] / t / data_handler.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use FindBin;
7 use Test::More;
8 use HTTP::Request::Common;
9 use JSON::MaybeXS;
10 use Capture::Tiny qw/:all/;
11
12 use lib "$FindBin::Bin/lib";
13 use Catalyst::Test 'TestDataHandlers';
14
15 ok my($res, $c) = ctx_request('/');
16
17 {
18   ok my $message = 'helloworld';
19   ok my $post = encode_json +{message=>$message};
20   ok my $req = POST $c->uri_for_action('/test_json'),
21      Content_Type => 'application/json',
22      Content => $post;
23
24   ok my $response = request $req, 'got a response from a catalyst controller';
25   is $response->content, $message, 'expected content body';
26 }
27
28 {
29   ok my $req = POST $c->uri_for_action('/test_nested_for'), [ 'nested.value' => 'expected' ];
30   ok my $response = request $req, 'got a response from a catalyst controller';
31   is $response->content, 'expected', 'expected content body';
32 }
33
34 {
35   my $out;
36   local *STDERR;
37   open(STDERR, ">", \$out) or die "Can't open STDERR: $!";
38   ok my $req = POST $c->uri_for_action('/test_nested_for'), 'Content-Type' => 'multipart/form-data', Content => { die => "a horrible death" };
39   ok my $response = request $req;
40   is($out, "[error] multipart/form-data does not have an available data handler. Valid data_handlers are application/json, application/x-www-form-urlencoded.\n", 'yep we throw the slightly more usefull error');
41 }
42
43 done_testing;