Skip to content

Commit 40426d1

Browse files
Samuel JeffressKhaledgarbaya
authored andcommitted
fix(createUpload): Setting ContentType
ContentType for createUpload was previously hardcoded to applicaion/octet-stream This fix uses the contentType parameter for uploading a file Closes #104
1 parent 3eb3240 commit 40426d1

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/create-space-api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,13 @@ export default function createSpaceApi ({
585585
* space.createUpload(uploadStream)
586586
*/
587587
function createUpload (data) {
588-
const { file } = data
588+
const { file, contentType } = data
589589
if (!file) {
590590
return Promise.reject(new Error('Unable to locate a file to upload.'))
591591
}
592592
return httpUpload.post('uploads', file, {
593593
headers: {
594-
'Content-Type': 'application/octet-stream'
594+
'Content-Type': contentType || 'application/octet-stream'
595595
}
596596
})
597597
.then(uploadResponse => {

test/unit/create-space-api-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,29 @@ test('API call createUpload', (t) => {
374374
file: '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>'
375375
})
376376
.then(() => {
377+
t.equals(httpUploadMock.post.args[0][2].headers['Content-Type'], 'image/svg')
378+
t.equals(httpUploadMock.post.args[0][1], '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>', 'uploads file to upload endpoint')
379+
t.deepEqual(entitiesMock.upload.wrapUpload.args[0][1], mockedUpload, 'wrapUpload was called with correct raw upload object')
380+
})
381+
})
382+
383+
test('API call createUpload defaults the content type to octet-stream', (t) => {
384+
const { api, httpUploadMock, entitiesMock } = setup(Promise.resolve({}))
385+
const mockedUpload = {
386+
sys: {
387+
id: 'some_random_id'
388+
}
389+
}
390+
httpUploadMock.post.returns(Promise.resolve({
391+
data: mockedUpload
392+
}))
393+
394+
return api.createUpload({ // no contentType set here
395+
fileName: 'filename.svg',
396+
file: '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>'
397+
})
398+
.then(() => {
399+
t.equals(httpUploadMock.post.args[0][2].headers['Content-Type'], 'application/octet-stream')
377400
t.equals(httpUploadMock.post.args[0][1], '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>', 'uploads file to upload endpoint')
378401
t.deepEqual(entitiesMock.upload.wrapUpload.args[0][1], mockedUpload, 'wrapUpload was called with correct raw upload object')
379402
})

0 commit comments

Comments
 (0)