File: file_upload.t

package info (click to toggle)
libwww-mechanize-perl 2.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: perl: 4,623; makefile: 6
file content (136 lines) | stat: -rw-r--r-- 3,797 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use strict;
use warnings;
use Test::More;
use Test::Fatal    qw( lives_ok );
use WWW::Mechanize ();
use URI::file      ();

my $file     = 't/file_upload.html';
my $filename = 'the_file_upload.html';
my $mc       = WWW::Mechanize->new;
my $uri      = URI::file->new_abs('t/file_upload.html')->as_string;
my ( $form, $input, $as_string );

# &field
$mc->get($uri);
$mc->field( 'document', [$file] );
($form) = $mc->forms;
$as_string = $form->make_request->as_string;
like(
    $as_string, qr! filename="$file" !x,
    q/$mc->field( 'document', [$file] )/
);
like(
    $as_string, qr!<form method="post" enctype="multipart/form-data"!,
    '... and the file was sent'
);

$mc->get($uri);
$mc->field( 'document', [ $file, $filename ] );
($form) = $mc->forms;
like(
    $form->make_request->as_string, qr! filename="$filename" !x,
    q/$mc->field( 'document', [$file, $filename] )/
);

$mc->get($uri);
$mc->field( 'document', [ $file, $filename, Content => 'changed content' ] );
($form) = $mc->forms;
$as_string = $form->make_request->as_string;
like(
    $as_string, qr! filename="$filename" !x,
    q/$mc->field( 'document', [$file, $filename, Content => 'changed content'] )/
);
like(
    $as_string, qr!changed content!,
    '... and the Content header was sent instead of the file'
);

# &set_fields

$mc->get($uri);
$mc->set_fields( 'document' => [$file] );
($form) = $mc->forms;
$as_string = $form->make_request->as_string;
like(
    $as_string, qr! filename="$file" !x,
    q/$mc->set_fields( 'document', [$file] )/
);
like(
    $as_string, qr!<form method="post" enctype="multipart/form-data"!,
    '... and the file was sent'
);

$mc->get($uri);
$mc->set_fields( 'document' => [ $file, $filename ] );
($form) = $mc->forms;
like(
    $form->make_request->as_string, qr! filename="$filename" !x,
    q/$mc->set_fields( 'document' => [ $file, $filename ] )/
);

$mc->get($uri);
$mc->set_fields(
    'document' => [ $file, $filename, Content => 'my content' ] );
($form) = $mc->forms;
$as_string = $form->make_request->as_string;
like(
    $as_string, qr! filename="$filename" !x,
    q/$mc->set_fields( 'document' => [ $file, $filename, Content => 'my content' ] )/
);
like(
    $as_string, qr!my content!,
    '... and the Content header was sent instead of the file'
);

$mc->get($uri);
$mc->set_fields( 'document' => [ [ $file, $filename ], 1 ] );
($form) = $mc->forms;
like(
    $form->make_request->as_string, qr! filename="$filename" !x,
    q/$mc->set_fields( 'document' => [[ $file, $filename ], 1] )/
);

$mc->get($uri);
$mc->set_fields(
    'document' => [ [ $file, $filename, Content => 'content' ], 1 ] );
($form) = $mc->forms;
like(
    $form->make_request->as_string, qr! filename="$filename" !x,
    q/$mc->set_fields( 'document' => [[ $file, $filename, Content => 'content' ], 1] )/
);

$mc->get($uri);
$mc->set_fields(
    'document' => [ [ undef, $filename, Content => 'content' ], 1 ] );
($form) = $mc->forms;
$as_string = $form->make_request->as_string;
like(
    $as_string, qr! filename="$filename" !x,
    q/$mc->set_fields( 'document' => [[ undef, $filename, Content => 'content' ], 1] )/
);

# &set_fields with multiple fields
$mc->get($uri);
$mc->set_fields(
    'another_field' => 'foo',
    'document'      => [ $file, $filename ]
);
($form) = $mc->forms;
like(
    $form->make_request->as_string, qr! filename="$filename" !x,
    q/$mc->set_fields( 'another_field' => 'foo', 'document' => [ $file, $filename ] )/
);

# field does not exist
$mc->get($uri);
lives_ok { $mc->set_fields( 'does_not_exist' => [ [$file], 1 ] ) }
'setting a field that does not exist lives';
($form) = $mc->forms;
$as_string = $form->make_request->as_string;
unlike(
    $as_string, qr! filename="$file" !x,
    q/$mc->set_fields( 'does_not_exist' => [ [$file], 1 ] )/
);

done_testing;