2020-07-04 15:22:44 +00:00
|
|
|
import * as plugins from './test-sdk.plugins';
|
2020-07-05 17:53:49 +00:00
|
|
|
import { AgTestServer } from './test-sdk.classes.testserver';
|
2020-07-04 15:22:44 +00:00
|
|
|
|
2020-07-25 13:34:26 +00:00
|
|
|
export {
|
|
|
|
AgTestServer
|
|
|
|
}
|
2020-07-05 17:53:49 +00:00
|
|
|
|
|
|
|
let testServer: AgTestServer;
|
|
|
|
export const createTestServer = async (handlerArg: plugins.agSdk.AAgHandler<any>) => {
|
|
|
|
testServer = new AgTestServer(handlerArg);
|
|
|
|
await testServer.start();
|
|
|
|
return testServer;
|
|
|
|
};
|
|
|
|
|
2020-07-25 13:34:26 +00:00
|
|
|
export const testFire = <
|
|
|
|
A extends plugins.agSdk.AAgHandler<any>,
|
|
|
|
T extends plugins.typedrequestInterfaces.ITypedRequest
|
|
|
|
>(
|
|
|
|
slug: string,
|
|
|
|
methodArg: T['method'],
|
|
|
|
requestArg: T['request']
|
|
|
|
) => {
|
|
|
|
if (!testServer) {
|
|
|
|
throw new Error('you need to create and start a testServer first!');
|
|
|
|
}
|
|
|
|
if (testServer.server.serverStatus !== 'running') {
|
|
|
|
throw new Error('you need to start the testServer first!');
|
|
|
|
}
|
|
|
|
const typedRequest = new plugins.typedrequest.TypedRequest<T>(
|
|
|
|
`https://localhost:${testServer.server.options.port}`,
|
|
|
|
methodArg
|
|
|
|
);
|
|
|
|
const responsePromise = typedRequest.fire(requestArg);
|
|
|
|
const expect = async (expectedResponseArg: T['response']) => {
|
|
|
|
const actualResponse = await responsePromise;
|
|
|
|
const comparisonResult = plugins.smartobject.compareObjects(expectedResponseArg, actualResponse);
|
|
|
|
let throwErrorBool = false;
|
|
|
|
if (comparisonResult.divergingProperties.length > 0) {
|
|
|
|
console.log(`The following properties diverged:`);
|
|
|
|
console.log(comparisonResult.divergingProperties);
|
|
|
|
throwErrorBool = true;
|
|
|
|
}
|
|
|
|
if (comparisonResult.missingProperties.length > 0) {
|
|
|
|
console.log(`The following properties diverged:`);
|
|
|
|
console.log(comparisonResult.divergingProperties);
|
|
|
|
throwErrorBool = true;
|
|
|
|
}
|
|
|
|
if (throwErrorBool) {
|
|
|
|
throw new Error('response did not comply');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return expect;
|
|
|
|
};
|
|
|
|
|
2020-07-05 17:53:49 +00:00
|
|
|
export const stopTestServer = async () => {
|
|
|
|
if (testServer) {
|
|
|
|
await testServer.stop();
|
|
|
|
}
|
|
|
|
};
|