@labdigital/commercetools-mock 1.11.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -36,10 +36,11 @@ __export(src_exports, {
36
36
  module.exports = __toCommonJS(src_exports);
37
37
 
38
38
  // src/ctMock.ts
39
- var import_nock = __toESM(require("nock"), 1);
40
39
  var import_express6 = __toESM(require("express"), 1);
41
40
  var import_supertest = __toESM(require("supertest"), 1);
42
41
  var import_morgan = __toESM(require("morgan"), 1);
42
+ var import_node = require("msw/node");
43
+ var import_msw = require("msw");
43
44
 
44
45
  // src/storage/abstract.ts
45
46
  var AbstractStorage = class {
@@ -1415,19 +1416,19 @@ var ProjectAPI = class {
1415
1416
 
1416
1417
  // src/lib/proxy.ts
1417
1418
  var copyHeaders = (headers) => {
1418
- const validHeaders = ["accept", "host", "authorization"];
1419
+ const validHeaders = ["accept", "host", "authorization", "content-type"];
1419
1420
  const result = {};
1420
- Object.entries(headers).forEach(([key, value]) => {
1421
+ for (const [key, value] of headers.entries()) {
1421
1422
  if (validHeaders.includes(key.toLowerCase())) {
1422
1423
  result[key] = value;
1423
1424
  }
1424
- });
1425
+ }
1425
1426
  return result;
1426
1427
  };
1427
1428
 
1428
1429
  // src/constants.ts
1429
- var DEFAULT_API_HOSTNAME = /^https:\/\/api\..*?\.commercetools.com:443$/;
1430
- var DEFAULT_AUTH_HOSTNAME = /^https:\/\/auth\..*?\.commercetools.com:443$/;
1430
+ var DEFAULT_API_HOSTNAME = "https://api.*.commercetools.com";
1431
+ var DEFAULT_AUTH_HOSTNAME = "https://auth.*.commercetools.com";
1431
1432
 
1432
1433
  // src/repositories/helpers.ts
1433
1434
  var import_uuid2 = require("uuid");
@@ -6402,12 +6403,13 @@ var DEFAULT_OPTIONS = {
6402
6403
  authHost: DEFAULT_AUTH_HOSTNAME,
6403
6404
  silent: false
6404
6405
  };
6406
+ var _globalListeners = [];
6405
6407
  var CommercetoolsMock = class {
6406
6408
  app;
6407
6409
  options;
6408
6410
  _storage;
6409
6411
  _oauth2;
6410
- _nockScopes = { auth: void 0, api: void 0 };
6412
+ _mswServer = void 0;
6411
6413
  _services;
6412
6414
  _repositories;
6413
6415
  _projectService;
@@ -6424,16 +6426,15 @@ var CommercetoolsMock = class {
6424
6426
  this.app = this.createApp({ silent: this.options.silent });
6425
6427
  }
6426
6428
  start() {
6427
- this.mockAuthHost();
6428
- this.mockApiHost();
6429
+ this.clear();
6430
+ this.startServer();
6429
6431
  }
6430
6432
  stop() {
6431
- this._nockScopes.auth?.persist(false);
6432
- this._nockScopes.auth = void 0;
6433
- this._nockScopes.api?.persist(false);
6434
- this._nockScopes.api = void 0;
6433
+ this._mswServer?.close();
6434
+ this._mswServer = void 0;
6435
6435
  }
6436
6436
  clear() {
6437
+ this._mswServer?.resetHandlers();
6437
6438
  this._storage.clear();
6438
6439
  }
6439
6440
  project(projectKey) {
@@ -6496,25 +6497,65 @@ var CommercetoolsMock = class {
6496
6497
  });
6497
6498
  return app;
6498
6499
  }
6499
- mockApiHost() {
6500
- const app = this.app;
6501
- this._nockScopes.api = (0, import_nock.default)(this.options.apiHost).persist().get(/.*/).reply(async function(uri) {
6502
- const response = await (0, import_supertest.default)(app).get(uri).set(copyHeaders(this.req.headers));
6503
- return [response.status, response.body];
6504
- }).post(/.*/).reply(async function(uri, body) {
6505
- const response = await (0, import_supertest.default)(app).post(uri).set(copyHeaders(this.req.headers)).send(body);
6506
- return [response.status, response.body];
6507
- }).delete(/.*/).reply(async function(uri, body) {
6508
- const response = await (0, import_supertest.default)(app).delete(uri).set(copyHeaders(this.req.headers)).send(body);
6509
- return [response.status, response.body];
6510
- });
6511
- }
6512
- mockAuthHost() {
6500
+ startServer() {
6501
+ if (_globalListeners.length > 0) {
6502
+ if (this._mswServer !== void 0) {
6503
+ throw new Error("Server already started");
6504
+ } else {
6505
+ console.warn("Server wasn't stopped properly, clearing");
6506
+ _globalListeners.forEach((listener) => listener.close());
6507
+ }
6508
+ }
6513
6509
  const app = this.app;
6514
- this._nockScopes.auth = (0, import_nock.default)(this.options.authHost).persist().post(/^\/oauth\/.*/).reply(async function(uri, body) {
6515
- const response = await (0, import_supertest.default)(app).post(uri + "?" + body).set(copyHeaders(this.req.headers)).send();
6516
- return [response.status, response.body];
6510
+ this._mswServer = (0, import_node.setupServer)(
6511
+ import_msw.http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
6512
+ const text = await request.text();
6513
+ const url = new URL(request.url);
6514
+ const res = await (0, import_supertest.default)(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(text);
6515
+ return new import_msw.HttpResponse(res.text, {
6516
+ status: res.status,
6517
+ headers: res.headers
6518
+ });
6519
+ }),
6520
+ import_msw.http.get(`${this.options.apiHost}/*`, async ({ request }) => {
6521
+ const body = await request.text();
6522
+ const url = new URL(request.url);
6523
+ const res = await (0, import_supertest.default)(app).get(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6524
+ return new import_msw.HttpResponse(res.text, {
6525
+ status: res.status,
6526
+ headers: res.headers
6527
+ });
6528
+ }),
6529
+ import_msw.http.post(`${this.options.apiHost}/*`, async ({ request }) => {
6530
+ const body = await request.text();
6531
+ const url = new URL(request.url);
6532
+ const res = await (0, import_supertest.default)(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6533
+ return new import_msw.HttpResponse(res.text, {
6534
+ status: res.status,
6535
+ headers: res.headers
6536
+ });
6537
+ }),
6538
+ import_msw.http.delete(`${this.options.apiHost}/*`, async ({ request }) => {
6539
+ const body = await request.text();
6540
+ const url = new URL(request.url);
6541
+ const res = await (0, import_supertest.default)(app).delete(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6542
+ return new import_msw.HttpResponse(res.text, {
6543
+ status: res.status,
6544
+ headers: res.headers
6545
+ });
6546
+ })
6547
+ );
6548
+ this._mswServer.listen({
6549
+ // We need to allow requests done by supertest
6550
+ onUnhandledRequest: (request, print) => {
6551
+ const url = new URL(request.url);
6552
+ if (url.hostname === "127.0.0.1") {
6553
+ return;
6554
+ }
6555
+ print.error();
6556
+ }
6517
6557
  });
6558
+ _globalListeners.push(this._mswServer);
6518
6559
  }
6519
6560
  };
6520
6561
  // Annotate the CommonJS export names for ESM import in node: