Skip to content

Commit 1ee6723

Browse files
committed
[FIX] serveResources: Correctly encode non UTF-8 resources
The replaceStream module converts all string it processes to UTF-8. Therefore, stop using it for strings that are not UTF-8 encoded. Resolves https://github.com/SAP/ui5-server/issues/196
1 parent 59eecb2 commit 1ee6723

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

lib/middleware/serveResources.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function createMiddleware({resourceCollections}) {
6363

6464
let stream = resource.getStream();
6565

66-
if ((type.startsWith("text/") || type === "application/javascript")) {
66+
if (charset === "UTF-8" && (type.startsWith("text/") || type === "application/javascript")) {
6767
if (resource._project) {
6868
stream = stream.pipe(replaceStream("${version}", resource._project.version));
6969
} else {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
showHelloButtonText=Say �!
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>Application A</title>
4+
<title>Application A - Version ${version}</title>
55
</head>
66
<body>
77

88
</body>
9-
</html>
9+
</html>

test/lib/server/acceptRemoteConnections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ test("Get resource from application.a (/index.html) with enabled remote connecti
4141
}
4242
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
4343
t.regex(res.headers["content-type"], /html/, "Correct content type");
44-
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
44+
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
4545
});
4646
});

test/lib/server/h2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ test("Get resource from application.a (/index.html)", (t) => {
5454
}
5555
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
5656
t.regex(res.headers["content-type"], /html/, "Correct content type");
57-
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
57+
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
5858
});
5959
});

test/lib/server/main.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test.before((t) => {
2121
});
2222
});
2323

24-
test.after(() => {
24+
test.after.always(() => {
2525
return new Promise((resolve, reject) => {
2626
serve.close((error) => {
2727
if (error) {
@@ -40,7 +40,7 @@ test("Get resource from application.a (/index.html)", (t) => {
4040
}
4141
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
4242
t.regex(res.headers["content-type"], /html/, "Correct content type");
43-
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
43+
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
4444
});
4545
});
4646

@@ -51,10 +51,27 @@ test("Get resource from application.a (/i18n/i18n.properties) with correct chars
5151
}
5252
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
5353
t.deepEqual(res.headers["content-type"], "text/plain; charset=ISO-8859-1", "Correct content type and charset");
54-
t.deepEqual(res.text, "showHelloButtonText=Say Hello!", "Correct response");
54+
t.deepEqual(Buffer.from(res.text, "latin1").toString(), "showHelloButtonText=Say Hello!", "Correct response");
5555
});
5656
});
5757

58+
test("Get resource from application.a (/i18n/i18n_de.properties) with correct encoding 'ISO-8859-1'", (t) => {
59+
return request.get("/i18n/i18n_de.properties")
60+
.responseType("arraybuffer")
61+
.then((res) => {
62+
if (res.error) {
63+
t.fail(res.error.text);
64+
}
65+
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
66+
t.deepEqual(res.headers["content-type"], "text/plain; charset=ISO-8859-1",
67+
"Correct content type and charset");
68+
69+
t.deepEqual(res.body.toString("latin1"), "showHelloButtonText=Say ä!", "Correct response");
70+
// Because it took so long to figure this out I keep the below line. It is equivalent to the deepEqual above
71+
// t.deepEqual(res.body.toString("latin1"), Buffer.from("showHelloButtonText=Say \u00e4!", "latin1").toString("latin1"),
72+
// "Correct response");
73+
});
74+
});
5875

5976
test("Get resource from library.a (/resources/library/a/.library)", (t) => {
6077
return request.get("/resources/library/a/.library").then((res) => {

0 commit comments

Comments
 (0)