Skip to content

Commit

Permalink
Improve examples and tests (#523)
Browse files Browse the repository at this point in the history
* improve code style

* test Array parameters support

* add example on how to use multipartParser as a standalone module

* add example for multiple and arrays

* Update multiple.js

* update tc for multiple values
  • Loading branch information
GrosSacASac committed Dec 4, 2019
1 parent 18d10d1 commit 036a809
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 18 deletions.
6 changes: 3 additions & 3 deletions example/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ server.listen(port);

console.log('listening on http://localhost:'+port+'/');


var message = '{"numbers":[1,2,3,4,5],"nested":{"key":"value"}}';
var request = http.request({
host: 'localhost',
path: '/',
port: port,
method: 'POST',
headers: { 'content-type':'application/json', 'content-length':48 }
headers: { 'content-type':'application/json', 'content-length': message.length }
}, function(response) {
var data = '';
console.log('\nServer responded with:');
Expand All @@ -62,5 +62,5 @@ var request = http.request({
// });
});

request.write('{"numbers":[1,2,3,4,5],"nested":{"key":"value"}}');
request.write(message);
request.end();
35 changes: 35 additions & 0 deletions example/multipartParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { MultipartParser } = require('../lib/multipart_parser.js');


const multipartParser = new MultipartParser();

// hand crafted multipart
const boundary = '--abcxyz';
const next = '\r\n';
const formData = 'Content-Disposition: form-data; ';
const buffer = Buffer.from(
`${boundary}${next}${formData}name="text"${next}${next}text ...${next}${next}${boundary}${next}${formData}name="z"${next}${next}text inside z${next}${next}${boundary}${next}${formData}name="file1"; filename="a.txt"${next}Content-Type: text/plain${next}${next}Content of a.txt.${next}${next}${boundary}${next}${formData}name="file2"; filename="a.html"${next}Content-Type: text/html${next}${next}<!DOCTYPE html><title>Content of a.html.</title>${next}${next}${boundary}--`
);

const logAnalyzed = (buffer, start, end) => {
if (buffer && start && end) {
console.log(String(buffer.slice(start, end)))
}
};

// multipartParser.onPartBegin
// multipartParser.onPartEnd

// multipartParser.on('partData', logAnalyzed) // non supported syntax
multipartParser.onPartData = logAnalyzed;
multipartParser.onHeaderField = logAnalyzed;
multipartParser.onHeaderValue = logAnalyzed;
multipartParser.initWithBoundary(boundary.substring(2));


const bytesParsed = multipartParser.write(buffer);
const error = multipartParser.end();

if (error) {
console.error(error);
}
50 changes: 50 additions & 0 deletions example/multiple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var common = require('../test/common');
var http = require('http'),
util = require('util'),
os = require('os'),
formidable = common.formidable,
port = common.port,
server;

server = http.createServer(function(req, res) {
if (req.url === '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
`<form action="/upload" enctype="multipart/form-data" method="post">
<label>simple<input type="text" name="simple"></label><br>
<label>array text 0<input type="text" name="atext[]"></label><br>
<label>array text 1<input type="text" name="atext[]"></label><br>
<label>file simple<input type="file" name="filesimple"></label><br>
<label>file attribute multiple<input type="file" name="multiplefile" multiple></label><br>
<label>file html array0<input type="file" name="filearray[]"></label><br>
<label>file html array1<input type="file" name="filearray[]"></label><br>
<label>file html array and mulitple0<input type="file" name="mfilearray[]" multiple></label><br>
<label>file html array and mulitple1<input type="file" name="mfilearray[]" multiple></label><br>
<br>
<button>Upload</button>
</form>`
);
} else if (req.url === '/upload') {
var form = new formidable.IncomingForm({multiples: true});

form.uploadDir = os.tmpdir();

form.parse(req, function (error, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received fields:\n\n '+util.inspect(fields));
res.write('\n\n');
res.end('received files:\n\n '+util.inspect(files));
})
} else {
res.writeHead(404, {'content-type': 'text/plain'});
res.end('404');
}
});
server.listen(port);

console.log('listening on http://localhost:'+port+'/');
14 changes: 7 additions & 7 deletions example/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var http = require('http'),
server;

server = http.createServer(function(req, res) {
if (req.url == '/') {
if (req.url === '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/post" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="text" name="data[foo][]"><br>'+
'<input type="submit" value="Submit">'+
'</form>'
`<form action="/post" method="post">
<input type="text" name="title"><br>
<input type="text" name="data[foo][]"><br>
<button>Submit</button>
'</form>`
);
} else if (req.url == '/post') {
} else if (req.url === '/post') {
var form = new formidable.IncomingForm(),
fields = [];

Expand Down
14 changes: 7 additions & 7 deletions example/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ var http = require('http'),
server;

server = http.createServer(function(req, res) {
if (req.url == '/') {
if (req.url === '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
`<form action="/upload" enctype="multipart/form-data" method="post">
<input type="text" name="title"><br>
<input type="file" name="upload" multiple><br>
<button>Upload</button>
</form>`
);
} else if (req.url == '/upload') {
} else if (req.url === '/upload') {
var form = new formidable.IncomingForm(),
files = [],
fields = [];
Expand Down
2 changes: 1 addition & 1 deletion test/legacy/simple/test-incoming-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ test(function parse() {
});

var parseCbOk = function (err, fields, files) {
assert.deepEqual(fields, {field1: 'bar', field2: 'nice'});
assert.deepEqual(fields, {field1: ['foo', 'bar'], field2: 'nice'});
assert.deepEqual(files, {file1: '2', file2: '3'});
};
form.parse(REQ, parseCbOk);
Expand Down
15 changes: 15 additions & 0 deletions test/unit/test-incoming-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var test = require('utest');
var assert = common.assert;
var IncomingForm = common.require('incoming_form').IncomingForm;
var path = require('path');
var Request = require('http').ClientRequest

var form;
test('IncomingForm', {
Expand Down Expand Up @@ -59,6 +60,20 @@ test('IncomingForm', {
ext = path.extname(form._uploadPath('file.aAa'));
assert.equal(ext, '.aAa');
},

'#_Array parameters support': function () {
form = new IncomingForm({multiples: true});
const req = new Request();
req.headers = 'content-type: json; content-length:8'
form.parse(req, function (error, fields, files) {
assert.equal(Array.isArray(fields.a), true);
assert.equal(fields.a[0], 1);
assert.equal(fields.a[1], 2);
})
form.emit('field', 'a[]', 1);
form.emit('field', 'a[]', 2);
form.emit('end');
},
});

function makeHeader(filename) {
Expand Down

0 comments on commit 036a809

Please sign in to comment.