Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(POST /api/file): REST API endpoint returns json content-type #3463

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/server/modules/blobstorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,12 @@ export const init: SpeckleModule['init'] = async (app) => {
)
)

const status = 400
const response = 'Upload request error. The server logs may have more details.'
res.status(status).end(response)
res.contentType('application/json')
res
.status(400)
.end(
'{ "error": "Upload request error. The server logs may have more details." }'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have implications for the frontend?

)
})

req.pipe(busboy)
Expand Down
4 changes: 3 additions & 1 deletion packages/server/modules/core/rest/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ export default (app: Router) => {
'Error during upload. Error occurred after {elapsedTimeMs}ms. Objects processed before error: {totalObjectsProcessed}. Error: {error}'
)
if (!requestDropped)
res.status(400).end('Upload request error. The server logs have more details')
res
.status(400)
.end('{"error": "Upload request error. The server logs have more details."}')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this change have implications for the frontend?

requestDropped = true
})

Expand Down
1 change: 1 addition & 0 deletions packages/server/modules/fileuploads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const init: SpeckleModule['init'] = async (app, isInitial) => {
'Error while uploading file.'
)
}
res.contentType('application/json')
res.status(response.statusCode).send(body)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ describe('FileUploads @fileuploads', () => {
.attach('test.ifc', require.resolve('@/readme.md'), 'test.ifc')

expect(response.statusCode).to.equal(201)
expect(response.body).to.deep.equal({})
expect(response.headers['content-type']).to.contain('application/json;')
expect(response.body.uploadResults).to.have.lengthOf(1)
expect(response.body.uploadResults[0].fileName).to.equal('test.ifc')
const gqlResponse = await sendRequest(userOneToken, {
query: `query ($streamId: String!) {
stream(id: $streamId) {
Expand All @@ -206,6 +208,9 @@ describe('FileUploads @fileuploads', () => {
expect(noErrors(gqlResponse))
expect(gqlResponse.body.data.stream.fileUploads).to.have.lengthOf(1)
expect(gqlResponse.body.data.stream.fileUploads[0].fileName).to.equal('test.ifc')
expect(gqlResponse.body.data.stream.fileUploads[0].id).to.equal(
response.body.uploadResults[0].blobId
)

//TODO expect subscription notification
})
Expand All @@ -217,7 +222,11 @@ describe('FileUploads @fileuploads', () => {
.attach('blob1', require.resolve('@/readme.md'), 'test1.ifc')
.attach('blob2', require.resolve('@/package.json'), 'test2.ifc')
expect(response.status).to.equal(201)
expect(response.body).to.deep.equal({})
expect(response.headers['content-type']).to.contain('application/json;')
expect(response.body.uploadResults).to.have.lengthOf(2)
expect(
response.body.uploadResults.map((file: { fileName: string }) => file.fileName)
).to.have.members(['test1.ifc', 'test2.ifc'])
const gqlResponse = await sendRequest(userOneToken, {
query: `query ($streamId: String!) {
stream(id: $streamId) {
Expand All @@ -241,6 +250,11 @@ describe('FileUploads @fileuploads', () => {
(file: { fileName: string }) => file.fileName
)
).to.have.members(['test1.ifc', 'test2.ifc'])
expect(
gqlResponse.body.data.stream.fileUploads.map((file: { id: string }) => file.id)
).to.have.members(
response.body.uploadResults.map((file: { blobId: string }) => file.blobId)
)
})

it('Returns 400 for bad form data', async () => {
Expand All @@ -252,6 +266,8 @@ describe('FileUploads @fileuploads', () => {
.send('--XXX\r\nCon')

expect(response.status).to.equal(400)
expect(response.headers['content-type']).to.contain('application/json;')
expect(response.body.error).to.contain('Upload request error.')
const gqlResponse = await sendRequest(userOneToken, {
query: `query ($streamId: String!) {
stream(id: $streamId) {
Expand All @@ -278,8 +294,11 @@ describe('FileUploads @fileuploads', () => {
.set('Authorization', `Bearer ${userOneToken}`)
.attach('toolarge.ifc', Buffer.alloc(114_857_601, 'asdf'), 'toolarge.ifc')
expect(response.status).to.equal(201)

expect(response.body).to.deep.equal({})
expect(response.headers['content-type']).to.contain('application/json;')
expect(response.body.uploadResults).to.have.lengthOf(1)
expect(
response.body.uploadResults.map((file: { fileName: string }) => file.fileName)
).to.have.members(['toolarge.ifc'])
const gqlResponse = await sendRequest(userOneToken, {
query: `query ($streamId: String!) {
stream(id: $streamId) {
Expand All @@ -298,6 +317,9 @@ describe('FileUploads @fileuploads', () => {
gqlResponse.body.data.stream.fileUploads,
JSON.stringify(gqlResponse.body.data)
).to.have.lengthOf(1)
expect(gqlResponse.body.data.stream.fileUploads[0].id).to.equal(
response.body.uploadResults[0].blobId
)
//TODO expect no notifications
})

Expand Down