Skip to content

Commit 4319daa

Browse files
authored
fix: cypress removes custom status text/reason phrase from http response (#22061)
1 parent d01932b commit 4319daa

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

packages/proxy/lib/http/response-middleware.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ const MaybeSendRedirectToClient: ResponseMiddleware = function () {
509509

510510
const CopyResponseStatusCode: ResponseMiddleware = function () {
511511
this.res.status(Number(this.incomingRes.statusCode))
512+
// Set custom status message/reason phrase from http response
513+
// https://github.com/cypress-io/cypress/issues/16973
514+
if (this.incomingRes.statusMessage) {
515+
this.res.statusMessage = this.incomingRes.statusMessage
516+
}
517+
512518
this.next()
513519
}
514520

packages/server/test/integration/http_requests_spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,6 +3783,65 @@ describe('Routes', () => {
37833783
})
37843784
})
37853785

3786+
// Set custom status message/reason phrase from http response
3787+
// https://github.com/cypress-io/cypress/issues/16973
3788+
it('set custom status message when reason phrase is set', function () {
3789+
nock(this.server.remoteStates.current().origin)
3790+
.post('/companies/validate', {
3791+
payload: { name: 'Brian' },
3792+
})
3793+
.reply(400, function (uri, requestBody) {
3794+
this.req.response.statusMessage = 'This is the custom status message'
3795+
3796+
return {
3797+
status: 400,
3798+
message: 'This is the reply body',
3799+
}
3800+
})
3801+
3802+
return this.rp({
3803+
method: 'POST',
3804+
url: 'http://localhost:8000/companies/validate',
3805+
json: true,
3806+
body: {
3807+
payload: { name: 'Brian' },
3808+
},
3809+
headers: {
3810+
'x-cypress-status': '400',
3811+
},
3812+
})
3813+
.then((res) => {
3814+
expect(res.statusCode).to.eq(400)
3815+
expect(res.statusMessage).to.eq('This is the custom status message')
3816+
})
3817+
})
3818+
3819+
// use default status message/reason phrase correspond to status code, from http response
3820+
// https://github.com/cypress-io/cypress/issues/16973
3821+
it('uses default status message when reason phrase is not set', function () {
3822+
nock(this.server.remoteStates.current().origin)
3823+
.post('/companies/validate', {
3824+
payload: { name: 'Brian' },
3825+
})
3826+
.reply(400)
3827+
3828+
return this.rp({
3829+
method: 'POST',
3830+
url: 'http://localhost:8000/companies/validate',
3831+
json: true,
3832+
body: {
3833+
payload: { name: 'Brian' },
3834+
},
3835+
headers: {
3836+
'x-cypress-status': '400',
3837+
},
3838+
})
3839+
.then((res) => {
3840+
expect(res.statusCode).to.eq(400)
3841+
expect(res.statusMessage).to.eq('Bad Request')
3842+
})
3843+
})
3844+
37863845
it('hands back 201 status codes', function () {
37873846
nock(this.server.remoteStates.current().origin)
37883847
.post('/companies/validate', {

0 commit comments

Comments
 (0)