Skip to content

Commit

Permalink
Adds content type parameter for BinaryPart
Browse files Browse the repository at this point in the history
  • Loading branch information
botic committed Aug 11, 2016
1 parent c910f6b commit 7c3ba54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
25 changes: 12 additions & 13 deletions modules/ringo/httpclient.js
Expand Up @@ -627,40 +627,39 @@ var TextPart = function(data, charset, filename) {

/**
* @name BinaryPart
* @param {String} data The data
* @param {String} charset The charset
* @param {String} filename An optional file name
* @param {String} data the data
* @param {String} fileName (optional) file name
* @param {String} contentType (optional) content type of the file
* @returns {BinaryPart} A newly constructed BinaryPart instance
* @constructor
* @example request({
* url: "http://example.org/post-multipart",
* method: "POST",
* contentType: "multipart/form-data",
* data: {
* "simple": new TextPart(title, "utf-8"),
* "txtFile": new TextPart(txtStream, "utf-8", "test.txt"),
* "image": new BinaryPart(binaryStream, "image.png")
* "image": new BinaryPart(binaryStream, "image.png"),
* "document": new BinaryPart(binaryStream, "invoce.doc", "application/msword")
* }
* });
*/
var BinaryPart = function(data, filename) {
var BinaryPart = function(data, fileName, contentType) {

/**
* Writes this BinaryPart's data
* @param {String} name The name of the text part
* @param {java.io.PrintWriter} writer The writer
* @param {java.io.OutputStream} outStream The output stream
* @param {String} name form parameter name of the text part
* @param {java.io.PrintWriter} writer print writer
* @param {java.io.OutputStream} outStream binary output stream
* @ignore
*/
this.write = function(name, writer, outStream) {
writer.append("Content-Disposition: form-data; name=\"")
.append(name).append("\"");
if (filename != null) {
writer.append("; filename=\"").append(filename).append("\"");
if (fileName != null) {
writer.append("; filename=\"").append(fileName).append("\"");
}
writer.append(CRLF);
writer.append("Content-Type: ")
.append(URLConnection.guessContentTypeFromName(filename))
.append(contentType || URLConnection.guessContentTypeFromName(fileName))
.append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Expand Down
27 changes: 27 additions & 0 deletions test/ringo/httpclient_test.js
Expand Up @@ -609,6 +609,33 @@ exports.testBinaryResponse = function() {
assert.strictEqual(exchange.contentBytes.toArray().join(""), [35, 114, 105, 110, 103, 111, 106, 115].join(""));
};

exports.testBinaryPart = function() {
let bin = new BinaryPart(fs.openRaw(module.resolve("./text_test.txt"), "r"), "foo.txt", "text/superplain");
let sw = new java.io.StringWriter();
let bos = new java.io.ByteArrayOutputStream();
bin.write("testname.txt", sw, bos);

let result = sw.toString().split("\r\n");
assert.equal(result[0], "Content-Disposition: form-data; name=\"testname.txt\"; filename=\"foo.txt\"");
assert.equal(result[1], "Content-Type: text/superplain");
assert.equal(result[2], "Content-Transfer-Encoding: binary");

sw.close();
bos.close();

bin = new BinaryPart(fs.openRaw(module.resolve("./text_test.txt"), "r"), "bar.txt");
sw = new java.io.StringWriter();
bos = new java.io.ByteArrayOutputStream();
bin.write("paramname", sw, bos);

result = sw.toString().split("\r\n");
assert.equal(result[0], "Content-Disposition: form-data; name=\"paramname\"; filename=\"bar.txt\"");
assert.equal(result[1], "Content-Type: text/plain");
assert.equal(result[2], "Content-Transfer-Encoding: binary");

sw.close();
bos.close();
};

// start the test runner if we're called directly from command line
if (require.main === module) {
Expand Down

0 comments on commit 7c3ba54

Please sign in to comment.