Skip to content

Commit 26fb025

Browse files
authored
fix(ClientRequest): support get with body (#2854)
1 parent 8348274 commit 26fb025

4 files changed

Lines changed: 76 additions & 31 deletions

File tree

lib/intercept.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ const { inherits } = require('util')
1010
const http = require('http')
1111
const { intercept: debug } = require('./debug')
1212
const globalEmitter = require('./global_emitter')
13-
const { BatchInterceptor } = require('@mswjs/interceptors')
13+
const { BatchInterceptor, getRawRequest } = require('@mswjs/interceptors')
1414
const {
1515
default: nodeInterceptors,
1616
} = require('@mswjs/interceptors/presets/node')
1717
const { createResponse } = require('./create_response')
1818
const { once } = require('events')
19+
const { arrayBuffer } = require('stream/consumers')
1920

2021
const interceptor = new BatchInterceptor({
2122
name: 'nock-interceptor',
@@ -409,7 +410,17 @@ function activate() {
409410
once(nockRequest, 'error'),
410411
once(nockRequest, 'response'),
411412
])
412-
const buffer = await request.arrayBuffer()
413+
414+
const rawRequest = getRawRequest(mswRequest)
415+
416+
// If this is GET request with body, we need to read the body from the socket because Fetch API doesn't support this.
417+
const buffer =
418+
rawRequest instanceof originalClientRequest &&
419+
rawRequest.method === 'GET' &&
420+
rawRequest.getHeader('content-length') > 0
421+
? // TODO: use getClientRequestBodyStream instead of access to internal properties
422+
await arrayBuffer(rawRequest.socket.requestStream)
423+
: await request.arrayBuffer()
413424
nockRequest.write(buffer)
414425
nockRequest.end()
415426
await promise

package-lock.json

Lines changed: 47 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"main": "./index.js",
2323
"types": "types",
2424
"dependencies": {
25-
"@mswjs/interceptors": "^0.38.1",
25+
"@mswjs/interceptors": "^0.38.5",
2626
"json-stringify-safe": "^5.0.1",
2727
"propagate": "^2.0.0"
2828
},

tests/got/test_intercept.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ describe('Intercept', () => {
7979
req.end()
8080
})
8181

82+
it('should intercept a GET request with body', async () => {
83+
const scope = nock('http://example.test')
84+
.get('/')
85+
.reply(200, (uri, body) => body)
86+
87+
const { body } = await got('http://example.test/', {
88+
method: 'GET',
89+
allowGetBody: true,
90+
body: 'Hello World!',
91+
})
92+
93+
expect(body).to.equal('Hello World!')
94+
scope.done()
95+
})
96+
8297
it('should intercept a request with a base path', async () => {
8398
const scope = nock('http://example.test/abc').get('/def').reply(201)
8499

0 commit comments

Comments
 (0)