typedserver/test/test.server.ts

136 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-03-30 13:15:48 +00:00
// tslint:disable-next-line:no-implicit-dependencies
2023-08-03 18:45:09 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2023-03-30 13:15:48 +00:00
// helper dependencies
// tslint:disable-next-line:no-implicit-dependencies
2023-08-03 18:45:09 +00:00
import * as smartpath from '@push.rocks/smartpath';
import * as smartrequest from '@push.rocks/smartrequest';
2023-03-30 13:15:48 +00:00
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',
2023-08-03 18:50:18 +00:00
start_url: '/',
display: 'standalone',
background_color: '#000',
theme_color: '#000',
scope: '/',
lang: 'en',
display_override: ['window-controls-overlay'],
2023-03-30 13:15:48 +00:00
},
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(
2023-07-01 10:29:35 +00:00
new typedserver.servertools.HandlerStatic(
smartpath.get.dirnameFromImportMetaUrl(import.meta.url)
)
2023-03-30 13:15:48 +00:00
);
});
tap.test('should add typedrequest and typedsocket', async () => {
2023-08-06 16:15:01 +00:00
const typedrequest = await import('@api.global/typedrequest');
2023-03-30 13:15:48 +00:00
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();