From ca12cd5842f5a6934a059113b85b6af07b6fb39f Mon Sep 17 00:00:00 2001 From: Pranay Prakash Date: Fri, 12 Apr 2024 12:00:16 -0700 Subject: [PATCH] test:add tests to ensure objects are not modified --- tests/got/test_reply_body.js | 18 +++++++++--------- tests/test_common.js | 12 ++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/got/test_reply_body.js b/tests/got/test_reply_body.js index 3d009c7c2..ebedb07af 100644 --- a/tests/got/test_reply_body.js +++ b/tests/got/test_reply_body.js @@ -116,20 +116,20 @@ describe('`reply()` body', () => { scope.done() }) - it('does not modify the original used for response', async () => { - const patchBody = { id: 1234 } + it('does not modify the object used for response', async () => { + const patchBody = { number: 1234 } + const form = new FormData() + form.append('number', 1234) + const scope = nock('http://example.test') .patch('/', patchBody) .reply(200, patchBody) - const { statusCode, headers, body } = await got.patch( - 'http://example.test/', - { body: JSON.stringify(patchBody) }, - ) + await got.patch('http://example.test/', { + form, + }) - expect(statusCode).to.equal(200) - expect(headers).to.include({ 'content-type': 'application/json' }) - expect(body).to.be.a('string').and.equal(JSON.stringify(patchBody)) + expect(patchBody.number).to.be.a('number').and.equal(1234) scope.done() }) }) diff --git a/tests/test_common.js b/tests/test_common.js index f91ddb474..68d8922dc 100644 --- a/tests/test_common.js +++ b/tests/test_common.js @@ -71,6 +71,18 @@ describe('Body Match', () => { const result = matchBody({}, { number: 1 }, '{"number": "1"}') expect(result).to.equal(false) }) + + it('should not modify the original spec object', () => { + const spec = { number: 1 } + matchBody( + { + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + }, + spec, + '', + ) + expect(spec).to.deep.equal({ number: 1 }) + }) }) })