Skip to content

Commit

Permalink
fix: revert "fix: res.send of cy.intercept doesn't override json-…
Browse files Browse the repository at this point in the history
…related content types. " (#17400)

This reverts commit c2a2206.
  • Loading branch information
flotwig authored Jul 19, 2021
1 parent c2f91f6 commit 190294c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 63 deletions.
24 changes: 0 additions & 24 deletions packages/driver/cypress/integration/commands/net_stubbing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 1 addition & 8 deletions packages/driver/src/cy/net-stubbing/events/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 1 addition & 13 deletions packages/driver/src/cy/net-stubbing/static-response-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
}
}

Expand Down
19 changes: 18 additions & 1 deletion packages/net-stubbing/lib/server/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)) {
Expand Down
17 changes: 0 additions & 17 deletions packages/net-stubbing/lib/util.ts

This file was deleted.

4 comments on commit 190294c

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 190294c Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.0.0/circle-develop-190294c0e60ae40815fa6fed3346dccae6c0a4df/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 190294c Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.0.0/appveyor-develop-190294c0e60ae40815fa6fed3346dccae6c0a4df/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 190294c Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.0.0/appveyor-develop-190294c0e60ae40815fa6fed3346dccae6c0a4df/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 190294c Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.0.0/circle-develop-190294c0e60ae40815fa6fed3346dccae6c0a4df/cypress.tgz

Please sign in to comment.