Skip to content

Commit

Permalink
fix(webpack-dev-server): do not encodeUri in loader (#20575)
Browse files Browse the repository at this point in the history
* 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 <ZachJW34@gmail.com>
  • Loading branch information
lmiller1990 and ZachJW34 committed Mar 15, 2022
1 parent 793cb3e commit 1b152fc
Show file tree
Hide file tree
Showing 17 changed files with 2,453 additions and 17 deletions.
2 changes: 1 addition & 1 deletion npm/webpack-dev-server/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const makeImport = (file: Cypress.Cypress['spec'], filename: string, chunkName:
const magicComments = chunkName ? `/* webpackChunkName: "${chunkName}" */` : ''

return `"${filename}": {
shouldLoad: () => document.location.pathname.includes("${encodeURI(file.absolute)}"),
shouldLoad: () => decodeURI(document.location.pathname).includes("${file.absolute}"),
load: () => import("${file.absolute}" ${magicComments}),
chunkName: "${chunkName}",
}`
Expand Down
108 changes: 93 additions & 15 deletions npm/webpack-dev-server/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { webpackDevServerFacts } from '../src/webpackDevServerFacts'

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

const requestSpecFile = (port: number) => {
const requestSpecFile = (file: string, port: number) => {
return new Promise((res) => {
const opts = {
host: 'localhost',
port,
path: '/test/fixtures/foo.spec.js',
path: encodeURI(file),
}

const callback = (response: EventEmitter) => {
Expand Down Expand Up @@ -41,13 +41,15 @@ const webpackConfig = {

}

const specs: Cypress.Cypress['spec'][] = [
{
name: `${root}/test/fixtures/foo.spec.js`,
relative: `${root}/test/fixtures/foo.spec.js`,
absolute: `${root}/test/fixtures/foo.spec.js`,
},
]
const createSpecs = (name: string): Cypress.Cypress['spec'][] => {
return [
{
name: `${root}/test/fixtures/${name}`,
relative: `${root}/test/fixtures/${name}`,
absolute: `${root}/test/fixtures/${name}`,
},
]
}

const config = {
projectRoot: root,
Expand All @@ -62,12 +64,12 @@ describe('#startDevServer', () => {
webpackConfig,
options: {
config,
specs,
specs: createSpecs('foo.spec.js'),
devServerEvents: new EventEmitter(),
},
})

const response = await requestSpecFile(port as number)
const response = await requestSpecFile('/test/fixtures/foo.spec.js', port as number)

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

Expand All @@ -76,13 +78,89 @@ describe('#startDevServer', () => {
})
})

it('serves specs in directory with [] chars via a webpack dev server', async () => {
const { port, close } = await startDevServer({
webpackConfig,
options: {
config,
specs: createSpecs('[foo]/bar.spec.js'),
devServerEvents: new EventEmitter(),
},
})

const response = await requestSpecFile('/test/fixtures/[foo]/bar.spec.js', port as number)

expect(response).to.eq(`it('this is a spec with a path containing []', () => {})\n`)

return new Promise((res) => {
close(() => res())
})
})

it('serves specs in directory with non English chars via a webpack dev server', async () => {
const { port, close } = await startDevServer({
webpackConfig,
options: {
config,
specs: createSpecs('サイプレス.spec.js'),
devServerEvents: new EventEmitter(),
},
})

const response = await requestSpecFile('/test/fixtures/サイプレス.spec.js', port as number)

expect(response).to.eq(`it('サイプレス', () => {})\n`)

return new Promise((res) => {
close(() => res())
})
})

it('serves specs in directory with ... in the file name via a webpack dev server', async () => {
const { port, close } = await startDevServer({
webpackConfig,
options: {
config,
specs: createSpecs('[...bar].spec.js'),
devServerEvents: new EventEmitter(),
},
})

const response = await requestSpecFile('/test/fixtures/[...bar].spec.js', port as number)

expect(response).to.eq(`it('...bar', () => {})\n`)

return new Promise((res) => {
close(() => res())
})
})

it('serves a file with spaces via a webpack dev server', async () => {
const { port, close } = await startDevServer({
webpackConfig,
options: {
config,
specs: createSpecs('foo bar.spec.js'),
devServerEvents: new EventEmitter(),
},
})

const response = await requestSpecFile('/test/fixtures/foo bar.spec.js', port as number)

expect(response).to.eq(`it('this is a spec with a path containing a space', () => {})\n`)

return new Promise((res) => {
close(() => res())
})
})

it('emits dev-server:compile:success event on successful compilation', async () => {
const devServerEvents = new EventEmitter()
const { close } = await startDevServer({
webpackConfig,
options: {
config,
specs,
specs: createSpecs('foo.spec.js'),
devServerEvents,
},
})
Expand Down Expand Up @@ -137,7 +215,7 @@ describe('#startDevServer', () => {
webpackConfig,
options: {
config,
specs,
specs: createSpecs('foo.spec.js'),
devServerEvents,
},
})
Expand Down Expand Up @@ -172,13 +250,13 @@ describe('#startDevServer', () => {
const { port, close } = await devServer(
{
config,
specs,
specs: createSpecs('foo.spec.js'),
devServerEvents,
},
defineDevServerConfig({ webpackConfig }),
)

const response = await requestSpecFile(port as number)
const response = await requestSpecFile('/test/fixtures/foo.spec.js', port as number)

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

Expand Down
1 change: 1 addition & 0 deletions npm/webpack-dev-server/test/fixtures/[...bar].spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('...bar', () => {})
1 change: 1 addition & 0 deletions npm/webpack-dev-server/test/fixtures/[foo]/bar.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('this is a spec with a path containing []', () => {})
1 change: 1 addition & 0 deletions npm/webpack-dev-server/test/fixtures/foo bar.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('this is a spec with a path containing a space', () => {})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('サイプレス', () => {})
2 changes: 1 addition & 1 deletion packages/server/lib/controllers/iframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const iframesController = {
// attach header data for webservers
// to properly intercept and serve assets from the correct src root
// TODO: define a contract for dev-server plugins to configure this behavior
req.headers.__cypress_spec_path = req.params[0]
req.headers.__cypress_spec_path = encodeURI(req.params[0])
req.url = `${config.devServerPublicPathRoute}/index.html`

// user the node proxy here instead of the network proxy
Expand Down
1 change: 1 addition & 0 deletions system-tests/projects/webpack-dev-server/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('...bar', () => {})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('this test is in a directory with [] in the path', () => {
it('works great!', () => {
expect(1).to.eq(1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('this test is in a file with space in the path', () => {
it('works great!', () => {
expect(1).to.eq(1)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
it('サイプレス', () => {})
10 changes: 10 additions & 0 deletions system-tests/projects/webpack-dev-server/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { startDevServer } = require('@cypress/webpack-dev-server')

module.exports = (on, config) => {
on('dev-server:start', (options) => {
return startDevServer({
options,
webpackConfig: {},
})
})
}
Empty file.
5 changes: 5 additions & 0 deletions system-tests/projects/webpack-dev-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"webpack": "^4"
}
}

0 comments on commit 1b152fc

Please sign in to comment.