Skip to content

Commit

Permalink
Adds additional check for multipart form requests
Browse files Browse the repository at this point in the history
  • Loading branch information
botic committed Aug 10, 2016
1 parent 71d4169 commit 36f3bcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions modules/ringo/httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ var Exchange = function(url, options) {
connection.setDoOutput(true);
var charset = getMimeParameter(options.contentType, "charset") || "utf-8";
if (options.method === "POST" && options.contentType === "multipart/form-data") {
// all parts must be instances of text or binary parts
for (let [name, part] in Iterator(reqData)) {
if (!(part instanceof TextPart) && !(part instanceof BinaryPart)) {
throw new Error("Multipart form data parts must be either TextPart or BinaryPart, but " + name + "isn't.");
}
}

writeMultipartData(reqData, connection, charset, generateBoundary());
} else {
writeData(reqData, connection, charset, options.contentType);
Expand Down
12 changes: 12 additions & 0 deletions test/ringo/httpclient_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,18 @@ exports.testPostMultipart = function() {
assert.isNotUndefined(received.image);
assert.strictEqual(received.image.value.length, imageByteArray.length);
assert.deepEqual(received.image.value.toArray(), imageByteArray.toArray());

// invalid request
assert.throws(function() {
request({
url: baseUri,
method: "POST",
contentType: "multipart/form-data",
data: {
"invalid": "Invalid"
}
});
}, Error);
};

exports.testProxiedRequest = function() {
Expand Down

0 comments on commit 36f3bcb

Please sign in to comment.