-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add test runner middleware
Static resources are copied over from OpenUI5.
- Loading branch information
1 parent
b910a67
commit ea77e20
Showing
10 changed files
with
1,151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.