Skip to content

Commit 8e23567

Browse files
fix(cy.intercept): allow fixtures to define null encoding when used in static response (#19379)
1 parent 88cd53b commit 8e23567

File tree

6 files changed

+143
-4
lines changed

6 files changed

+143
-4
lines changed

packages/driver/cypress/integration/commands/net_stubbing_spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,61 @@ describe('network stubbing', function () {
13191319
cy.contains('#result', '""').should('be.visible')
13201320
})
13211321

1322+
// @see https://github.com/cypress-io/cypress/issues/19330
1323+
// @see https://github.com/cypress-io/cypress/issues/19344
1324+
it('load fixture as Buffer when encoding is null', function () {
1325+
// call through normally on everything
1326+
cy.spy(Cypress, 'backend')
1327+
1328+
cy.intercept('/fixtures/media/small.mp4', {
1329+
fixture: 'media/small.mp4,null',
1330+
}).as('video')
1331+
1332+
cy.visit('/fixtures/video.html')
1333+
.then(() => {
1334+
// @ts-ignore .getCall is a Sinon spy command
1335+
expect(Cypress.backend.getCall(0)).to.be.calledWithMatch(
1336+
'net',
1337+
'route:added',
1338+
{
1339+
staticResponse: {
1340+
fixture: {
1341+
filePath: 'media/small.mp4',
1342+
encoding: null,
1343+
},
1344+
},
1345+
},
1346+
)
1347+
})
1348+
})
1349+
1350+
it('load fixture with specified encoding', function () {
1351+
// call through normally on everything
1352+
cy.spy(Cypress, 'backend')
1353+
1354+
cy.intercept('non-existing-image.png', {
1355+
headers: { 'content-type': 'image/jpeg' },
1356+
fixture: 'media/cypress.png,base64',
1357+
}).as('video')
1358+
1359+
cy.visit('/fixtures/img-embed.html')
1360+
.then(() => {
1361+
// @ts-ignore .getCall is a Sinon spy command
1362+
expect(Cypress.backend.getCall(0)).to.be.calledWithMatch(
1363+
'net',
1364+
'route:added',
1365+
{
1366+
staticResponse: {
1367+
fixture: {
1368+
filePath: 'media/cypress.png',
1369+
encoding: 'base64',
1370+
},
1371+
},
1372+
},
1373+
)
1374+
})
1375+
})
1376+
13221377
// @see https://github.com/cypress-io/cypress/issues/8623
13231378
it('works with images', function () {
13241379
cy.visit('/fixtures/img-embed.html')
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const {
2+
getBackendStaticResponse,
3+
} = require('../../../../src/cy/net-stubbing/static-response-utils')
4+
5+
describe('driver/src/cy/net-stubbing/static-response-utils', () => {
6+
describe('.getBackendStaticResponse', () => {
7+
describe('delay', () => {
8+
it('does not set delay when delayMS is not provided', () => {
9+
const staticResponse = getBackendStaticResponse({})
10+
11+
expect(staticResponse).to.not.have.property('delay')
12+
})
13+
14+
it('sets delay', () => {
15+
const staticResponse = getBackendStaticResponse({ delayMs: 200 })
16+
17+
expect(staticResponse).to.have.property('delay', 200)
18+
})
19+
})
20+
21+
describe('fixtures', () => {
22+
it('does not set fixtures data when fixtures string is not provided', () => {
23+
const staticResponse = getBackendStaticResponse({})
24+
25+
expect(staticResponse).to.not.have.property('fixtures')
26+
})
27+
28+
it('parses fixture string without file encoding', () => {
29+
const staticResponse = getBackendStaticResponse({ fixture: 'file.html' })
30+
31+
expect(staticResponse.fixture).to.deep.equal({
32+
filePath: 'file.html',
33+
encoding: undefined,
34+
})
35+
})
36+
37+
it('parses fixture string with file encoding', () => {
38+
const staticResponse = getBackendStaticResponse({ fixture: 'file.html,utf8' })
39+
40+
expect(staticResponse.fixture).to.deep.equal({
41+
filePath: 'file.html',
42+
encoding: 'utf8',
43+
})
44+
})
45+
46+
it('parses fixture string with file encoding set as null to indicate Buffer', () => {
47+
const staticResponse = getBackendStaticResponse({ fixture: 'file.html,null' })
48+
49+
expect(staticResponse.fixture).to.deep.equal({
50+
filePath: 'file.html',
51+
encoding: null,
52+
})
53+
})
54+
})
55+
56+
describe('body', () => {
57+
it('does not set body when body is undefined', () => {
58+
const staticResponse = getBackendStaticResponse({})
59+
60+
expect(staticResponse).to.not.have.property('body')
61+
})
62+
63+
it('sets body when body is provided as a string', () => {
64+
const staticResponse = getBackendStaticResponse({ body: 'body' })
65+
66+
expect(staticResponse.body).to.eq('body')
67+
})
68+
69+
it('sets body when body is provided as a ArrayBuffer', () => {
70+
const buffer = new ArrayBuffer(8)
71+
const staticResponse = getBackendStaticResponse({ body: buffer })
72+
73+
expect(staticResponse.body).to.be.a('arraybuffer')
74+
expect(staticResponse.body).to.eq(buffer)
75+
})
76+
})
77+
})
78+
})

packages/driver/src/cy/net-stubbing/static-response-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function parseStaticResponseShorthand (statusCodeOrBody: number | string
9595
function getFixtureOpts (fixture: string): FixtureOpts {
9696
const [filePath, encoding] = fixture.split(',')
9797

98-
return { filePath, encoding }
98+
return { filePath, encoding: encoding === 'null' ? null : encoding }
9999
}
100100

101101
export function getBackendStaticResponse (staticResponse: Readonly<StaticResponse>): BackendStaticResponseWithArrayBuffer {

packages/net-stubbing/lib/internal-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from './external-types'
88

99
export type FixtureOpts = {
10-
encoding: string
10+
encoding: string | null
1111
filePath: string
1212
}
1313

packages/server/lib/fixture.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module.exports = {
165165

166166
return obj
167167
}).catch((err) => {
168-
throw new Error(`'${fixture}' is not a valid JavaScript object.${err.toString()}`)
168+
throw new Error(`'${fixture}' is not a valid JavaScript object.\n${err.toString()}`)
169169
})
170170
},
171171

@@ -190,10 +190,16 @@ module.exports = {
190190
parseHtml (p, fixture) {
191191
return fs.readFileAsync(p, 'utf8')
192192
.bind(this)
193+
.catch((err) => {
194+
throw new Error(`Unable to parse '${fixture}'.\n${err.toString()}`)
195+
})
193196
},
194197

195198
parse (p, fixture, encoding) {
196199
return fs.readFileAsync(p, encoding)
197200
.bind(this)
201+
.catch((err) => {
202+
throw new Error(`Unable to parse '${fixture}'.\n${err.toString()}`)
203+
})
198204
},
199205
}

packages/server/test/unit/fixture_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ ParseError: Unterminated string constant\
215215
.then(() => {
216216
throw new Error('should have failed but did not')
217217
}).catch((err) => {
218-
expect(err.message).to.eq(`'bad_js.js' is not a valid JavaScript object.\n${e}`)
218+
expect(err.message).to.eq(`'bad_js.js' is not a valid JavaScript object.\n\n${e}`)
219219
})
220220
})
221221
})

0 commit comments

Comments
 (0)