Changing default behavior of upload handling to stop taking over the upload extension...
[catagits/HTTP-Body.git] / t / 11new-suffix.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 => 6;
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         {
25                 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );       
26                 like(
27                         $file, qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/,
28                         'everything is file_temp_suffix now'
29                 );
30         }
31
32         {
33                 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload4}{tempname} );       
34                 like(
35                         $file, qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/,
36                         'everything is file_temp_suffix now'
37                 );
38         }
39
40 }
41
42 {
43     my $uploads = uploads_for('006');
44
45         {
46                 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );       
47                 like(
48                         $file, qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/,
49                         'everything is file_temp_suffix now'
50                 );
51         }
52
53 }
54
55 {
56     my $uploads = uploads_for('014');
57
58         {
59                 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload}{tempname} );        
60                 like(
61                         $file, qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/,
62                         'everything is file_temp_suffix now'
63                 );
64         }
65
66         {
67                 my ($volume,$directories,$file) = File::Spec->splitpath( $uploads->{upload2}{tempname} );       
68                 like(
69                         $file, qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/,
70                         'everything is file_temp_suffix now'
71                 );
72         }
73
74         like(
75                 $uploads->{upload2}{tempname}, qr/\Q$HTTP::Body::MultiPart::file_temp_suffix\E$/,
76                 'everything is file_temp_suffix now'
77         );
78
79 }
80
81 sub uploads_for {
82     my $number = shift;
83
84     my $headers = PAML::LoadFile( catfile( $path, "$number-headers.pml" ) );
85     my $content = IO::File->new( catfile( $path, "$number-content.dat" ) );
86     my $body    = HTTP::Body->new( $headers->{'Content-Type'}, $headers->{'Content-Length'} );
87     my $tempdir = tempdir( 'XXXXXXX', CLEANUP => 1, DIR => File::Spec->tmpdir() );
88     $body->tmpdir($tempdir);
89
90     binmode $content, ':raw';
91
92     while ( $content->read( my $buffer, 1024 ) ) {
93         $body->add($buffer);
94     }
95
96     $body->cleanup(1);
97
98     return $body->upload;
99 }