From fcbed667853d23046aea367c483d203bd8d4810e Mon Sep 17 00:00:00 2001 From: Matt Loar Date: Sat, 1 Jun 2013 21:46:29 -0700 Subject: [PATCH 1/3] Fix Data object support. --- lib/multipartform.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/multipartform.js b/lib/multipartform.js index e917676..f72eb06 100644 --- a/lib/multipartform.js +++ b/lib/multipartform.js @@ -68,7 +68,7 @@ Part.prototype = { header = "Content-Disposition: form-data; name=\"" + this.name + "\"; filename=\"" + this.value.filename + "\"\r\n" + "Content-Type: " + this.value.contentType; - } if (this.value instanceof File) { + } else if (this.value instanceof File) { header = "Content-Disposition: form-data; name=\"" + this.name + "\"; filename=\"" + this.value.filename + "\"\r\n" + "Content-Length: " + this.value.fileSize + "\r\n" + @@ -127,6 +127,9 @@ Part.prototype = { }); })(); // reader() }); + } else if (this.value instanceof Data) { + stream.write(this.value.data + "\r\n"); + callback(); } else { stream.write(this.value + "\r\n"); callback(); From 85d7862a82e8e9d59434c3098cbe5629e1a85603 Mon Sep 17 00:00:00 2001 From: Matt Loar Date: Sat, 1 Jun 2013 22:36:17 -0700 Subject: [PATCH 2/3] Add Content-Length and properly handle Buffers. --- lib/multipartform.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/multipartform.js b/lib/multipartform.js index f72eb06..7196724 100644 --- a/lib/multipartform.js +++ b/lib/multipartform.js @@ -67,6 +67,7 @@ Part.prototype = { if (this.value.data) { header = "Content-Disposition: form-data; name=\"" + this.name + "\"; filename=\"" + this.value.filename + "\"\r\n" + + "Content-Length: " + this.value.data.length + "\r\n" + "Content-Type: " + this.value.contentType; } else if (this.value instanceof File) { header = "Content-Disposition: form-data; name=\"" + this.name + @@ -128,7 +129,8 @@ Part.prototype = { })(); // reader() }); } else if (this.value instanceof Data) { - stream.write(this.value.data + "\r\n"); + stream.write(this.value.data); + stream.write("\r\n"); callback(); } else { stream.write(this.value + "\r\n"); From be34cbc428f33a91b03b0997eb7a68e1830e612b Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Wed, 23 Apr 2014 17:18:08 +0200 Subject: [PATCH 3/3] Add test case for mloar's PR danwrong/restler#119. --- test/restler.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/restler.js b/test/restler.js index d2c64c0..43b5320 100644 --- a/test/restler.js +++ b/test/restler.js @@ -294,7 +294,26 @@ module.exports['Multipart'] = { test.done(); }); - } + }, + + 'Test multipart request with Data vars': function(test) { + rest.post(host, { + data: { + a: 10, + b: rest.data('b.txt', 'text/plain', 'thing'), + c: rest.data('c.txt', 'text/plain', new Buffer('thing')) + }, + multipart: true + }).on('complete', function(data) { + test.re(data, /content-type\: multipart\/form-data/, 'should set "content-type" header'); + test.re(data, /name="a"(\s)+10/, 'should send a=10'); + test.re(data, /name="b"; filename="b.txt"\s+Content-Length: 5\s+Content-Type: text\/plain\s+thing\s/, 'should send b=thing'); + test.re(data, /name="c"; filename="c.txt"\s+Content-Length: 5\s+Content-Type: text\/plain\s+thing\s/, 'should send c=thing'); + test.re(data, /content-length: 410/, 'should send content-length header'); + + test.done(); + }); + }, };