diff --git a/packages/sdk-middleware-http/package.json b/packages/sdk-middleware-http/package.json index 4c951abf5..91733ad5d 100644 --- a/packages/sdk-middleware-http/package.json +++ b/packages/sdk-middleware-http/package.json @@ -32,6 +32,8 @@ "build:bundles": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js -n CommercetoolsSdkMiddlewareHttp -i ./src/index.js" }, "devDependencies": { + "@commercetools/sdk-client": "^2.1.2", + "@commercetools/sdk-middleware-queue": "^2.1.4", "abort-controller": "3.0.0", "nock": "12.0.3", "node-fetch": "2.6.1" diff --git a/packages/sdk-middleware-http/test/http.spec.js b/packages/sdk-middleware-http/test/http.spec.js index 2505b1dd8..1dcddf7f7 100644 --- a/packages/sdk-middleware-http/test/http.spec.js +++ b/packages/sdk-middleware-http/test/http.spec.js @@ -2,6 +2,8 @@ import nock from 'nock' import fetch from 'node-fetch' import AbortController from 'abort-controller' +import { createQueueMiddleware } from '@commercetools/sdk-middleware-queue' +import { createClient } from '@commercetools/sdk-client' import { createHttpMiddleware } from '../src' function createTestRequest(options) { @@ -1116,4 +1118,45 @@ describe('Http', () => { httpMiddleware(next)(request, response) })) + + test('queueMiddleware with timeout', async () => { + expect.assertions(20) + const queueMiddleware = createQueueMiddleware({ + concurrency: 10, + }) + + const httpMiddleware = createHttpMiddleware({ + host: testHost, + timeout: 1000, + fetch, + getAbortController: () => new AbortController(), + }) + + const ctpClient = createClient({ + middlewares: [queueMiddleware, httpMiddleware], + }) + + const request = createTestRequest({ + uri: '/foo/bar', + }) + + nock(testHost) + .defaultReplyHeaders({ + 'Content-Type': 'application/json', + }) + .get('/foo/bar') + .times(20) + .delay(1500) // delay response with 1.5 s + .reply(200, { foo: 'bar' }) + + await Promise.all( + [...Array(20)].map(async () => { + try { + await ctpClient.execute(request) + } catch (err) { + expect(err.message).toEqual('The user aborted a request.') + } + }) + ) + }) })