Skip to content

Commit 1b152fc

Browse files
fix(webpack-dev-server): do not encodeUri in loader (#20575)
* fix: do not encodeUri in webpack loader * add tests * fix test * fix: use decodeURI in loader and add tests (#20593) * add tests for non-unicode and specs with ... * add more system tests Co-authored-by: Zachary Williams <[email protected]>
1 parent 793cb3e commit 1b152fc

File tree

17 files changed

+2453
-17
lines changed

17 files changed

+2453
-17
lines changed

npm/webpack-dev-server/src/loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const makeImport = (file: Cypress.Cypress['spec'], filename: string, chunkName:
1818
const magicComments = chunkName ? `/* webpackChunkName: "${chunkName}" */` : ''
1919

2020
return `"${filename}": {
21-
shouldLoad: () => document.location.pathname.includes("${encodeURI(file.absolute)}"),
21+
shouldLoad: () => decodeURI(document.location.pathname).includes("${file.absolute}"),
2222
load: () => import("${file.absolute}" ${magicComments}),
2323
chunkName: "${chunkName}",
2424
}`

npm/webpack-dev-server/test/e2e.spec.ts

+93-15
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { webpackDevServerFacts } from '../src/webpackDevServerFacts'
88

99
import { defineDevServerConfig, devServer, startDevServer } from '../'
1010

11-
const requestSpecFile = (port: number) => {
11+
const requestSpecFile = (file: string, port: number) => {
1212
return new Promise((res) => {
1313
const opts = {
1414
host: 'localhost',
1515
port,
16-
path: '/test/fixtures/foo.spec.js',
16+
path: encodeURI(file),
1717
}
1818

1919
const callback = (response: EventEmitter) => {
@@ -41,13 +41,15 @@ const webpackConfig = {
4141

4242
}
4343

44-
const specs: Cypress.Cypress['spec'][] = [
45-
{
46-
name: `${root}/test/fixtures/foo.spec.js`,
47-
relative: `${root}/test/fixtures/foo.spec.js`,
48-
absolute: `${root}/test/fixtures/foo.spec.js`,
49-
},
50-
]
44+
const createSpecs = (name: string): Cypress.Cypress['spec'][] => {
45+
return [
46+
{
47+
name: `${root}/test/fixtures/${name}`,
48+
relative: `${root}/test/fixtures/${name}`,
49+
absolute: `${root}/test/fixtures/${name}`,
50+
},
51+
]
52+
}
5153

5254
const config = {
5355
projectRoot: root,
@@ -62,12 +64,12 @@ describe('#startDevServer', () => {
6264
webpackConfig,
6365
options: {
6466
config,
65-
specs,
67+
specs: createSpecs('foo.spec.js'),
6668
devServerEvents: new EventEmitter(),
6769
},
6870
})
6971

70-
const response = await requestSpecFile(port as number)
72+
const response = await requestSpecFile('/test/fixtures/foo.spec.js', port as number)
7173

7274
expect(response).to.eq('const foo = () => {}\n')
7375

@@ -76,13 +78,89 @@ describe('#startDevServer', () => {
7678
})
7779
})
7880

81+
it('serves specs in directory with [] chars via a webpack dev server', async () => {
82+
const { port, close } = await startDevServer({
83+
webpackConfig,
84+
options: {
85+
config,
86+
specs: createSpecs('[foo]/bar.spec.js'),
87+
devServerEvents: new EventEmitter(),
88+
},
89+
})
90+
91+
const response = await requestSpecFile('/test/fixtures/[foo]/bar.spec.js', port as number)
92+
93+
expect(response).to.eq(`it('this is a spec with a path containing []', () => {})\n`)
94+
95+
return new Promise((res) => {
96+
close(() => res())
97+
})
98+
})
99+
100+
it('serves specs in directory with non English chars via a webpack dev server', async () => {
101+
const { port, close } = await startDevServer({
102+
webpackConfig,
103+
options: {
104+
config,
105+
specs: createSpecs('サイプレス.spec.js'),
106+
devServerEvents: new EventEmitter(),
107+
},
108+
})
109+
110+
const response = await requestSpecFile('/test/fixtures/サイプレス.spec.js', port as number)
111+
112+
expect(response).to.eq(`it('サイプレス', () => {})\n`)
113+
114+
return new Promise((res) => {
115+
close(() => res())
116+
})
117+
})
118+
119+
it('serves specs in directory with ... in the file name via a webpack dev server', async () => {
120+
const { port, close } = await startDevServer({
121+
webpackConfig,
122+
options: {
123+
config,
124+
specs: createSpecs('[...bar].spec.js'),
125+
devServerEvents: new EventEmitter(),
126+
},
127+
})
128+
129+
const response = await requestSpecFile('/test/fixtures/[...bar].spec.js', port as number)
130+
131+
expect(response).to.eq(`it('...bar', () => {})\n`)
132+
133+
return new Promise((res) => {
134+
close(() => res())
135+
})
136+
})
137+
138+
it('serves a file with spaces via a webpack dev server', async () => {
139+
const { port, close } = await startDevServer({
140+
webpackConfig,
141+
options: {
142+
config,
143+
specs: createSpecs('foo bar.spec.js'),
144+
devServerEvents: new EventEmitter(),
145+
},
146+
})
147+
148+
const response = await requestSpecFile('/test/fixtures/foo bar.spec.js', port as number)
149+
150+
expect(response).to.eq(`it('this is a spec with a path containing a space', () => {})\n`)
151+
152+
return new Promise((res) => {
153+
close(() => res())
154+
})
155+
})
156+
79157
it('emits dev-server:compile:success event on successful compilation', async () => {
80158
const devServerEvents = new EventEmitter()
81159
const { close } = await startDevServer({
82160
webpackConfig,
83161
options: {
84162
config,
85-
specs,
163+
specs: createSpecs('foo.spec.js'),
86164
devServerEvents,
87165
},
88166
})
@@ -137,7 +215,7 @@ describe('#startDevServer', () => {
137215
webpackConfig,
138216
options: {
139217
config,
140-
specs,
218+
specs: createSpecs('foo.spec.js'),
141219
devServerEvents,
142220
},
143221
})
@@ -172,13 +250,13 @@ describe('#startDevServer', () => {
172250
const { port, close } = await devServer(
173251
{
174252
config,
175-
specs,
253+
specs: createSpecs('foo.spec.js'),
176254
devServerEvents,
177255
},
178256
defineDevServerConfig({ webpackConfig }),
179257
)
180258

181-
const response = await requestSpecFile(port as number)
259+
const response = await requestSpecFile('/test/fixtures/foo.spec.js', port as number)
182260

183261
expect(response).to.eq('const foo = () => {}\n')
184262

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it('...bar', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it('this is a spec with a path containing []', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it('this is a spec with a path containing a space', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it('サイプレス', () => {})

packages/server/lib/controllers/iframes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const iframesController = {
4545
// attach header data for webservers
4646
// to properly intercept and serve assets from the correct src root
4747
// TODO: define a contract for dev-server plugins to configure this behavior
48-
req.headers.__cypress_spec_path = req.params[0]
48+
req.headers.__cypress_spec_path = encodeURI(req.params[0])
4949
req.url = `${config.devServerPublicPathRoute}/index.html`
5050

5151
// user the node proxy here instead of the network proxy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it('...bar', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('this test is in a directory with [] in the path', () => {
2+
it('works great!', () => {
3+
expect(1).to.eq(1)
4+
})
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('this test is in a file with space in the path', () => {
2+
it('works great!', () => {
3+
expect(1).to.eq(1)
4+
})
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it('サイプレス', () => {})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { startDevServer } = require('@cypress/webpack-dev-server')
2+
3+
module.exports = (on, config) => {
4+
on('dev-server:start', (options) => {
5+
return startDevServer({
6+
options,
7+
webpackConfig: {},
8+
})
9+
})
10+
}

system-tests/projects/webpack-dev-server/cypress/support/component.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"webpack": "^4"
4+
}
5+
}

0 commit comments

Comments
 (0)