Skip to content

Commit

Permalink
[FIX] serveResources: Do not process manifest.json in test-resources
Browse files Browse the repository at this point in the history
The middleware is supposed to be in sync with the behavior of the build.
During the build, only manifest.json files within `/resources/` are
processed, so the server must not process files from `/test-resources/`.

A check for `/resources/` would not be correct, as the pathname is based
on the 'runtime'-style, so for applications the path might just be
`/manifest.json`.
  • Loading branch information
matz3 committed Aug 9, 2024
1 parent b8bc722 commit 964e784
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rProperties = /\.properties$/i;
const rReplaceVersion = /\.(library|js|json)$/i;
const rManifest = /\/manifest\.json$/i;
const rResourcesPrefix = /^\/resources\//i;
const rTestResourcesPrefix = /^\/test-resources\//i;

function isFresh(req, res) {
return fresh(req.headers, {
Expand Down Expand Up @@ -49,9 +50,13 @@ function createMiddleware({resources, middlewareUtil}) {
next();
return;
}
} else if (rManifest.test(resource.getPath()) && resource.getProject()?.getNamespace()) {
} else if (
rManifest.test(pathname) && !rTestResourcesPrefix.test(pathname) &&
resource.getProject()?.getNamespace()
) {
// Special handling for manifest.json file by adding additional content to the served manifest.json
// NOTE: This should only be done for manifest.json files that exist in the sources.
// NOTE: This should only be done for manifest.json files that exist in the sources,
// not in test-resources.
// Files created by generateLibraryManifest (see above) should not be handled in here.
// Only manifest.json files in library / application projects should be handled.
// resource.getProject.getNamespace() returns null for all other kind of projects.
Expand Down
66 changes: 66 additions & 0 deletions test/lib/server/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,3 +1338,69 @@ test.serial("manifestEnhancer: no generation of supported locales for theme libr
t.is(setStringSpy.callCount, 0);
t.is(setHeaderSpy.getCall(0).lastArg, "application/json; charset=UTF-8");
});

test.serial("manifestEnhancer: no processing of files within /test-resources/", async (t) => {
t.plan(4);

const readerWriter = resourceFactory.createAdapter({virBasePath: "/"});

const input = `{
"_version": "1.58.0",
"sap.app": {
"id": "sap.ui.demo.lib",
"type": "library"
},
"sap.ui5": {
"library": {
"i18n": "messagebundle.properties"
}
}
}`;

const expected = input;

const project = {
getNamespace: () => "sap.ui.demo.lib",
getVersion: () => "1.0.0",
getReader: () => readerWriter
};

const resource = await writeResource(readerWriter, "/test-resources/sap/ui/demo/lib/manifest.json", 1024 * 1024,
input, "utf8", project
);
const setStringSpy = sinon.spy(resource, "setString");

const serveResourcesMiddlewareWithMock = t.context.serveResourcesMiddlewareWithMock =
await esmock.p("../../../../lib/middleware/serveResources", {
"@ui5/fs/fsInterface": sinon.stub().returns({
readdir(fsPath, callback) {
t.fail("fs.readdir should never be called");
}
})
});
const middleware = serveResourcesMiddlewareWithMock({
middlewareUtil: new MiddlewareUtil({graph: "graph", project: "project"}),
resources: {
all: readerWriter
}
});

const response = fakeResponse;

const setHeaderSpy = sinon.spy(response, "setHeader");
const req = {
url: "/test-resources/sap/ui/demo/lib/manifest.json",
headers: {}
};
const next = function(err) {
throw new Error(`Next callback called with error: ${err.stack}`);
};

await middleware(req, response, next);
const content = await resource.getString();

t.is(content, expected);
t.is(setHeaderSpy.callCount, 2);
t.is(setStringSpy.callCount, 0);
t.is(setHeaderSpy.getCall(0).lastArg, "application/json; charset=UTF-8");
});

0 comments on commit 964e784

Please sign in to comment.