node-tdd 4.0.2 → 4.0.4

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.
@@ -1,75 +1,21 @@
1
1
  import crypto from 'crypto';
2
- import qs from 'querystring';
3
- import get from 'lodash.get';
2
+ import { tryParseJson } from './util.js';
4
3
 
5
- import xml2js from 'xml2js';
6
-
7
- const parseRequestBody = (body) => Object.values(Object
8
- .entries(qs.parse(body))
9
- .filter(([k, v]) => k.startsWith('SendMessageBatchRequestEntry.'))
10
- .sort((a, b) => a[0].localeCompare(b[0]))
11
- .map(([k, v]) => [k.split('.'), v])
12
- .reduce((p, [k, v]) => {
13
- if (p[k[1]] === undefined) {
14
- Object.assign(p, { [k[1]]: {} });
15
- }
16
- Object.assign(p[k[1]], { [k[2]]: v });
17
- return p;
18
- }, {}));
19
-
20
- const parseResponseBody = (body) => {
21
- let parsed = null;
22
- xml2js.parseString(body, (err, result) => {
23
- parsed = result;
24
- });
25
- return parsed;
26
- };
27
-
28
- export default (requestBody, responseBody, scope) => {
4
+ export default (requestBody, responseBody, scope, req) => {
29
5
  if (
30
- !/^https:\/\/sqs\.[a-z0-9-]+\.amazonaws\.com:443$/.test(scope.basePath)
31
- || !responseBody.startsWith('<?xml version="1.0"?><SendMessageBatchResponse')) {
6
+ scope?.basePath !== 'https://sqs.us-west-2.amazonaws.com:443'
7
+ || req?.options?.headers?.['x-amz-target'] !== 'AmazonSQS.SendMessageBatch'
8
+ ) {
32
9
  return responseBody;
33
10
  }
34
11
 
35
- const responseBodyParsed = parseResponseBody(responseBody);
36
-
37
- const resultEntries = parseRequestBody(requestBody)
38
- .map(({ Id, MessageBody }, idx) => [
39
- '<SendMessageBatchResultEntry>',
40
- `<Id>${Id}</Id>`,
41
- `<MessageId>${
42
- get(responseBodyParsed, [
43
- 'SendMessageBatchResponse',
44
- 'SendMessageBatchResult',
45
- 0,
46
- 'SendMessageBatchResultEntry',
47
- idx,
48
- 'MessageId'
49
- ], crypto.randomUUID())
50
- }</MessageId>`,
51
- `<MD5OfMessageBody>${
52
- crypto.createHash('md5').update(MessageBody).digest('hex')
53
- }</MD5OfMessageBody>`,
54
- '</SendMessageBatchResultEntry>'
55
- ].join(''));
56
- return [
57
- '<?xml version="1.0"?>',
58
- '<SendMessageBatchResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">',
59
- '<SendMessageBatchResult>',
60
- ...resultEntries,
61
- '</SendMessageBatchResult>',
62
- '<ResponseMetadata>',
63
- `<RequestId>${
64
- get(responseBodyParsed, [
65
- 'SendMessageBatchResponse',
66
- 'ResponseMetadata',
67
- 0,
68
- 'RequestId',
69
- 0
70
- ], crypto.randomUUID())
71
- }</RequestId>`,
72
- '</ResponseMetadata>',
73
- '</SendMessageBatchResponse>'
74
- ].join('');
12
+ const requestJson = tryParseJson(requestBody);
13
+ const responseJson = tryParseJson(responseBody);
14
+ return {
15
+ Successful: requestJson.Entries.map(({ Id, MessageBody }, idx) => ({
16
+ Id,
17
+ MessageId: responseJson?.Successful?.[idx]?.MessageId || crypto.randomUUID(),
18
+ MD5OfMessageBody: crypto.createHash('md5').update(MessageBody).digest('hex')
19
+ }))
20
+ };
75
21
  };
@@ -91,8 +91,21 @@ export default (opts) => {
91
91
  nockBack.setMode(hasCassette ? 'lockdown' : 'record');
92
92
  nockBack.fixtures = opts.cassetteFolder;
93
93
  nockMock.patch();
94
- nockListener.subscribe('no match', () => {
94
+ nockListener.subscribe('no match', (req) => {
95
95
  assert(hasCassette === true);
96
+
97
+ // convert 404 response code to 500
98
+ const destroyOriginal = req.destroy;
99
+ req.destroy = (err) => {
100
+ if (err.status === 404 && err.statusCode === 404 && err.code === 'ERR_NOCK_NO_MATCH') {
101
+ // eslint-disable-next-line no-param-reassign
102
+ err.statusCode = 500;
103
+ // eslint-disable-next-line no-param-reassign
104
+ err.status = 500;
105
+ }
106
+ return destroyOriginal.call(req, err);
107
+ };
108
+
96
109
  const { protocol, options, body } = requestInjector.getLast();
97
110
  if (anyFlagPresent(['record'])) {
98
111
  expectedCassette.push(async () => {
@@ -211,7 +224,7 @@ export default (opts) => {
211
224
  const responseBody = tryParseJson([
212
225
  healSqsSendMessageBatch
213
226
  ].reduce(
214
- (respBody, fn) => fn(requestBodyString, respBody, scope),
227
+ (respBody, fn) => fn(requestBodyString, respBody, scope, req),
215
228
  interceptor.body
216
229
  ));
217
230
  // eslint-disable-next-line no-param-reassign
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-tdd",
3
3
  "type": "module",
4
- "version": "4.0.2",
4
+ "version": "4.0.4",
5
5
  "description": "Drop in extension for mocha to abstract commonly used test setups",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "homepage": "https://github.com/blackflux/node-tdd#readme",
43
43
  "devDependencies": {
44
- "@aws-sdk/client-sqs": "3.385.0",
44
+ "@aws-sdk/client-sqs": "3.458.0",
45
45
  "@babel/core": "7.23.3",
46
46
  "@babel/eslint-parser": "7.23.3",
47
47
  "@babel/register": "7.22.15",
@@ -51,7 +51,7 @@
51
51
  "axios": "1.6.2",
52
52
  "c8": "8.0.1",
53
53
  "chai": "4.3.10",
54
- "eslint": "8.53.0",
54
+ "eslint": "8.54.0",
55
55
  "eslint-config-airbnb-base": "15.0.0",
56
56
  "eslint-plugin-import": "2.29.0",
57
57
  "eslint-plugin-json": "3.1.0",
@@ -80,12 +80,11 @@
80
80
  "lodash.get": "4.4.2",
81
81
  "lru-cache-ext": "4.0.0",
82
82
  "minimist": "1.2.8",
83
- "nock": "13.3.8",
83
+ "nock": "13.4.0",
84
84
  "normalize-url": "2.0.1",
85
85
  "object-scan": "19.0.5",
86
86
  "smart-fs": "4.0.1",
87
87
  "timekeeper": "2.3.1",
88
- "tmp": "0.2.1",
89
- "xml2js": "0.6.2"
88
+ "tmp": "0.2.1"
90
89
  }
91
90
  }