11var common = require ( '../common' ) ;
22var assert = common . assert ;
33var http = require ( 'http' ) ;
4- var path = require ( 'path' ) ;
5- var mime = require ( 'mime-types' ) ;
64var parseUrl = require ( 'url' ) . parse ;
75var FormData = require ( common . dir . lib + '/form_data' ) ;
86var IncomingForm = require ( 'formidable' ) . IncomingForm ;
97
108// static server prepared for all tests
119var remoteFile = 'http://localhost:' + common . staticPort + '/unicycle.jpg' ;
1210
13- var FIELDS ;
1411var server ;
1512
1613var parsedUrl = parseUrl ( remoteFile ) ;
@@ -21,19 +18,31 @@ var options = {
2118 host : parsedUrl . hostname
2219} ;
2320
21+ var FIELDS = {
22+ 'my_field' : {
23+ value : 'my_value'
24+ } ,
25+ 'my_buffer' : {
26+ type : FormData . DEFAULT_CONTENT_TYPE ,
27+ value : common . defaultTypeValue
28+ } ,
29+ 'remote_file' : {
30+ value : 'TBD' ,
31+ name : remoteFile
32+ }
33+ } ;
34+ // count total
35+ var fieldsPassed = Object . keys ( FIELDS ) . length ;
36+
2437// request static file
2538http . request ( options , function ( response ) {
2639
27- FIELDS = [
28- { name : 'my_field' , value : 'my_value' } ,
29- { name : 'my_buffer' , value : new Buffer ( [ 1 , 2 , 3 ] ) } ,
30- { name : 'remote_file' , value : response }
31- ] ;
32-
3340 var form = new FormData ( ) ;
34- FIELDS . forEach ( function ( field ) {
35- form . append ( field . name , field . value ) ;
36- } ) ;
41+
42+ // add http response to the form fields
43+ FIELDS [ 'remote_file' ] . value = response ;
44+
45+ common . actions . populateFields ( form , FIELDS ) ;
3746
3847 server . listen ( common . port , function ( ) {
3948 common . actions . submit ( form , server ) ;
@@ -48,23 +57,16 @@ server = http.createServer(function(req, res) {
4857
4958 form . parse ( req ) ;
5059
51- form
52- . on ( 'field' , function ( name , value ) {
53- var field = FIELDS . shift ( ) ;
54- assert . strictEqual ( name , field . name ) ;
55- assert . strictEqual ( value , field . value + '' ) ;
56- } )
57- . on ( 'file' , function ( name , file ) {
58- var field = FIELDS . shift ( ) ;
59- assert . strictEqual ( name , field . name ) ;
60- // http response doesn't have path property
61- assert . strictEqual ( file . name , path . basename ( field . value . path || remoteFile ) ) ;
62- assert . strictEqual ( file . type , mime . lookup ( file . name ) ) ;
63- } )
64- . on ( 'end' , common . actions . formOnEnd . bind ( null , res ) ) ;
60+ common . actions . checkForm ( form , FIELDS , function ( fieldsChecked )
61+ {
62+ // keep track of number of the processed fields
63+ fieldsPassed = fieldsPassed - fieldsChecked ;
64+ // finish it
65+ common . actions . formOnEnd ( res ) ;
66+ } ) ;
6567} ) ;
6668
6769
6870process . on ( 'exit' , function ( ) {
69- assert . strictEqual ( FIELDS . length , 0 ) ;
71+ assert . strictEqual ( fieldsPassed , 0 ) ;
7072} ) ;
0 commit comments