@labdigital/commercetools-mock 2.1.0 → 2.3.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "2.1.0",
4
+ "version": "2.3.0",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
@@ -37,10 +37,10 @@
37
37
  "body-parser": "^1.20.2",
38
38
  "deep-equal": "^2.2.2",
39
39
  "express": "^4.18.2",
40
+ "light-my-request": "^5.11.0",
40
41
  "lodash.isequal": "^4.5.0",
41
42
  "morgan": "^1.10.0",
42
43
  "msw": "^2.0.0",
43
- "supertest": "^6.3.3",
44
44
  "uuid": "^9.0.0"
45
45
  },
46
46
  "devDependencies": {
@@ -67,6 +67,7 @@
67
67
  "got": "^11.8.3",
68
68
  "husky": "^8.0.3",
69
69
  "prettier": "^3.0.0",
70
+ "supertest": "^6.3.3",
70
71
  "timekeeper": "^2.3.1",
71
72
  "ts-node": "^10.9.1",
72
73
  "tslib": "^2.6.1",
package/src/ctMock.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import express, { NextFunction, Request, Response } from 'express'
2
- import supertest from 'supertest'
3
2
  import morgan from 'morgan'
3
+ import inject from 'light-my-request'
4
4
  import { setupServer, SetupServer } from 'msw/node'
5
5
  import { http, HttpResponse } from 'msw'
6
6
  import { AbstractStorage, InMemoryStorage } from './storage/index.js'
@@ -16,6 +16,7 @@ import { ProjectService } from './services/project.js'
16
16
  import { createRepositories, RepositoryMap } from './repositories/index.js'
17
17
  import { createServices } from './services/index.js'
18
18
  import { ProjectRepository } from './repositories/project.js'
19
+ import { mapHeaderType } from './helpers.js'
19
20
 
20
21
  export type CommercetoolsMockOptions = {
21
22
  validateCredentials: boolean
@@ -175,56 +176,66 @@ export class CommercetoolsMock {
175
176
  }
176
177
  }
177
178
 
178
- const app = this.app
179
+ const server = this.app
179
180
  this._mswServer = setupServer(
180
181
  http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
181
- const text = await request.text()
182
+ const body = await request.text()
182
183
  const url = new URL(request.url)
183
- const res = await supertest(app)
184
- .post(url.pathname + '?' + url.searchParams.toString())
185
- .set(copyHeaders(request.headers))
186
- .send(text)
184
+ const headers = copyHeaders(request.headers)
187
185
 
188
- return new HttpResponse(res.text, {
189
- status: res.status,
190
- headers: res.headers,
186
+ const res = await inject(server)
187
+ .post(url.pathname + '?' + url.searchParams.toString())
188
+ .body(body)
189
+ .headers(headers)
190
+ .end()
191
+ return new HttpResponse(res.body, {
192
+ status: res.statusCode,
193
+ headers: mapHeaderType(res.headers),
191
194
  })
192
195
  }),
193
196
  http.get(`${this.options.apiHost}/*`, async ({ request }) => {
194
197
  const body = await request.text()
195
198
  const url = new URL(request.url)
196
- const res = await supertest(app)
199
+ const headers = copyHeaders(request.headers)
200
+
201
+ const res = await inject(server)
197
202
  .get(url.pathname + '?' + url.searchParams.toString())
198
- .set(copyHeaders(request.headers))
199
- .send(body)
200
- return new HttpResponse(res.text, {
201
- status: res.status,
202
- headers: res.headers,
203
+ .body(body)
204
+ .headers(headers)
205
+ .end()
206
+ return new HttpResponse(res.body, {
207
+ status: res.statusCode,
208
+ headers: mapHeaderType(res.headers),
203
209
  })
204
210
  }),
205
211
  http.post(`${this.options.apiHost}/*`, async ({ request }) => {
206
212
  const body = await request.text()
207
213
  const url = new URL(request.url)
208
- const res = await supertest(app)
214
+ const headers = copyHeaders(request.headers)
215
+
216
+ const res = await inject(server)
209
217
  .post(url.pathname + '?' + url.searchParams.toString())
210
- .set(copyHeaders(request.headers))
211
- .send(body)
212
- return new HttpResponse(res.text, {
213
- status: res.status,
214
- headers: res.headers,
218
+ .body(body)
219
+ .headers(headers)
220
+ .end()
221
+ return new HttpResponse(res.body, {
222
+ status: res.statusCode,
223
+ headers: mapHeaderType(res.headers),
215
224
  })
216
225
  }),
217
226
  http.delete(`${this.options.apiHost}/*`, async ({ request }) => {
218
227
  const body = await request.text()
219
228
  const url = new URL(request.url)
220
- const res = await supertest(app)
221
- .delete(url.pathname + '?' + url.searchParams.toString())
222
- .set(copyHeaders(request.headers))
223
- .send(body)
229
+ const headers = copyHeaders(request.headers)
224
230
 
225
- return new HttpResponse(res.text, {
226
- status: res.status,
227
- headers: res.headers,
231
+ const res = await inject(server)
232
+ .delete(url.pathname + '?' + url.searchParams.toString())
233
+ .body(body)
234
+ .headers(headers)
235
+ .end()
236
+ return new HttpResponse(res.body, {
237
+ status: res.statusCode,
238
+ headers: mapHeaderType(res.headers),
228
239
  })
229
240
  })
230
241
  )
package/src/helpers.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { OutgoingHttpHeaders } from 'node:http'
1
2
  import { ParsedQs } from 'qs'
2
3
  import { v4 as uuidv4 } from 'uuid'
3
4
 
@@ -59,3 +60,20 @@ export const queryParamsValue = (
59
60
  }
60
61
 
61
62
  export const cloneObject = <T>(o: T): T => JSON.parse(JSON.stringify(o))
63
+
64
+ export const mapHeaderType = (
65
+ outgoingHttpHeaders: OutgoingHttpHeaders
66
+ ): HeadersInit => {
67
+ const headersInit: HeadersInit = {}
68
+ for (const key in outgoingHttpHeaders) {
69
+ const value = outgoingHttpHeaders[key]
70
+ if (Array.isArray(value)) {
71
+ // Join multiple values for the same header with a comma
72
+ headersInit[key] = value.join(', ')
73
+ } else if (value !== undefined) {
74
+ // Single value or undefined
75
+ headersInit[key] = value.toString()
76
+ }
77
+ }
78
+ return headersInit
79
+ }