typedserver/test/test.server.ts

127 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-03-30 13:15:48 +00:00
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@pushrocks/tapbundle';
// helper dependencies
// tslint:disable-next-line:no-implicit-dependencies
import * as smartpath from '@pushrocks/smartpath';
import * as smartrequest from '@pushrocks/smartrequest';
import * as typedserver from '../ts/index.js';
let testServer: typedserver.servertools.Server;
let testRoute: typedserver.servertools.Route;
let testRoute2: typedserver.servertools.Route;
let testHandler: typedserver.servertools.Handler;
// =================
// Test class Server
// =================
tap.test('should create a valid Server', async () => {
testServer = new typedserver.servertools.Server({
cors: true,
domain: 'testing.git.zone',
forceSsl: false,
appVersion: 'v3.2.1',
manifest: {
name: 'Test App',
short_name: 'testapp',
},
feed: true,
sitemap: true,
robots: true,
});
expect(testServer).toBeInstanceOf(typedserver.servertools.Server);
});
// ================
// Test class Route
// ================
tap.test('should create a valid Route', async () => {
testRoute = testServer.addRoute('/someroute');
testRoute2 = testServer.addRoute('/someroute/*');
expect(testRoute).toBeInstanceOf(typedserver.servertools.Route);
});
// ==================
// Test class Handler
// ==================
tap.test('should produce a valid handler', async () => {
testHandler = new typedserver.servertools.Handler('POST', (request, response) => {
console.log('request body is:');
console.log(request.body);
response.send('hi');
});
expect(testHandler).toBeInstanceOf(typedserver.servertools.Handler);
});
tap.test('should add handler to route', async () => {
testRoute.addHandler(testHandler);
});
tap.test('should create a valid StaticHandler', async () => {
testRoute2.addHandler(
new typedserver.servertools.HandlerStatic(smartpath.get.dirnameFromImportMetaUrl(import.meta.url))
);
});
tap.test('should add typedrequest and typedsocket', async () => {
const typedrequest = await import('@apiglobal/typedrequest');
const typedrouter = new typedrequest.TypedRouter();
testServer.addTypedRequest(typedrouter);
testServer.addTypedSocket(typedrouter);
});
// =====================
// start the server and test the configuration
// =====================
tap.test('should start the server allright', async () => {
await testServer.start(3000);
});
// see if a demo request holds up
tap.test('should issue a request', async (tools) => {
2023-03-30 14:42:02 +00:00
const response = await smartrequest.postJson('http://127.0.0.1:3000/someroute', {
2023-03-30 13:15:48 +00:00
headers: {
'X-Forwarded-Proto': 'https',
},
requestBody: {
someprop: 'hi',
},
});
console.log(response.body);
});
tap.test('should get a file from disk', async () => {
2023-03-30 14:42:02 +00:00
const response = await fetch('http://127.0.0.1:3000/someroute/testresponse.js');
2023-03-30 13:15:48 +00:00
console.log(response.status);
console.log(response.headers);
});
tap.test('should answer a preflight request', async () => {
2023-03-30 14:42:02 +00:00
const response = await fetch('http://127.0.0.1:3000/some/randompath/', {
2023-03-30 13:15:48 +00:00
method: 'OPTIONS',
});
console.log(response.headers);
});
tap.test('should exposer a sitemap', async () => {
2023-03-30 14:42:02 +00:00
const response = await fetch('http://127.0.0.1:3000/sitemap');
2023-03-30 13:15:48 +00:00
console.log(await response.text());
});
// ========
// clean up
// ========
tap.test('should stop the server', async () => {
await testServer.stop();
});
tap.start();