Skip to content

Commit

Permalink
[FEATURE] Add test runner middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Mar 11, 2020
1 parent f8958c1 commit 22b52df
Show file tree
Hide file tree
Showing 10 changed files with 1,151 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# /node_modules/* and /bower_components/* ignored by default

# Ignore test runner static resources
lib/middleware/testRunner/

# Exclude coverage folder
coverage/

Expand Down
1 change: 1 addition & 0 deletions lib/middleware/MiddlewareManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class MiddlewareManager {
mountPath: "/discovery"
});
await this.addMiddleware("serveResources");
await this.addMiddleware("testRunner");
await this.addMiddleware("serveThemes");
await this.addMiddleware("versionInfo", {
mountPath: "/resources/sap-ui-version.json",
Expand Down
1 change: 1 addition & 0 deletions lib/middleware/middlewareRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const middlewares = {
versionInfo: "./versionInfo",
connectUi5Proxy: "./connectUi5Proxy",
serveThemes: "./serveThemes",
testRunner: "./testRunner",
nonReadRequests: "./nonReadRequests"
};

Expand Down
59 changes: 59 additions & 0 deletions lib/middleware/testRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const {promisify} = require("util");
const fs = require("graceful-fs");
const readFile = promisify(fs.readFile);
const path = require("path");
const mime = require("mime-types");
const log = require("@ui5/logger").getLogger("server:middleware:testRunner");

const testRunnerResourceRegEx = /\/test-resources\/sap\/ui\/qunit\/(testrunner\.(html|css)|TestRunner.js)$/;
const resourceCache = {};

function serveResource(res, resourcePath, resourceContent) {
const type = mime.lookup(resourcePath) || "application/octet-stream";
const charset = mime.charset(type);
const contentType = type + (charset ? "; charset=" + charset : "");

// resources served by this middleware do not change often
res.setHeader("Cache-Control", "public, max-age=1800");

res.setHeader("Content-Type", contentType);
res.end(resourceContent);
}

/**
* Creates and returns the middleware to serve a resource index.
*
* @module @ui5/server/middleware/testRunner
* @param {object} parameters Parameters
* @param {object} parameters.resources Contains the resource reader or collection to access project related files
* @returns {Function} Returns a server middleware closure.
*/
function createMiddleware({resources}) {
return async function(req, res, next) {
try {
const pathname = req.path; // TODO: Other middlewares use the parseurl module here but I don't get why
const parts = testRunnerResourceRegEx.exec(pathname);
const resourceName = parts && parts[1];

if (resourceName) { // either "testrunner.html", "testrunner.css" or "TestRunner.js" (case sensitive!)
log.verbose(`Serving ${pathname}`);
let pResource;
if (!resourceCache[pathname]) {
pResource = readFile(path.join(__dirname, "testRunner", resourceName), {encoding: "utf8"});
resourceCache[pathname] = pResource;
} else {
pResource = resourceCache[pathname];
}

const resourceContent = await pResource;
serveResource(res, pathname, resourceContent);
} else {
next();
}
} catch (err) {
next(err);
}
};
}

module.exports = createMiddleware;
Loading

0 comments on commit 22b52df

Please sign in to comment.