Skip to content

Commit

Permalink
[FIX] serveResources: Correctly encode non UTF-8 resources
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RandomByte committed Jun 24, 2019
1 parent 59eecb2 commit 1ee6723
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function createMiddleware({resourceCollections}) {

let stream = resource.getStream();

if ((type.startsWith("text/") || type === "application/javascript")) {
if (charset === "UTF-8" && (type.startsWith("text/") || type === "application/javascript")) {
if (resource._project) {
stream = stream.pipe(replaceStream("${version}", resource._project.version));
} else {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/application.a/webapp/i18n/i18n_de.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
showHelloButtonText=Say �!
4 changes: 2 additions & 2 deletions test/fixtures/application.a/webapp/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Application A</title>
<title>Application A - Version ${version}</title>
</head>
<body>

</body>
</html>
</html>
2 changes: 1 addition & 1 deletion test/lib/server/acceptRemoteConnections.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ test("Get resource from application.a (/index.html) with enabled remote connecti
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
});
});
2 changes: 1 addition & 1 deletion test/lib/server/h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ test("Get resource from application.a (/index.html)", (t) => {
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
});
});
23 changes: 20 additions & 3 deletions test/lib/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test.before((t) => {
});
});

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

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

test("Get resource from application.a (/i18n/i18n_de.properties) with correct encoding 'ISO-8859-1'", (t) => {
return request.get("/i18n/i18n_de.properties")
.responseType("arraybuffer")
.then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.deepEqual(res.headers["content-type"], "text/plain; charset=ISO-8859-1",
"Correct content type and charset");

t.deepEqual(res.body.toString("latin1"), "showHelloButtonText=Say ä!", "Correct response");
// Because it took so long to figure this out I keep the below line. It is equivalent to the deepEqual above
// t.deepEqual(res.body.toString("latin1"), Buffer.from("showHelloButtonText=Say \u00e4!", "latin1").toString("latin1"),
// "Correct response");
});
});

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

0 comments on commit 1ee6723

Please sign in to comment.