From 4182fead5e2ba50a1a20ddff15ac89fb529641aa Mon Sep 17 00:00:00 2001 From: Filippo De Santis Date: Fri, 20 May 2022 06:59:16 +0200 Subject: [PATCH 1/2] Add a small paragraph documenting reply accept a function as the data parameter --- docs/best-practices/mocking-request.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/best-practices/mocking-request.md b/docs/best-practices/mocking-request.md index ebcc90d24b7..9b89d0bcebf 100644 --- a/docs/best-practices/mocking-request.md +++ b/docs/best-practices/mocking-request.md @@ -101,4 +101,36 @@ const badRequest = await bankTransfer('1234567890', '100') // subsequent request to origin http://localhost:3000 was not allowed (net.connect disabled) ``` +## Reply with data based on request +If the mocked response needs to be dinamically derived from the reuqest parametes, you can provide a function instead of an object to `reply` + +```js +mockPool.intercept({ + path: '/bank-transfer', + method: 'POST', + headers: { + 'X-TOKEN-SECRET': 'SuperSecretToken', + }, + body: JSON.stringify({ + recepient: '1234567890', + amount: '100' + }) +}).reply(200, (opts) => { + // do something with opts + + return { message: 'transaction processed' } +}) +``` + +in this case opts will be + +``` +{ + method: 'POST', + headers: { 'X-TOKEN-SECRET': 'SuperSecretToken' }, + body: '{"recepient":"1234567890","amount":"100"}', + origin: 'http://localhost:3000', + path: '/bank-transfer' +} +``` From ad2f2cc6d0192b9ad2f3e08f8382921cd0721a97 Mon Sep 17 00:00:00 2001 From: Filippo De Santis Date: Mon, 23 May 2022 09:41:00 +0200 Subject: [PATCH 2/2] Update docs/best-practices/mocking-request.md Co-authored-by: Frazer Smith --- docs/best-practices/mocking-request.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/best-practices/mocking-request.md b/docs/best-practices/mocking-request.md index 9b89d0bcebf..b98a450a32e 100644 --- a/docs/best-practices/mocking-request.md +++ b/docs/best-practices/mocking-request.md @@ -103,7 +103,7 @@ const badRequest = await bankTransfer('1234567890', '100') ## Reply with data based on request -If the mocked response needs to be dinamically derived from the reuqest parametes, you can provide a function instead of an object to `reply` +If the mocked response needs to be dynamically derived from the request parameters, you can provide a function instead of an object to `reply` ```js mockPool.intercept({