@@ -31,11 +31,13 @@ describe('document_conversion', function() {
31
31
]
32
32
}
33
33
} ,
34
- file : fs . createReadStream ( __dirname + '/resources/sampleWORD .docx' ) ,
34
+ file : fs . createReadStream ( __dirname + '/resources/sampleWord .docx' ) ,
35
35
} ;
36
36
37
37
before ( function ( ) {
38
- nock . disableNetConnect ( ) ;
38
+ nock . disableNetConnect ( ) ; // for running tests
39
+ // or
40
+ //nock.recorder.rec(); // for creating tests
39
41
} ) ;
40
42
41
43
after ( function ( ) {
@@ -74,12 +76,74 @@ describe('document_conversion', function() {
74
76
75
77
it ( 'should generate a valid payload' , function ( ) {
76
78
var req = servInstance . convert ( payload , noop ) ;
79
+ assert ( req ) ;
77
80
var url = service_options . url + convertPath ;
78
81
assert . equal ( req . uri . href . slice ( 0 , url . length ) , url ) ;
79
82
assert . equal ( req . method , 'POST' ) ;
80
83
assert ( req . formData ) ;
81
84
} ) ;
82
85
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
+
83
147
it ( 'should send extra config params' , function ( ) {
84
148
var req = servInstance . convert ( payload , noop ) ;
85
149
var config = JSON . parse ( req . formData . config . value ) ;
0 commit comments