1
1
use strict;
2
2
use warnings;
3
3
use Test::More;
4
+ use Test::Exception;
4
5
use WWW::Mechanize;
5
6
use URI::file;
6
7
7
8
my $file = ' t/file_upload.html' ;
8
9
my $filename = ' the_file_upload.html' ;
9
10
my $mc = WWW::Mechanize-> new;
10
11
my $uri = URI::file-> new_abs( ' t/file_upload.html' )-> as_string;
11
- my ($form , $input );
12
+ my ($form , $input , $as_string );
12
13
13
14
# &field
15
+ $mc -> get( $uri );
16
+ $mc -> field( ' document' , [$file ] );
17
+ ($form ) = $mc -> forms;
18
+ $as_string = $form -> make_request-> as_string;
19
+ like( $as_string , qr ! filename="$file " ! x ,
20
+ q/ $mc->field( 'document', [$file] )/ );
21
+ like(
22
+ $as_string , qr ! <form method="post" enctype="multipart/form-data"! ,
23
+ ' ... and the file was sent'
24
+ );
14
25
15
26
$mc -> get( $uri );
16
27
$mc -> field( ' document' , [$file , $filename ] );
@@ -19,24 +30,46 @@ like( $form->make_request->as_string, qr! filename="$filename" !x,
19
30
q/ $mc->field( 'document', [$file, $filename] )/ );
20
31
21
32
$mc -> get( $uri );
22
- $mc -> field( ' document' , [$file , $filename , Content => ' content' ] );
33
+ $mc -> field( ' document' , [$file , $filename , Content => ' changed content' ] );
23
34
($form ) = $mc -> forms;
24
- like( $form -> make_request-> as_string, qr ! filename="$filename " ! x ,
25
- q/ $mc->field( 'document', [$file, $filename, Content => 'content'] )/ );
35
+ $as_string = $form -> make_request-> as_string;
36
+ like( $as_string , qr ! filename="$filename " ! x ,
37
+ q/ $mc->field( 'document', [$file, $filename, Content => 'changed content'] )/ );
38
+ like(
39
+ $as_string , qr ! changed content! ,
40
+ ' ... and the Content header was sent instead of the file'
41
+ );
42
+
26
43
27
44
# &set_fields
28
45
46
+ $mc -> get( $uri );
47
+ $mc -> set_fields( ' document' => [$file ] );
48
+ ($form ) = $mc -> forms;
49
+ $as_string = $form -> make_request-> as_string;
50
+ like( $as_string , qr ! filename="$file " ! x ,
51
+ q/ $mc->set_fields( 'document', [$file] )/ );
52
+ like(
53
+ $as_string , qr ! <form method="post" enctype="multipart/form-data"! ,
54
+ ' ... and the file was sent'
55
+ );
56
+
29
57
$mc -> get( $uri );
30
58
$mc -> set_fields( ' document' => [ $file , $filename ] );
31
59
($form ) = $mc -> forms;
32
60
like( $form -> make_request-> as_string, qr ! filename="$filename " ! x ,
33
61
q/ $mc->set_fields( 'document' => [ $file, $filename ] )/ );
34
62
35
63
$mc -> get( $uri );
36
- $mc -> set_fields( ' document' => [ $file , $filename , Content => ' content' ] );
64
+ $mc -> set_fields( ' document' => [ $file , $filename , Content => ' my content' ] );
37
65
($form ) = $mc -> forms;
38
- like( $form -> make_request-> as_string, qr ! filename="$filename " ! x ,
39
- q/ $mc->set_fields( 'document' => [ $file, $filename, Content => 'content' ] )/ );
66
+ $as_string = $form -> make_request-> as_string;
67
+ like( $as_string , qr ! filename="$filename " ! x ,
68
+ q/ $mc->set_fields( 'document' => [ $file, $filename, Content => 'my content' ] )/ );
69
+ like(
70
+ $as_string , qr ! my content! ,
71
+ ' ... and the Content header was sent instead of the file'
72
+ );
40
73
41
74
$mc -> get( $uri );
42
75
$mc -> set_fields( ' document' => [[ $file , $filename ], 1] );
@@ -51,4 +84,21 @@ $mc->set_fields
51
84
like( $form -> make_request-> as_string, qr ! filename="$filename " ! x ,
52
85
q/ $mc->set_fields( 'document' => [[ $file, $filename, Content => 'content' ], 1] )/ );
53
86
87
+ $mc -> get( $uri );
88
+ $mc -> set_fields
89
+ ( ' document' => [[ undef , $filename , Content => ' content' ], 1] );
90
+ ($form ) = $mc -> forms;
91
+ $as_string = $form -> make_request-> as_string;
92
+ like( $as_string , qr ! filename="$filename " ! x ,
93
+ q/ $mc->set_fields( 'document' => [[ undef, $filename, Content => 'content' ], 1] )/ );
94
+
95
+ # field does not exist
96
+ $mc -> get( $uri );
97
+ lives_ok { $mc -> set_fields( ' does_not_exist' => [ [$file ], 1 ] ) }
98
+ ' setting a field that does not exist lives' ;
99
+ ($form ) = $mc -> forms;
100
+ $as_string = $form -> make_request-> as_string;
101
+ unlike( $as_string , qr ! filename="$file " ! x ,
102
+ q/ $mc->set_fields( 'does_not_exist' => [ [$file], 1 ] )/ );
103
+
54
104
done_testing;
0 commit comments