Skip to content

Commit f1aeaec

Browse files
committedOct 28, 2021
fix(middleware): replace %X_UA_COMPATIBLE% marker anywhere in the file
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
1 parent 36d41cb commit f1aeaec

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed
 

‎lib/middleware/karma.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function getQuery (urlStr) {
4545
function getXUACompatibleMetaElement (url) {
4646
const query = getQuery(url)
4747
if (query['x-ua-compatible']) {
48-
return `\n<meta http-equiv="X-UA-Compatible" content="${query['x-ua-compatible']}"/>`
48+
return `<meta http-equiv="X-UA-Compatible" content="${query['x-ua-compatible']}"/>`
4949
}
5050
return ''
5151
}
@@ -107,7 +107,7 @@ function createKarmaMiddleware (
107107
} else { // serve client.html
108108
return serveStaticFile('/client.html', requestedRangeHeader, response, (data) =>
109109
data
110-
.replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
110+
.replace('%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
111111
.replace('%X_UA_COMPATIBLE_URL%', getXUACompatibleUrl(request.url)))
112112
}
113113
}
@@ -226,7 +226,7 @@ function createKarmaMiddleware (
226226
.replace('%CLIENT_CONFIG%', 'window.__karma__.config = ' + JSON.stringify(client) + ';\n')
227227
.replace('%SCRIPT_URL_ARRAY%', () => 'window.__karma__.scriptUrls = ' + JSON.stringify(scriptUrls) + ';\n')
228228
.replace('%MAPPINGS%', () => 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n')
229-
.replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
229+
.replace('%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url))
230230
})
231231
})
232232
} else if (requestUrl === '/context.json') {

‎static/client.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-->
66
<html>
77
<head>
8-
%X_UA_COMPATIBLE%
8+
%X_UA_COMPATIBLE%
99
<title>Karma</title>
1010
<link href="favicon.ico" rel="icon" type="image/x-icon">
1111
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

‎static/debug.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-->
77
<html>
88
<head>
9-
%X_UA_COMPATIBLE%
9+
%X_UA_COMPATIBLE%
1010
<title>Karma DEBUG RUNNER</title>
1111
<link href="favicon.ico" rel="icon" type="image/x-icon" />
1212
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

‎test/unit/middleware/karma.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ describe('middleware.karma', () => {
2727
const fsMock = mocks.fs.create({
2828
karma: {
2929
static: {
30-
'client.html': mocks.fs.file(0, 'CLIENT HTML\n%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%'),
30+
'client.html': mocks.fs.file(0, 'CLIENT HTML%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%'),
3131
'client_with_context.html': mocks.fs.file(0, 'CLIENT_WITH_CONTEXT\n%SCRIPT_URL_ARRAY%'),
3232
'context.html': mocks.fs.file(0, 'CONTEXT\n%SCRIPTS%'),
33-
'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%\n%X_UA_COMPATIBLE%'),
33+
'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%%X_UA_COMPATIBLE%'),
3434
'karma.js': mocks.fs.file(0, 'root: %KARMA_URL_ROOT%, proxy: %KARMA_PROXY_PATH%, v: %KARMA_VERSION%')
3535
}
3636
}
@@ -170,7 +170,7 @@ describe('middleware.karma', () => {
170170

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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.