Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add test runner middleware #293

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
60 changes: 60 additions & 0 deletions lib/middleware/testRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const {promisify} = require("util");
const fs = require("graceful-fs");
const readFile = promisify(fs.readFile);
const path = require("path");
const mime = require("mime-types");
const parseurl = require("parseurl");
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 = parseurl(req).pathname;
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