File: 02_request_json.t

package info (click to toggle)
libdancer-perl 1.3521%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,460 kB
  • sloc: perl: 7,436; xml: 2,211; sh: 54; makefile: 32; sql: 5
file content (65 lines) | stat: -rw-r--r-- 1,666 bytes parent folder | download | duplicates (5)
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
use Test::More import => ['!pass'];
use strict;
use warnings;
use Dancer ':tests';
use Dancer::Test;

BEGIN {
    plan skip_all => "need JSON"
      unless Dancer::ModuleLoader->load('JSON');

    plan tests => 11;
}

set 'serializer' => 'JSON', 'show_errors' => 1;

get  '/'          => sub { { foo => 'bar' } };
post '/'          => sub { params };
put  '/'          => sub { param("id") };
get  '/error'     => sub { send_error( { foo => 42 }, 401 ) };
get  '/error_bis' => sub { send_error( 42, 402 ) };
get  '/json'      => sub {
    content_type('application/json');
    to_json( { foo => 'bar' } )
};

response_content_is [ PUT => '/',
                      {
                       body    => '{"id": "foo"}' ,
                       headers => [ 'Content-Type' => 'application/json' ],
                      }
                    ] => 'foo';


for my $route ( '/', '/json' ) {
    my $res = dancer_response( GET => $route );
    is $res->header('Content-Type'), 'application/json';
    like $res->content, qr/"foo" : "bar"/;
}

my $res = dancer_response
  (
   POST => '/',
   {
    params  => { foo            => 1 },
    headers => [ 'Content-Type' => 'application/json' ]
   }
  );

is_deeply(
          from_json( $res->content ),
          { foo => 1 },
          "data is correctly deserialized"
         );

is $res->header('Content-Type'), 'application/json',
  'goodcontent type set in response';

$res = dancer_response( GET => '/error' );
is $res->status, 401;
is_deeply( from_json( $res->content ), { foo => 42 } );

$res = dancer_response( GET => '/error_bis' );
is $res->status, 402;
is_deeply( from_json( $res->content ), { error => 42 } );