Changing default behavior of upload handling to stop taking over the upload extension...
[catagits/HTTP-Body.git] / t / 08multipart-suffix.t
CommitLineData
6a7b9183 1#!perl
2
3use strict;
4use warnings;
5
6use FindBin;
7use lib "$FindBin::Bin/lib";
8
23b5a50c 9use Test::More tests => 6;
6a7b9183 10use Test::Deep;
11
12use Cwd;
13use HTTP::Body;
14use File::Spec::Functions;
15use IO::File;
16use PAML;
17use File::Temp qw/ tempdir /;
18
19my $path = catdir( getcwd(), 't', 'data', 'multipart' );
20
21{
cc75c886 22 $HTTP::Body::MultiPart::file_temp_suffix = undef;
23
6a7b9183 24 my $uploads = uploads_for('001');
25
05f6d238 26 {
27 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );
28 like(
29a092ff 29 $file, qr/^.{10}\.pl$/,
05f6d238 30 'tempname preserves .pl suffix'
31 );
32 }
33
34 {
35 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload4}{tempname} );
36 unlike(
29a092ff 37 $file, qr/^.{10}\..+$/,
05f6d238 38 'tempname for upload4 has no suffix'
39 );
40 }
41
6a7b9183 42}
43
44{
45 my $uploads = uploads_for('006');
46
05f6d238 47 {
48 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );
49 like(
29a092ff 50 $file, qr/^.{10}\.pl$/,
05f6d238 51 'tempname preserves .pl suffix with Windows filename'
52 );
53 }
54
6a7b9183 55}
56
57{
58 my $uploads = uploads_for('014');
59
05f6d238 60 {
61 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload}{tempname} );
62 like(
29a092ff 63 $file, qr/^.{10}\.foo\.txt$/,
05f6d238 64 'tempname preserves .foo.txt suffix'
65 );
66 }
67
68 {
69 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );
70 like(
29a092ff 71 $file, qr/^.{10}\.txt$/,
05f6d238 72 'tempname preserves .txt suffix when dir name has .'
73 );
74 }
75
76 like(
77 $uploads->{upload2}{tempname}, qr/[\\\/]\w+.txt$/,
78 'tempname only gets extension from filename, not from a directory name'
79 );
23b5a50c 80
6a7b9183 81}
82
83sub uploads_for {
84 my $number = shift;
85
86 my $headers = PAML::LoadFile( catfile( $path, "$number-headers.pml" ) );
87 my $content = IO::File->new( catfile( $path, "$number-content.dat" ) );
88 my $body = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
89 my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
90 $body->tmpdir($tempdir);
91
92 binmode $content, ':raw';
93
94 while ( $content->read( my $buffer, 1024 ) ) {
95 $body->add($buffer);
96 }
97
98 $body->cleanup(1);
99
100 return $body->upload;
101}