Commit | Line | Data |
6a7b9183 |
1 | #!perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use FindBin; |
7 | use lib "$FindBin::Bin/lib"; |
8 | |
23b5a50c |
9 | use Test::More tests => 6; |
6a7b9183 |
10 | use Test::Deep; |
11 | |
12 | use Cwd; |
13 | use HTTP::Body; |
14 | use File::Spec::Functions; |
15 | use IO::File; |
16 | use PAML; |
17 | use File::Temp qw/ tempdir /; |
18 | |
19 | my $path = catdir( getcwd(), 't', 'data', 'multipart' ); |
20 | |
21 | { |
22 | my $uploads = uploads_for('001'); |
23 | |
24 | like( |
25 | $uploads->{upload2}{tempname}, qr/\.pl$/, |
26 | 'tempname preserves .pl suffix' |
27 | ); |
28 | |
29 | unlike( |
30 | $uploads->{upload4}{tempname}, qr/\..+$/, |
31 | 'tempname for upload4 has no suffix' |
32 | ); |
33 | } |
34 | |
35 | { |
36 | my $uploads = uploads_for('006'); |
37 | |
38 | like( |
39 | $uploads->{upload2}{tempname}, qr/\.pl$/, |
40 | 'tempname preserves .pl suffix with Windows filename' |
41 | ); |
42 | } |
43 | |
44 | { |
45 | my $uploads = uploads_for('014'); |
46 | |
47 | like( |
48 | $uploads->{upload}{tempname}, qr/\.foo\.txt$/, |
49 | 'tempname preserves .foo.txt suffix' |
50 | ); |
23b5a50c |
51 | |
52 | like( |
53 | $uploads->{upload2}{tempname}, qr/\.txt$/, |
54 | 'tempname preserves .txt suffix when dir name has .' |
55 | ); |
56 | |
57 | unlike( |
58 | $uploads->{upload2}{tempname}, qr/\\/, |
59 | 'tempname only gets extension from filename, not from a directory name' |
60 | ); |
6a7b9183 |
61 | } |
62 | |
63 | sub uploads_for { |
64 | my $number = shift; |
65 | |
66 | my $headers = PAML::LoadFile( catfile( $path, "$number-headers.pml" ) ); |
67 | my $content = IO::File->new( catfile( $path, "$number-content.dat" ) ); |
68 | my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} ); |
69 | my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() ); |
70 | $body->tmpdir($tempdir); |
71 | |
72 | binmode $content, ':raw'; |
73 | |
74 | while ( $content->read( my $buffer, 1024 ) ) { |
75 | $body->add($buffer); |
76 | } |
77 | |
78 | $body->cleanup(1); |
79 | |
80 | return $body->upload; |
81 | } |