fix: revert "fix: res.send of cy.intercept doesn't override json-related content types. " (#17400)

This reverts commit c2a22060b7.
This commit is contained in:
Zach Bloomquist
2021-07-19 19:23:34 -04:00
committed by GitHub
parent c2f91f6364
commit 190294c0e6
5 changed files with 20 additions and 63 deletions

View File

@@ -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',

View File

@@ -75,14 +75,7 @@ export const onResponse: HandlerFn<CyHttpMessages.IncomingResponse> = 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)

View File

@@ -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<StaticRespons
backendStaticResponse.body = staticResponse.body
} else {
backendStaticResponse.body = JSON.stringify(staticResponse.body)
// There are various json-related MIME types. We cannot simply set it as `application/json`.
// @see https://www.iana.org/assignments/media-types/media-types.xhtml
if (
!backendStaticResponse.headers ||
(backendStaticResponse.headers &&
!caseInsensitiveHas(backendStaticResponse.headers, 'content-type'))
) {
_.set(backendStaticResponse, 'headers.content-type', 'application/json')
}
_.set(backendStaticResponse, 'headers.content-type', 'application/json')
}
}

View File

@@ -16,7 +16,6 @@ import ThrottleStream from 'throttle'
import MimeTypes from 'mime-types'
import { CypressIncomingRequest } from '@packages/proxy'
import { InterceptedRequest } from './intercepted-request'
import { caseInsensitiveGet, caseInsensitiveHas } from '../util'
// TODO: move this into net-stubbing once cy.route is removed
import { parseContentType } from '@packages/server/lib/controllers/xhrs'
@@ -80,6 +79,24 @@ function _getFakeClientResponse (opts: {
return clientResponse
}
const caseInsensitiveGet = function (obj, lowercaseProperty) {
for (let key of Object.keys(obj)) {
if (key.toLowerCase() === lowercaseProperty) {
return obj[key]
}
}
}
const caseInsensitiveHas = function (obj, lowercaseProperty) {
for (let key of Object.keys(obj)) {
if (key.toLowerCase() === lowercaseProperty) {
return true
}
}
return false
}
export function setDefaultHeaders (req: CypressIncomingRequest, res: IncomingMessage) {
const setDefaultHeader = (lowercaseHeader: string, defaultValueFn: () => string) => {
if (!caseInsensitiveHas(res.headers, lowercaseHeader)) {

View File

@@ -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
}