File: 05_serializer_and_resource.t

package info (click to toggle)
libdancer-plugin-rest-perl 0.11-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 172 kB
  • sloc: perl: 151; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,781 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Dancer::ModuleLoader;
use Test::More import => ['!pass'];

# Dancer::Test had a bug in version previous 1.3059_01 that prevent this test
# from running correctly.
my $dancer_version = eval "\$Dancer::VERSION";
$dancer_version =~ s/_//g;
plan skip_all => "Dancer 1.3059_01 is needed for this test (you have $dancer_version)"
  if $dancer_version < 1.305901;

plan skip_all => 'tests require JSON'
    unless Dancer::ModuleLoader->load('JSON');

{
    package Webservice;
    use Dancer;
    use Dancer::Plugin::REST;

    setting environment => 'testing';
    prepare_serializer_for_format;

    resource user =>
        get    => sub { [ id   => params->{id},   format => params->{format} ] },
        delete => sub { [ id   => params->{id},   format => params->{format} ] },
        create => sub { [ user => params->{user}, format => params->{format} ] },
        update => sub { [ user => params->{user}, format => params->{format} ] };
}

use Dancer::Test;
plan tests => 4;

my $r = dancer_response GET => '/user/42.json';

is $r->{content}, '["id","42","format","json"]'
    => "'id' and 'format' params are properly set for resource during GET";

$r = dancer_response POST => '/user.json', { params => { user => 'test' } };

is $r->{content}, '["user","test","format","json"]'
    => "'user' and 'format' params properly set for resource during POST";

$r = dancer_response PUT => '/user/1.json', { params => { user => 'anothertest' } };

is  $r->{content}, '["user","anothertest","format","json"]'
    => "'user' and 'format' params properly set for resource during PUT";

$r = dancer_response DELETE => '/user/6.json';

is $r->{content}, '["id","6","format","json"]'
    => "'id' and 'format' params properly set for resource during DELETE";