File: file_post.t

package info (click to toggle)
libhtml-formfu-perl 0.09007-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,184 kB
  • sloc: perl: 13,186; makefile: 9; sql: 5
file content (112 lines) | stat: -rw-r--r-- 3,277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use strict;
use warnings;

use Test::More;
use HTML::FormFu;
use IO::File;

eval "use CGI";
if ($@) {
    plan skip_all => 'CGI required';
    exit;
}

plan tests => 25;

# Copied from CGI.pm - http://search.cpan.org/perldoc?CGI

%ENV = (
    %ENV,
    'SCRIPT_NAME'       => '/test.cgi',
    'SERVER_NAME'       => 'perl.org',
    'HTTP_CONNECTION'   => 'TE, close',
    'REQUEST_METHOD'    => 'POST',
    'SCRIPT_URI'        => 'http://www.perl.org/test.cgi',
    'SCRIPT_FILENAME'   => '/home/usr/test.cgi',
    'SERVER_SOFTWARE'   => 'Apache/1.3.27 (Unix) ',
    'HTTP_TE'           => 'deflate,gzip;q=0.3',
    'QUERY_STRING'      => '',
    'REMOTE_PORT'       => '1855',
    'HTTP_USER_AGENT'   => 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)',
    'SERVER_PORT'       => '80',
    'REMOTE_ADDR'       => '127.0.0.1',
    'CONTENT_TYPE'      => 'multipart/form-data; boundary=xYzZY',
    'SERVER_PROTOCOL'   => 'HTTP/1.1',
    'PATH'              => '/usr/local/bin:/usr/bin:/bin',
    'REQUEST_URI'       => '/test.cgi',
    'GATEWAY_INTERFACE' => 'CGI/1.1',
    'SCRIPT_URL'        => '/test.cgi',
    'SERVER_ADDR'       => '127.0.0.1',
    'DOCUMENT_ROOT'     => '/home/develop',
    'HTTP_HOST'         => 'www.perl.org'
);

my $io = new IO::File 't/elements/file_post.txt';
$io->binmode;

local *STDIN = $io;

my $q = CGI->new();

my $form = HTML::FormFu->new( {
        action   => 'http://www.perl.org/test.cgi',
        elements => [
            { type => 'Text', name => 'multiple' },
            { type => 'File', name => 'multiple' },
            { type => 'File', name => 'multiple' },
            { type => 'File', name => 'hello_world' },
            { type => 'File', name => 'does_not_exist_gif' },
            { type => 'File', name => '100x100_gif' },
            { type => 'File', name => '300x300_gif' },
        ],
    } );

$form->process($q);

{
    my $multiple = $form->params->{multiple};

    is( @$multiple, 3 );

    my ( $m1, $m2, $m3 ) = @$multiple;

    ok( !ref $m1 );
    is( $m1, 'foo' );

    isa_ok( $m2, 'HTML::FormFu::Upload' );
    is( $m2->filename,                'one.txt' );
    is( $m2->headers->content_length, 4 );
    is( $m2->headers->content_type,   'text/plain' );
    is( $m2->slurp,                   "One\n" );

    # new Cat-compatable methods
    is( $m2->size, 4 );
    is( $m2->type, 'text/plain' );

    isa_ok( $m3, 'HTML::FormFu::Upload' );
    is( $m3->filename,                'two.txt' );
    is( $m3->headers->content_length, 5 );
    is( $m3->headers->content_type,   'text/plain' );
    is( $m3->slurp,                   "Two!\n" );
}

{
    my $value = $form->params->{hello_world};

    isa_ok( $value, 'HTML::FormFu::Upload' );
    is( $value->filename,                'hello_world.txt' );
    is( $value->headers->content_length, 13 );
    is( $value->headers->content_type,   'text/plain' );
    is( $value->slurp,                   "Hello World!\n" );
}

{
    my $value = $form->params->{does_not_exist_gif};

    isa_ok( $value, 'HTML::FormFu::Upload' );
    is( $value->filename,                'does_not_exist.gif' );
    is( $value->headers->content_length, undef );
    is( $value->headers->content_type,   'application/octet-stream' );
    is( $value->slurp,                   '' );
}