/
04_mutable.t
94 lines (74 loc) · 2.5 KB
/
04_mutable.t
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
use Test::More import => ['!pass'];
use strict;
use warnings;
use Dancer;
BEGIN {
plan skip_all => 'YAML is needed to run this test'
unless Dancer::ModuleLoader->load('YAML');
plan skip_all => 'JSON is needed to run this test'
unless Dancer::ModuleLoader->load('JSON');
}
plan tests => 17;
ok(setting('serializer' => 'Mutable'), "serializer Mutable loaded");
my $s = Dancer::Serializer->engine;
isa_ok($s, $_) for qw(
Dancer::Engine
Dancer::Serializer::Abstract
Dancer::Serializer::Mutable);
can_ok $s, qw(serialize deserialize);
ok !defined $s->content_type, 'no content_type defined';
ok $s->support_content_type('application/json'),
'application/json is a supported content_type';
ok !$s->support_content_type('foo/bar'),
'foo/bar is not a supported content_type';
my $request = Dancer::Request->new_for_request('GET', '/',);
Dancer::SharedData->request($request);
my $data = {foo => 42};
ok my $result = $s->serialize($data), 'got result from serialize';
like $result, qr/{"foo":42}/, 'result is correct';
is $s->content_type, 'application/json', 'correct content_type';
$result = $s->deserialize($result, $request);
is_deeply($result, $data, 'got same result');
%ENV = (
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/',
'PATH_INFO' => '/',
'QUERY_STRING' => 'foo=bar&number=42',
'HTTP_ACCEPT_TYPE' => 'text/x-yaml',
);
my $req = Dancer::Request->new(\%ENV);
Dancer::SharedData->request($req);
eval { $s->deserialize($data) };
ok $@, 'died okay';
$result = $s->serialize($data);
is $s->content_type, 'text/x-yaml', 'correct content_type';
# tests to check _find_content_type works as expected
%ENV = (
'REQUEST_METHOD' => 'GET',
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_ACCEPT' => 'text/xml',
'HTTP_ACCEPT_TYPE' => 'text/x-yaml',
'PATH_INFO' => '/',
);
$req = Dancer::Request->new(\%ENV);
Dancer::SharedData->request($req);
my $ct = $s->_find_content_type($req);
is_deeply $ct, ['text/xml', 'text/x-yaml', 'application/json'];
%ENV = (
'REQUEST_METHOD' => 'PUT',
'PATH_INFO' => '/',
);
$req = Dancer::Request->new(\%ENV);
Dancer::SharedData->request($req);
$ct = $s->_find_content_type($req);
is_deeply $ct, ['application/json'];
%ENV = (
'REQUEST_METHOD' => 'PUT',
'PATH_INFO' => '/',
'HTTP_ACCEPT' => 'text/xml',
'CONTENT_TYPE' => 'application/json',
);
$req = Dancer::Request->new(\%ENV);
Dancer::SharedData->request($req);
$ct = $s->_find_content_type($req);
is_deeply $ct, ['application/json', 'text/xml'];