Fixed mock-api to accept POST requests

This commit is contained in:
Erik Tiekstra
2021-08-12 11:23:55 +02:00
parent 9c1f88e6a7
commit 35213d6aba
2 changed files with 10 additions and 5 deletions

View File

@@ -82,6 +82,7 @@ const apiData = {
currentUser: currentUser.generate(),
authorizations: authorizations.generate(),
getTokenFullAccess: authTokens.auth_code_from_CIAM_with_all_permissions,
invites: [],
};
fs.writeFileSync('api.json', JSON.stringify(apiData, null, '\t'));

View File

@@ -30,20 +30,24 @@ server.use(
);
router.render = (req, res) => {
const pathname = req._parsedOriginalUrl.pathname;
const method = req.originalMethod;
const parsedUrl = method === 'GET' ? req._parsedOriginalUrl : req._parsedUrl;
const pathname = parsedUrl.pathname;
const params = parsedUrl.query ? new URLSearchParams(parsedUrl.query) : null;
const requestHeaders = req.headers;
// all paths except /auth/token requires Authorization header.
if (!pathname.includes('/auth/token') && !req.headers.authorization) {
if (!pathname.includes('/auth/token') && !requestHeaders.authorization) {
return res.status(401).jsonp({ error: 'No valid access-token' });
}
// Return custom error when status is 404.
if (res.statusCode === 404) {
return res.status(404).jsonp({ error: `Can't find path: ${pathname}` });
}
const params = req._parsedUrl.query ? new URLSearchParams(req._parsedUrl.query) : null;
// Add createdAt to the body
if (req.originalMethod === 'POST') {
if (method === 'POST') {
req.body.createdAt = Date.now();
}