d71d3d08a6833e8a625412d95b928ebdc7af4fa7
[catagits/HTTP-Body.git] / t / 09rt88342.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/lib";
8
9 use Test::More tests => 3;
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   $HTTP::Body::MultiPart::basename_regexp = qr/(\.\w+(?:\.\w+)*)$/;
23
24     my $uploads = uploads_for('015');
25
26         {
27                 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload}{tempname} );        
28                 like(
29                         $file, qr/^.{10}\.tar\.gz\.Z$/,
30                         'tempname preserves .tar.gz.Z suffix'
31                 );
32         }
33
34   {
35     my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );  
36     like(
37       $file, qr/^.{10}\.png$/,
38       'tempname preserves .png suffix'
39     );
40   }
41
42   {
43     my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload3}{tempname} );  
44     like(
45       $file, qr/^.{10}\.txt$/,
46       'tempname preserves .txt suffix'
47     );
48   }
49
50 }
51
52 sub uploads_for {
53     my $number = shift;
54
55     my $headers = PAML::LoadFile( catfile( $path, "$number-headers.pml" ) );
56     my $content = IO::File->new( catfile( $path, "$number-content.dat" ) );
57     my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
58     my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
59     $body->tmpdir($tempdir);
60
61     binmode $content, ':raw';
62
63     while ( $content->read( my $buffer, 1024 ) ) {
64         $body->add($buffer);
65     }
66
67     $body->cleanup(1);
68
69     return $body->upload;
70 }