Skip to content

Commit

Permalink
fix(middleware): replace %X_UA_COMPATIBLE% marker anywhere in the file
Browse files Browse the repository at this point in the history
Previously %X_UA_COMPATIBLE% marker was only replaced if it was located at the start of the line.
The limitation looks pretty arbitrary and caused the marker not to be replaced in the custom
debug.html file used by Angular CLI as the marker was not located at the start of the line
(probably because the file was re-formatted). This commit changes the behavior to replace the
marker anywhere within the file, not just at the start of the line and thus fixes the problem
for Angular CLI and potentially other people using custom files.

Fixes #3711
  • Loading branch information
devoto13 committed Oct 28, 2021
1 parent 36d41cb commit f1aeaec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/middleware/karma.js
Expand Up @@ -45,7 +45,7 @@ function getQuery (urlStr) {
function getXUACompatibleMetaElement (url) {
const query = getQuery(url)
if (query['x-ua-compatible']) {
return `\n<meta http-equiv="X-UA-Compatible" content="${query['x-ua-compatible']}"/>`
return `<meta http-equiv="X-UA-Compatible" content="${query['x-ua-compatible']}"/>`
}
return ''
}
Expand Down Expand Up @@ -107,7 +107,7 @@ function createKarmaMiddleware (
} else { // serve client.html
return serveStaticFile('/client.html', requestedRangeHeader, response, (data) =>
data
.replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
.replace('%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
.replace('%X_UA_COMPATIBLE_URL%', getXUACompatibleUrl(request.url)))
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ function createKarmaMiddleware (
.replace('%CLIENT_CONFIG%', 'window.__karma__.config = ' + JSON.stringify(client) + ';\n')
.replace('%SCRIPT_URL_ARRAY%', () => 'window.__karma__.scriptUrls = ' + JSON.stringify(scriptUrls) + ';\n')
.replace('%MAPPINGS%', () => 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n')
.replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
.replace('%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
})
})
} else if (requestUrl === '/context.json') {
Expand Down
2 changes: 1 addition & 1 deletion static/client.html
Expand Up @@ -5,7 +5,7 @@
-->
<html>
<head>
%X_UA_COMPATIBLE%
%X_UA_COMPATIBLE%
<title>Karma</title>
<link href="favicon.ico" rel="icon" type="image/x-icon">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Expand Down
2 changes: 1 addition & 1 deletion static/debug.html
Expand Up @@ -6,7 +6,7 @@
-->
<html>
<head>
%X_UA_COMPATIBLE%
%X_UA_COMPATIBLE%
<title>Karma DEBUG RUNNER</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Expand Down
8 changes: 4 additions & 4 deletions test/unit/middleware/karma.spec.js
Expand Up @@ -27,10 +27,10 @@ describe('middleware.karma', () => {
const fsMock = mocks.fs.create({
karma: {
static: {
'client.html': mocks.fs.file(0, 'CLIENT HTML\n%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%'),
'client.html': mocks.fs.file(0, 'CLIENT HTML%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%'),
'client_with_context.html': mocks.fs.file(0, 'CLIENT_WITH_CONTEXT\n%SCRIPT_URL_ARRAY%'),
'context.html': mocks.fs.file(0, 'CONTEXT\n%SCRIPTS%'),
'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%\n%X_UA_COMPATIBLE%'),
'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%%X_UA_COMPATIBLE%'),
'karma.js': mocks.fs.file(0, 'root: %KARMA_URL_ROOT%, proxy: %KARMA_PROXY_PATH%, v: %KARMA_VERSION%')
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('middleware.karma', () => {

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CLIENT HTML\n<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>?x-ua-compatible=xxx%3Dyyy')
expect(response).to.beServedAs(200, 'CLIENT HTML<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>?x-ua-compatible=xxx%3Dyyy')
done()
})

Expand All @@ -182,7 +182,7 @@ describe('middleware.karma', () => {

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'DEBUG\n\n<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>')
expect(response).to.beServedAs(200, 'DEBUG\n<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>')
done()
})

Expand Down

0 comments on commit f1aeaec

Please sign in to comment.