|
| 1 | +import type { Server } from 'http'; |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import type { Express } from 'express'; |
| 5 | +import express from 'express'; |
| 6 | + |
| 7 | +import { setupServer } from '.'; |
| 8 | + |
| 9 | +const raoState: Record< |
| 10 | + string, |
| 11 | + { |
| 12 | + tmpIndexName: string; |
| 13 | + successful: boolean; |
| 14 | + } |
| 15 | +> = {}; |
| 16 | + |
| 17 | +export function assertValidReplaceAllObjectsFailed(expectedCount: number): void { |
| 18 | + const count = Object.values(raoState).filter((s) => s.successful).length; |
| 19 | + if (count !== expectedCount) { |
| 20 | + throw new Error(`Expected ${expectedCount} call to replaceAllObjectsFailed, got ${count} instead.`); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function addRoutes(app: Express): void { |
| 25 | + app.use(express.urlencoded({ extended: true })); |
| 26 | + app.use( |
| 27 | + express.json({ |
| 28 | + type: ['application/json', 'text/plain'], // the js client sends the body as text/plain |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + app.post('/1/indexes/:indexName/operation', (req, res) => { |
| 33 | + const lang = req.params.indexName.match(/^cts_e2e_replace_all_objects_too_big_(.*)$/)?.[1] as string; |
| 34 | + raoState[lang] = { |
| 35 | + tmpIndexName: req.body.destination, |
| 36 | + successful: false, |
| 37 | + }; |
| 38 | + |
| 39 | + res.json({ taskID: 123, updatedAt: '2021-01-01T00:00:00.000Z' }); |
| 40 | + }); |
| 41 | + |
| 42 | + app.post('/1/indexes/:indexName/batch', (_req, res) => { |
| 43 | + res.status(400).json({ message: 'Record is too big', status: 400 }); |
| 44 | + }); |
| 45 | + |
| 46 | + app.delete('/1/indexes/:indexName', (req, res) => { |
| 47 | + const lang = req.params.indexName.match(/^cts_e2e_replace_all_objects_too_big_(.*)_tmp/)?.[1] as string; |
| 48 | + expect(raoState[lang].tmpIndexName).to.equal(req.params.indexName); |
| 49 | + raoState[lang].successful = true; |
| 50 | + |
| 51 | + res.json({ taskID: 456, deletedAt: '2021-01-01T00:00:00.000Z' }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +export function replaceAllObjectsServerFailed(): Promise<Server> { |
| 56 | + // this server is used to simulate the responses for the replaceAllObjects method, with cleanup |
| 57 | + return setupServer('replaceAllObjectsFailed', 6684, addRoutes); |
| 58 | +} |
0 commit comments