From 190294c0e60ae40815fa6fed3346dccae6c0a4df Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Mon, 19 Jul 2021 19:23:34 -0400 Subject: [PATCH] fix: revert "fix: `res.send` of `cy.intercept` doesn't override json-related content types. " (#17400) This reverts commit c2a22060b72c61c159e8c000e0c403ed892c599a. --- .../integration/commands/net_stubbing_spec.ts | 24 ------------------- .../src/cy/net-stubbing/events/response.ts | 9 +------ .../cy/net-stubbing/static-response-utils.ts | 14 +---------- packages/net-stubbing/lib/server/util.ts | 19 ++++++++++++++- packages/net-stubbing/lib/util.ts | 17 ------------- 5 files changed, 20 insertions(+), 63 deletions(-) delete mode 100644 packages/net-stubbing/lib/util.ts diff --git a/packages/driver/cypress/integration/commands/net_stubbing_spec.ts b/packages/driver/cypress/integration/commands/net_stubbing_spec.ts index 21cb968af88b..3e3d59d641de 100644 --- a/packages/driver/cypress/integration/commands/net_stubbing_spec.ts +++ b/packages/driver/cypress/integration/commands/net_stubbing_spec.ts @@ -2685,30 +2685,6 @@ describe('network stubbing', { retries: 2 }, function () { .wait('@get') }) - // https://github.com/cypress-io/cypress/issues/17084 - it('does not overwrite the json-related content-type header', () => { - cy.intercept('/json-content-type', (req) => { - req.on('response', (res) => { - res.send({ - statusCode: 500, - headers: { - 'content-type': 'application/problem+json', - 'access-control-allow-origin': '*', - }, - body: { - status: 500, - title: 'Internal Server Error', - }, - }) - }) - }) - - fetch('/json-content-type') - .then((res) => { - expect(res.headers.get('content-type')).to.eq('application/problem+json') - }) - }) - context('body parsing', function () { [ 'application/json', diff --git a/packages/driver/src/cy/net-stubbing/events/response.ts b/packages/driver/src/cy/net-stubbing/events/response.ts index e5f7dc668720..405ea2c0070e 100644 --- a/packages/driver/src/cy/net-stubbing/events/response.ts +++ b/packages/driver/src/cy/net-stubbing/events/response.ts @@ -75,14 +75,7 @@ export const onResponse: HandlerFn = async (Cyp // arguments to res.send() are merged with the existing response const _staticResponse = _.defaults({}, staticResponse, _.pick(res, STATIC_RESPONSE_KEYS)) - _staticResponse.headers = _.defaults({}, _staticResponse.headers, res.headers) - - // https://github.com/cypress-io/cypress/issues/17084 - // When a user didn't provide content-type, - // we remove the content-type provided by the server - if (!staticResponse.headers || !staticResponse.headers['content-type']) { - delete _staticResponse.headers['content-type'] - } + _.defaults(_staticResponse.headers, res.headers) sendStaticResponse(requestId, _staticResponse) diff --git a/packages/driver/src/cy/net-stubbing/static-response-utils.ts b/packages/driver/src/cy/net-stubbing/static-response-utils.ts index 982d18977466..b89d4b40217b 100644 --- a/packages/driver/src/cy/net-stubbing/static-response-utils.ts +++ b/packages/driver/src/cy/net-stubbing/static-response-utils.ts @@ -5,9 +5,6 @@ import { BackendStaticResponseWithArrayBuffer, FixtureOpts, } from '@packages/net-stubbing/lib/types' -import { - caseInsensitiveHas, -} from '@packages/net-stubbing/lib/util' import $errUtils from '../../cypress/error_utils' // user-facing StaticResponse only @@ -115,16 +112,7 @@ export function getBackendStaticResponse (staticResponse: Readonly string) => { if (!caseInsensitiveHas(res.headers, lowercaseHeader)) { diff --git a/packages/net-stubbing/lib/util.ts b/packages/net-stubbing/lib/util.ts deleted file mode 100644 index e922fcc8c120..000000000000 --- a/packages/net-stubbing/lib/util.ts +++ /dev/null @@ -1,17 +0,0 @@ -export const caseInsensitiveGet = function (obj, lowercaseProperty) { - for (let key of Object.keys(obj)) { - if (key.toLowerCase() === lowercaseProperty) { - return obj[key] - } - } -} - -export const caseInsensitiveHas = function (obj, lowercaseProperty) { - for (let key of Object.keys(obj)) { - if (key.toLowerCase() === lowercaseProperty) { - return true - } - } - - return false -}