Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Jan 8, 2024
1 parent bf9e11c commit cda94e1
Showing 1 changed file with 260 additions and 1 deletion.
261 changes: 260 additions & 1 deletion test/lib/server/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ test.serial("Check if utf8 characters are correctly processed in version replace
});
});

test.serial("Check if manifest.json file is served properly by using manifestTransformer", async (t) => {
test.serial("manifestTransformer: request manifest.json with auto generated supported locales", async (t) => {
t.plan(4);

const readerWriter = resourceFactory.createAdapter({virBasePath: "/"});
Expand Down Expand Up @@ -618,3 +618,262 @@ test.serial("Check if manifest.json file is served properly by using manifestTra
t.is(setStringSpy.callCount, 1);
t.is(setHeaderSpy.getCall(0).lastArg, "application/json; charset=UTF-8");
});

test.serial("manifestTransformer: manifest.json with manual defined supported locales", async (t) => {
t.plan(4);

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

const input = `{
"_version": "1.58.0",
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"supportedLocales": ["fr", "de"],
"fallbackLocale": "de"
}
}
}
}
}`;
const expected = `{
"_version": "1.58.0",
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"supportedLocales": ["fr", "de"],
"fallbackLocale": "de"
}
}
}
}
}`;

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

const resource = await writeResource(readerWriter, "/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: "/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");
});

test.serial("manifestTransformer: no generation of supported locales " +
"if manifest.json version is below 1.21.0",
async (t) => {
t.plan(4);

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

const input = `{
"_version": "1.20.0",
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"fallbackLocale": "de"
}
}
}
}
}`;
const expected = `{
"_version": "1.20.0",
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"fallbackLocale": "de"
}
}
}
}
}`;

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

const resource = await writeResource(readerWriter, "/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: "/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");
});

test.serial("manifestTransformer: no generation of supported locales " +
"if manifest.json version is not defined",
async (t) => {
t.plan(4);

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

const input = `{
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"fallbackLocale": "de"
}
}
}
}
}`;
const expected = `{
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"fallbackLocale": "de"
}
}
}
}
}`;

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

const resource = await writeResource(readerWriter, "/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: "/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 cda94e1

Please sign in to comment.