support form-urlencoded

This commit is contained in:
Simon Larsen 2023-11-17 13:58:46 +00:00
parent 544bbb2d2f
commit 57c13add9c
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
2 changed files with 84 additions and 44 deletions

View File

@ -243,11 +243,32 @@ export default class API {
}
try {
const finalHeaders: Dictionary<string> = {
...apiHeaders,
...headers,
};
let finalBody:
| JSONObject
| JSONArray
| URLSearchParams
| undefined = data;
// if content-type is form-url-encoded, then stringify the data
if (
finalHeaders['Content-Type'] ===
'application/x-www-form-urlencoded' &&
data
) {
finalBody = new URLSearchParams(data as Dictionary<string>);
}
const result: AxiosResponse = await axios({
method: method,
url: url.toString(),
headers: { ...apiHeaders, ...headers },
data,
headers: finalHeaders,
data: finalBody,
});
result.headers = await this.onResponseSuccessHeaders(

View File

@ -20,49 +20,68 @@ router.get(
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
try {
logger.info('Request Headers: ');
logger.info(req.headers);
logger.info('Request Body: ');
logger.info(req.body);
const responseCode: number | undefined =
LocalCache.getNumber('TestServer', 'responseCode') || 200;
const responseTime: number | undefined =
LocalCache.getNumber('TestServer', 'responseTime') || 0;
const responseBody: string | undefined =
LocalCache.getString('TestServer', 'responseBody') || '';
let responseHeaders: JSONValue | undefined =
LocalCache.getJSON('TestServer', 'responseHeaders') || {};
logger.info('Response Code: ' + responseCode);
logger.info('Response Time: ' + responseTime);
logger.info('Response Body: ');
logger.info(responseBody);
logger.info('Response Headers: ');
logger.info(responseHeaders);
if (responseHeaders && typeof responseHeaders === Typeof.String) {
responseHeaders = JSON.parse(responseHeaders.toString());
}
if (responseTime > 0) {
await Sleep.sleep(responseTime);
}
// middleware marks the probe as alive.
// so we don't need to do anything here.
return Response.sendCustomResponse(
req,
res,
responseCode,
responseBody,
responseHeaders ? (responseHeaders as any) : {}
);
} catch (err) {
return next(err);
}
await returnResponse(req, res, next);
}
);
router.post(
'/',
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
await returnResponse(req, res, next);
}
);
const returnResponse: Function = async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
try {
logger.info('Request Headers: ');
logger.info(req.headers);
logger.info('Request Body: ');
logger.info(req.body);
const responseCode: number | undefined =
LocalCache.getNumber('TestServer', 'responseCode') || 200;
const responseTime: number | undefined =
LocalCache.getNumber('TestServer', 'responseTime') || 0;
const responseBody: string | undefined =
LocalCache.getString('TestServer', 'responseBody') || '';
let responseHeaders: JSONValue | undefined =
LocalCache.getJSON('TestServer', 'responseHeaders') || {};
logger.info('Response Code: ' + responseCode);
logger.info('Response Time: ' + responseTime);
logger.info('Response Body: ');
logger.info(responseBody);
logger.info('Response Headers: ');
logger.info(responseHeaders);
if (responseHeaders && typeof responseHeaders === Typeof.String) {
responseHeaders = JSON.parse(responseHeaders.toString());
}
if (responseTime > 0) {
await Sleep.sleep(responseTime);
}
// middleware marks the probe as alive.
// so we don't need to do anything here.
return Response.sendCustomResponse(
req,
res,
responseCode,
responseBody,
responseHeaders ? (responseHeaders as any) : {}
);
} catch (err) {
return next(err);
}
};
export default router;