Skip to content

Commit f1786d5

Browse files
committed
1 parent aa46963 commit f1786d5

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed

services/document_conversion/v1.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,18 @@ DocumentConversion.prototype.conversion_target = {
5454
NORMALIZED_TEXT: 'NORMALIZED_TEXT'
5555
};
5656

57+
// this sets up the content type "headers" in the form/multipart body (not in the actual headers)
5758
function fixupContentType(params) {
58-
if (params.file && params.file.path && /.html?$/.test(params.file.path)) {
59+
if (params.content_type) {
60+
params.file = {
61+
value: params.file,
62+
options: {
63+
contentType: params.content_type
64+
}
65+
};
66+
}
67+
else if (params.file.path && /.html?$/.test(params.file.path)) {
68+
// for HTML, the service requires that a utf-8 charset be specified in the content-type
5969
params.file = {
6070
value: params.file,
6171
options: {
@@ -70,8 +80,11 @@ function fixupContentType(params) {
7080
*
7181
* To convert a previously uploaded document, set params.document_id
7282
*
83+
* @param {Object} params
7384
* @param {Object} params.conversion_target Must be set to one of ['ANSWER_UNITS', 'NORMALIZED_HTML', 'NORMALIZED_TEXT']
7485
* @param {ReadableStream} [params.file] The document file to convert.
86+
* @param {String} [params.content_type] Overrides the default content-type determined from the file name.
87+
* @param {Function} callback
7588
*/
7689
DocumentConversion.prototype.convert = function(params, callback) {
7790
params = params || {};
@@ -108,7 +121,7 @@ DocumentConversion.prototype.convert = function(params, callback) {
108121
parameters.options.formData = {
109122
file: params.file,
110123
config: {
111-
value: JSON.stringify(omit(params,['file'])),
124+
value: JSON.stringify(omit(params,['file', 'content_type'])),
112125
options: {
113126
contentType: 'application/json; charset=utf-8'
114127
}

test/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ module.exports = {
33
"node": true,
44
"mocha": true
55
},
6+
"globals": {
7+
"Promise": false // used in some tests
8+
},
69
"extends": "../.eslintrc.js"
710
}

test/test.document_conversion.v1.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ describe('document_conversion', function() {
3131
]
3232
}
3333
},
34-
file: fs.createReadStream(__dirname + '/resources/sampleWORD.docx'),
34+
file: fs.createReadStream(__dirname + '/resources/sampleWord.docx'),
3535
};
3636

3737
before(function() {
38-
nock.disableNetConnect();
38+
nock.disableNetConnect(); // for running tests
39+
// or
40+
//nock.recorder.rec(); // for creating tests
3941
});
4042

4143
after(function() {
@@ -74,12 +76,74 @@ describe('document_conversion', function() {
7476

7577
it('should generate a valid payload', function() {
7678
var req = servInstance.convert(payload, noop);
79+
assert(req);
7780
var url = service_options.url + convertPath;
7881
assert.equal(req.uri.href.slice(0, url.length), url);
7982
assert.equal(req.method, 'POST');
8083
assert(req.formData);
8184
});
8285

86+
function checkContentType(params, contentType) {
87+
return new Promise(function(resolve, reject) {
88+
// the file content-type is in the body for form/multipart POST requests
89+
// so we're having nock intercept the request, check the body, then send a fake response
90+
var expectation = nock('http://ibm.com:80')
91+
.post('/v1/convert_document?version=2015-12-01', function(body) {
92+
var re = new RegExp('Content-Type: ' + contentType);
93+
return re.exec(body) || re.exec(Buffer.from(body, 'hex').toString());
94+
})
95+
.reply(201, '');
96+
97+
servInstance.convert(params, function(err) {
98+
if (err) {
99+
return reject(err);
100+
}
101+
assert(expectation.isDone());
102+
resolve();
103+
});
104+
});
105+
}
106+
107+
it('should set a default content type based on the file extension', function() {
108+
return checkContentType({
109+
conversion_target: 'ANSWER_UNITS',
110+
file: fs.createReadStream(__dirname + '/resources/sampleWord.docx')
111+
}, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
112+
});
113+
114+
it('should allow the content type to be manually set', function() {
115+
return checkContentType({
116+
conversion_target: 'ANSWER_UNITS',
117+
file: fs.createReadStream(__dirname + '/resources/sampleWordWrongExtension.html'),
118+
content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
119+
}, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
120+
});
121+
122+
// be default, request sets the content-type, but not the charset. the service requires both for html,
123+
// and only accepts utf-8
124+
it('should add the charset to the content-type for .htm files', function() {
125+
return checkContentType({
126+
conversion_target: 'ANSWER_UNITS',
127+
file: fs.createReadStream(__dirname + '/resources/sampleHtml.htm')
128+
}, 'text/html; charset=utf-8');
129+
});
130+
131+
// same as above, except with .html instead of .htm
132+
it('should add the charset to the content-type for .html files', function() {
133+
return checkContentType({
134+
conversion_target: 'ANSWER_UNITS',
135+
file: fs.createReadStream(__dirname + '/resources/sampleHtml.html')
136+
}, 'text/html; charset=utf-8');
137+
});
138+
139+
it('should not override the user-set content-type for html files', function() {
140+
return checkContentType({
141+
conversion_target: 'ANSWER_UNITS',
142+
file: fs.createReadStream(__dirname + '/resources/sampleHtml.htm'),
143+
content_type: 'text/plain'
144+
}, 'text/plain');
145+
});
146+
83147
it('should send extra config params', function() {
84148
var req = servInstance.convert(payload, noop);
85149
var config = JSON.parse(req.formData.config.value);

test/test.integration-all-services.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,11 @@ describe(MAX_RETRIES, 'integration-all-services', function() {
620620
describe('functional_document_conversion', function() {
621621
this.timeout(TWENTY_SECONDS);
622622
describe('v1', function() {
623+
var document_conversion = watson.document_conversion(auth.document_conversion);
624+
623625
it('convertFile()', function(done) {
624-
var document_conversion = watson.document_conversion(auth.document_conversion);
625626
document_conversion.convert({
626-
file: fs.createReadStream(__dirname + '/resources/sampleWORD.docx'),
627+
file: fs.createReadStream(__dirname + '/resources/sampleWord.docx'),
627628
conversion_target: 'ANSWER_UNITS',
628629
// word: {
629630
// heading: {
@@ -635,6 +636,14 @@ describe(MAX_RETRIES, 'integration-all-services', function() {
635636
// }
636637
}, failIfError.bind(failIfError, done));
637638
});
639+
640+
it('convertFile() with overridden content-type', function(done) {
641+
document_conversion.convert({
642+
conversion_target: 'ANSWER_UNITS',
643+
file: fs.createReadStream(__dirname + '/resources/sampleWordWrongExtension.html'),
644+
content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
645+
}, failIfError.bind(failIfError, done));
646+
})
638647
});
639648
});
640649
});

0 commit comments

Comments
 (0)