1# vim:ft=perl
2use strict;
3use warnings;
4
5use Test::More 'no_plan';
6BEGIN { use_ok('Email::MIME::ContentType'); }
7
8my %cd_tests = (
9    'inline' => { type => 'inline', attributes => {} },
10    'attachment' => { type => 'attachment', attributes => {} },
11
12    'attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"' => {
13        type => 'attachment',
14        attributes => {
15            filename => 'genome.jpeg',
16            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
17        }
18    },
19
20    q(attachment; filename*=UTF-8''genom%C3%A9.jpeg; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
21        type => 'attachment',
22        attributes => {
23            filename => "genom\x{E9}.jpeg",
24            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
25        }
26    },
27
28    q(attachment; filename=loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
29        type => 'attachment',
30        attributes => {
31            filename => 'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
32            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
33        }
34    },
35
36    q(attachment; filename*0=loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo; filename*1=ong; filename=looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo...; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
37        type => 'attachment',
38        attributes => {
39            filename => 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
40            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
41        }
42    },
43
44    q(attachment; filename="l\\"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
45        type => 'attachment',
46        attributes => {
47            filename => 'l"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
48            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
49        }
50    },
51
52    q(attachment; filename*0="l\\"oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"; filename*1="ong"; filename="l\"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo..."; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
53        type => 'attachment',
54        attributes => {
55            filename => 'l"ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
56            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
57        }
58    },
59
60    q(attachment; filename*=UTF-8''%C3%A9loooooooooooooooooooooooooooooooooooooooooooooooooong; filename=eloooooooooooooooooooooooooooooooooooooooooooooooooong; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
61        type => 'attachment',
62        attributes => {
63            filename => "\x{E9}loooooooooooooooooooooooooooooooooooooooooooooooooong",
64            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
65        }
66    },
67
68    q(attachment; filename*0*=UTF-8''%C3%A9loooooooooooooooooooooooooooooooooooooooooooooooooo; filename*1*=ong; filename=elooooooooooooooooooooooooooooooooooooooooooooooooooong; modification-date="Wed, 12 Feb 1997 16:29:51 -0500") => {
69        type => 'attachment',
70        attributes => {
71            filename => "\x{E9}looooooooooooooooooooooooooooooooooooooooooooooooooong",
72            'modification-date' => 'Wed, 12 Feb 1997 16:29:51 -0500'
73        }
74    },
75
76    q(attachment; filename*=UTF-8''%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9; filename=eeeeeeeee) => {
77        type => 'attachment',
78        attributes => {
79            filename => "\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}"
80        }
81    },
82
83    q(attachment; filename*0*=UTF-8''%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9%C3%A9; filename*1*=%C3%A9; filename=eeeeeeeeee) => {
84        type => 'attachment',
85        attributes => {
86            filename => "\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}\x{E9}"
87        }
88    },
89
90    q(attachment; filename="UTF-8''name") => {
91        type => 'attachment',
92        attributes => {
93            filename => "UTF-8''name"
94        }
95    },
96);
97
98sub test {
99    my ($expect, $struct) = @_;
100    local $_;
101    my $info = $expect;
102    $info =~ s/\r/\\r/g;
103    $info =~ s/\n/\\n/g;
104    my $got = build_content_disposition($struct);
105    is($got, $expect, "Can build C-D <$info>");
106    my $parsed = parse_content_disposition($got);
107    is_deeply($parsed, $struct, "Can parse C-D <$info>");
108}
109
110for (sort keys %cd_tests) {
111    test($_, $cd_tests{$_});
112}
113