Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 47038c1

Browse files
committed
test: Add test for upload() api function
This was a real pain in the arse to figure out, mostly due to cy.request() not yet having support for 'multipart/form-data' content. At least there's a functional workaround.
1 parent b148e4e commit 47038c1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

cypress/e2e/2-api/api.cy.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,46 @@ describe('api tests', () => {
443443
)
444444
})
445445

446+
// Upload
447+
// Equivalent curl command:
448+
// curl -k -F apikey="2MXwA5jGZkIQ3UNEcKsuDNSPMlx" \
449+
// -F dbname="Assembly Election 2017v2.sqlite" \
450+
// -F file=@../../test_data/Assembly\ Election\ 2017.sqlite \
451+
// https://localhost:9444/v1/upload
452+
it('upload', () => {
453+
cy.readFile('cypress/test_data/Assembly Election 2017.sqlite', 'binary').then((dbData) => {
454+
const blob = Cypress.Blob.binaryStringToBlob(dbData)
455+
456+
// Manually construct a form data object, as cy.request() doesn't yet have proper support
457+
// for form data
458+
const z = new FormData()
459+
z.set('apikey', '2MXwA5jGZkIQ3UNEcKsuDNSPMlx')
460+
z.set('dbname', 'Assembly Election 2017v2.sqlite')
461+
z.set('file', blob)
462+
463+
// Send the request
464+
cy.request({
465+
method: 'POST',
466+
url: 'https://localhost:9444/v1/upload',
467+
body: z
468+
}).then(
469+
(response) => {
470+
expect(response.status).to.eq(201)
471+
472+
// For some unknown reason Cypress thinks the response.body is an ArrayBuffer (wtf?), when it's just standard
473+
// json. It's *probably* some side effect of using Cypress.Blob.binaryStringToBlob() above, but that seems
474+
// pretty silly.
475+
// Anyway, we manually convert it to something that JSON.parse() can operate on, then proceed as per normal
476+
let fixedBody = Cypress.Blob.arrayBufferToBinaryString(response.body)
477+
let jsonBody = JSON.parse(fixedBody)
478+
479+
expect(jsonBody).to.have.keys(['commit', 'url'])
480+
expect(jsonBody.url).to.match(/.*\/default\/Assembly\ Election\ 2017v2\.sqlite/)
481+
}
482+
)
483+
})
484+
})
485+
446486
// Webpage
447487
// Equivalent curl command:
448488
// curl -k -F apikey="2MXwA5jGZkIQ3UNEcKsuDNSPMlx" \

0 commit comments

Comments
 (0)