@@ -13,12 +13,13 @@ var remoteFile = 'http://nodejs.org/images/logo.png';
1313// wrap non simple values into function
1414// just to deal with ReadStream "autostart"
1515// Can't wait for 0.10
16- var FIELDS = [
17- { name : 'my_field' , value : 'my_value' } ,
18- { name : 'my_buffer' , value : function ( ) { return new Buffer ( [ 1 , 2 , 3 ] ) } } ,
19- { name : 'my_file' , value : function ( ) { return fs . createReadStream ( common . dir . fixture + '/unicycle.jpg' ) } } ,
20- { name : 'remote_file' , value : function ( ) { return request ( remoteFile ) } }
21- ] ;
16+ var FIELDS = {
17+ 'my_field' : 'my_value' ,
18+ 'my_buffer' : function ( ) { return new Buffer ( [ 1 , 2 , 3 ] ) ; } ,
19+ 'my_file' : function ( ) { return fs . createReadStream ( common . dir . fixture + '/unicycle.jpg' ) ; } ,
20+ 'remote_file' : function ( ) { return request ( remoteFile ) ; }
21+ } ;
22+ var fieldsPassed = 4 ;
2223
2324var server = http . createServer ( function ( req , res ) {
2425
@@ -28,14 +29,16 @@ var server = http.createServer(function(req, res) {
2829
2930 form
3031 . on ( 'field' , function ( name , value ) {
31- var field = FIELDS . shift ( ) ;
32- assert . strictEqual ( name , field . name ) ;
33- assert . strictEqual ( value , field . value + '' ) ;
32+ fieldsPassed -- ;
33+ var field = FIELDS [ name ] ;
34+ assert . ok ( field ) ;
35+ assert . strictEqual ( value , '' + field ) ;
3436 } )
3537 . on ( 'file' , function ( name , file ) {
36- var field = FIELDS . shift ( ) ;
37- assert . strictEqual ( name , field . name ) ;
38- assert . strictEqual ( file . name , path . basename ( field . value . path ) ) ;
38+ fieldsPassed -- ;
39+ var field = FIELDS [ name ] ;
40+ assert . ok ( field ) ;
41+ assert . strictEqual ( file . name , path . basename ( field . path ) ) ;
3942 assert . strictEqual ( file . type , mime . lookup ( file . name ) ) ;
4043 } )
4144 . on ( 'end' , function ( ) {
@@ -48,13 +51,17 @@ server.listen(common.port, function() {
4851
4952 var form = new FormData ( ) ;
5053
51- FIELDS . forEach ( function ( field ) {
54+ for ( var name in FIELDS )
55+ {
56+ if ( ! FIELDS . hasOwnProperty ( name ) ) continue ;
57+
5258 // important to append ReadStreams within the same tick
53- if ( ( typeof field . value == 'function' ) ) {
54- field . value = field . value ( ) ;
59+ if ( ( typeof FIELDS [ name ] == 'function' ) ) {
60+ FIELDS [ name ] = FIELDS [ name ] ( ) ;
5561 }
56- form . append ( field . name , field . value ) ;
57- } ) ;
62+
63+ form . append ( name , FIELDS [ name ] ) ;
64+ }
5865
5966 form . submit ( 'http://localhost:' + common . port + '/' , function ( err , res ) {
6067
@@ -73,5 +80,5 @@ server.listen(common.port, function() {
7380} ) ;
7481
7582process . on ( 'exit' , function ( ) {
76- assert . strictEqual ( FIELDS . length , 0 ) ;
83+ assert . strictEqual ( fieldsPassed , 0 ) ;
7784} ) ;
0 commit comments