]> git.proxmox.com Git - pve-common.git/blob - test/api_parameter_test.pl
bump version to 8.2.1
[pve-common.git] / test / api_parameter_test.pl
1 #!/usr/bin/perl
2 package PVE::TestAPIParameters;
3
4 # Tests the automatic conversion of -list and array parameter types
5
6 use strict;
7 use warnings;
8
9 use lib '../src';
10
11 use PVE::RESTHandler;
12 use PVE::JSONSchema;
13
14 use Test::More;
15
16 use base qw(PVE::RESTHandler);
17
18 my $setup = [
19 {
20 name => 'list-format-with-list',
21 parameter => {
22 type => 'string',
23 format => 'pve-configid-list',
24 },
25 value => "foo,bar",
26 'value-expected' => "foo,bar",
27 },
28 {
29 name => 'array-format-with-array',
30 parameter => {
31 type => 'array',
32 items => {
33 type => 'string',
34 format => 'pve-configid',
35 },
36 },
37 value => ['foo', 'bar'],
38 'value-expected' => ['foo', 'bar'],
39 },
40 # TODO: below behaviour should be deprecated with 9.x and fail with 10.x
41 {
42 name => 'list-format-with-alist',
43 parameter => {
44 type => 'string',
45 format => 'pve-configid-list',
46 },
47 value => "foo\0bar",
48 'value-expected' => "foo\0bar",
49 },
50 {
51 name => 'array-format-with-non-array',
52 parameter => {
53 type => 'array',
54 items => {
55 type => 'string',
56 format => 'pve-configid',
57 },
58 },
59 value => "foo",
60 'value-expected' => ['foo'],
61 },
62 {
63 name => 'list-format-with-array',
64 parameter => {
65 type => 'string',
66 format => 'pve-configid-list',
67 },
68 value => ['foo', 'bar'],
69 'value-expected' => "foo,bar",
70 },
71 ];
72
73 for my $data ($setup->@*) {
74 __PACKAGE__->register_method({
75 name => $data->{name},
76 path => $data->{name},
77 method => 'POST',
78 parameters => {
79 additionalProperties => 0,
80 properties => {
81 param => $data->{parameter},
82 },
83 },
84 returns => { type => 'null' },
85 code => sub {
86 my ($param) = @_;
87 return $param->{param};
88 }
89 });
90
91 my ($handler, $info) = __PACKAGE__->find_handler('POST', $data->{name});
92 my $param = {
93 param => $data->{value},
94 };
95
96 my $res = $handler->handle($info, $param);
97 is_deeply($res, $data->{'value-expected'}, $data->{name});
98 }
99
100 done_testing();