File: 02_build_content.t

package info (click to toggle)
libhttp-tiny-multipart-perl 0.08-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 128 kB
  • sloc: perl: 268; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (3)
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
#!perl

use strict;
use warnings;

use HTTP::Tiny;
use HTTP::Tiny::Multipart;

use Test::More;

{
    my $error;
    eval { HTTP::Tiny::Multipart::_build_content(); 1; } or $error = $@;
    like $error, qr/Can't use an undefined value/, 'undefined value passed';
}

{
    my $error;
    eval { HTTP::Tiny::Multipart::_build_content('string'); 1; } or $error = $@;
    like $error, qr/Can't use string \("string"\)/, 'string passed';
}

{
    my $error;
    eval { HTTP::Tiny::Multipart::_build_content(['test']); 1; } or $error = $@;
    like $error, qr/form data reference must have an even number of terms/, 'odd number of elements in arrayref passed';
}

{
    my $content = HTTP::Tiny::Multipart::_build_content([ 'field1' => 'test']);
    is_deeply $content, [ "Content-Disposition: form-data; name=\"field1\"\x0d\x0a\x0d\x0atest\x0d\x0a" ], "simple field";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content([ 'field1' => 'test', field2 => 'noch ein test']);
    is_deeply $content, [
        "Content-Disposition: form-data; name=\"field1\"\x0d\x0a\x0d\x0atest\x0d\x0a",
        "Content-Disposition: form-data; name=\"field2\"\x0d\x0a\x0d\x0anoch ein test\x0d\x0a",
    ], "two simple fields";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content({ 'field1' => 'test' });
    is_deeply $content, [ "Content-Disposition: form-data; name=\"field1\"\x0d\x0a\x0d\x0atest\x0d\x0a" ], "simple field (hashref)";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content([ 'field1' => [ 'test', 'noch ein test'] ]);
    is_deeply $content, [
        "Content-Disposition: form-data; name=\"field1\"\x0d\x0a\x0d\x0atest\x0d\x0a",
        "Content-Disposition: form-data; name=\"field1\"\x0d\x0a\x0d\x0anoch ein test\x0d\x0a",
    ], "one field, two values";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content({ 'field1' => { content => 'test' }  });
    is_deeply $content,
       [ "Content-Disposition: form-data; name=\"field1\"; filename=\"field1\"\x0d\x0a\x0d\x0atest\x0d\x0a" ],
       "simple file field";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content({ 'field1' => { content => 'test', content_type => 'text/plain' }  });
    is_deeply $content,
       [ "Content-Disposition: form-data; name=\"field1\"; filename=\"field1\"\x0d\x0aContent-Type: text/plain\x0d\x0a\x0d\x0atest\x0d\x0a" ],
       "simple file field with content type";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content({ 'field1' => { content => 'test', content_type => 'text/plain', filename => 'test.txt' }  });
    is_deeply $content,
       [ "Content-Disposition: form-data; name=\"field1\"; filename=\"test.txt\"\x0d\x0aContent-Type: text/plain\x0d\x0a\x0d\x0atest\x0d\x0a" ],
       "simple file field with content type and filename (basename)";
}

{
    my $content = HTTP::Tiny::Multipart::_build_content({ 'field1' => { content => 'test', content_type => 'text/plain', filename => '/tmp/test.txt' }  });
    is_deeply $content,
       [ "Content-Disposition: form-data; name=\"field1\"; filename=\"test.txt\"\x0d\x0aContent-Type: text/plain\x0d\x0a\x0d\x0atest\x0d\x0a" ],
       "simple file field with content type and filename (basename)";
}

done_testing();