2021-02-25 07:22:24 -05:00
|
|
|
const AbstractMockApi = require('./AbstractMockApi')
|
2020-08-03 07:34:04 -04:00
|
|
|
const { EventEmitter } = require('events')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
class MockV1HistoryApi extends AbstractMockApi {
|
|
|
|
reset() {
|
|
|
|
this.fakeZipCall = 0
|
|
|
|
this.requestedZipPacks = 0
|
|
|
|
this.sentChunks = 0
|
|
|
|
this.events = new EventEmitter()
|
|
|
|
}
|
|
|
|
|
|
|
|
applyRoutes() {
|
|
|
|
this.app.get(
|
2019-05-29 05:21:06 -04:00
|
|
|
'/api/projects/:project_id/version/:version/zip',
|
|
|
|
(req, res, next) => {
|
|
|
|
res.header('content-disposition', 'attachment; name=project.zip')
|
|
|
|
res.header('content-type', 'application/octet-stream')
|
2021-02-25 07:22:24 -05:00
|
|
|
res.send(
|
2020-12-15 05:23:54 -05:00
|
|
|
`Mock zip for ${req.params.project_id} at version ${req.params.version}`
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.get(
|
2019-05-29 05:21:06 -04:00
|
|
|
'/fake-zip-download/:project_id/version/:version',
|
|
|
|
(req, res, next) => {
|
|
|
|
if (!(this.fakeZipCall++ > 0)) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
res.header('content-disposition', 'attachment; name=project.zip')
|
|
|
|
res.header('content-type', 'application/octet-stream')
|
2020-08-03 07:34:04 -04:00
|
|
|
if (req.params.version === '42') {
|
|
|
|
return res.send(
|
2020-12-15 05:23:54 -05:00
|
|
|
`Mock zip for ${req.params.project_id} at version ${req.params.version}`
|
2020-08-03 07:34:04 -04:00
|
|
|
)
|
|
|
|
}
|
2021-02-25 07:22:24 -05:00
|
|
|
const writeChunk = () => {
|
|
|
|
res.write('chunk' + this.sentChunks++)
|
2020-08-03 07:34:04 -04:00
|
|
|
}
|
2021-02-25 07:22:24 -05:00
|
|
|
const writeEvery = interval => {
|
2020-08-03 07:34:04 -04:00
|
|
|
if (req.aborted) return
|
|
|
|
|
|
|
|
// setInterval delays the first run
|
|
|
|
writeChunk()
|
|
|
|
const periodicWrite = setInterval(writeChunk, interval)
|
|
|
|
req.on('aborted', () => clearInterval(periodicWrite))
|
|
|
|
|
|
|
|
const deadLine = setTimeout(() => {
|
|
|
|
clearInterval(periodicWrite)
|
|
|
|
res.end()
|
|
|
|
}, 10 * 1000)
|
|
|
|
res.on('end', () => clearTimeout(deadLine))
|
|
|
|
}
|
|
|
|
if (req.params.version === '100') {
|
|
|
|
return writeEvery(100)
|
|
|
|
}
|
|
|
|
res.sendStatus(400)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.post(
|
2019-05-29 05:21:06 -04:00
|
|
|
'/api/projects/:project_id/version/:version/zip',
|
|
|
|
(req, res, next) => {
|
2021-02-25 07:22:24 -05:00
|
|
|
this.requestedZipPacks++
|
|
|
|
this.events.emit('v1-history-pack-zip')
|
|
|
|
res.json({
|
2020-12-15 05:23:54 -05:00
|
|
|
zipUrl: `http://localhost:3100/fake-zip-download/${req.params.project_id}/version/${req.params.version}`
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
this.app.delete('/api/projects/:project_id', (req, res, next) => {
|
2021-01-14 08:56:36 -05:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:22:24 -05:00
|
|
|
module.exports = MockV1HistoryApi
|
|
|
|
|
|
|
|
// type hint for the inherited `instance` method
|
|
|
|
/**
|
|
|
|
* @function instance
|
|
|
|
* @memberOf MockV1HistoryApi
|
|
|
|
* @static
|
|
|
|
* @returns {MockV1HistoryApi}
|
|
|
|
*/
|