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 1 commit
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think req.path might contain a query. That's why parseurl is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this stem from the time when our middlewares have been used in different server environments? See e.g. #183 .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codeworrior thank you for digging that out!

The express documentation makes it pretty clear that req.path does not contain the query: https://expressjs.com/en/api.html#req.path

Since we already use parseurl elsewhere, I guess it doesn't hurt to offer the same compatibility as the other middlewares. I'll change the code 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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