168 lines
4.8 KiB
TypeScript
168 lines
4.8 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import * as smartpath from '@push.rocks/smartpath';
|
|
import * as smartrequest from '@push.rocks/smartrequest';
|
|
|
|
import * as typedserver from '../ts/index.js';
|
|
|
|
let testServer: typedserver.TypedServer;
|
|
|
|
// =================
|
|
// Test TypedServer
|
|
// =================
|
|
|
|
tap.test('should create a valid TypedServer', async () => {
|
|
testServer = new typedserver.TypedServer({
|
|
cors: true,
|
|
domain: 'testing.git.zone',
|
|
forceSsl: false,
|
|
port: 3000,
|
|
appVersion: 'v3.2.1',
|
|
manifest: {
|
|
name: 'Test App',
|
|
short_name: 'testapp',
|
|
start_url: '/',
|
|
display: 'standalone',
|
|
background_color: '#000',
|
|
theme_color: '#000',
|
|
scope: '/',
|
|
lang: 'en',
|
|
display_override: ['window-controls-overlay'],
|
|
},
|
|
feed: true,
|
|
sitemap: true,
|
|
robots: true,
|
|
serveDir: smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
|
|
});
|
|
expect(testServer).toBeInstanceOf(typedserver.TypedServer);
|
|
});
|
|
|
|
// ================
|
|
// Test addRoute
|
|
// ================
|
|
|
|
tap.test('should add a POST route', async () => {
|
|
testServer.addRoute('/someroute', 'POST', async (ctx) => {
|
|
const body = await ctx.json();
|
|
console.log('request body is:');
|
|
console.log(body);
|
|
return new Response(JSON.stringify({ message: 'hi', received: body }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
});
|
|
});
|
|
|
|
tap.test('should add a GET route with params', async () => {
|
|
testServer.addRoute('/users/:id', 'GET', async (ctx) => {
|
|
const userId = ctx.params.id;
|
|
return new Response(JSON.stringify({ userId }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
});
|
|
});
|
|
|
|
// =====================
|
|
// Test typedrouter integration
|
|
// =====================
|
|
|
|
tap.test('should have a typedrouter', async () => {
|
|
expect(testServer.typedrouter).toBeDefined();
|
|
});
|
|
|
|
// =====================
|
|
// Start the server and test
|
|
// =====================
|
|
|
|
tap.test('should start the server', async () => {
|
|
await testServer.start();
|
|
});
|
|
|
|
// Test POST route
|
|
tap.test('should handle a POST request', async () => {
|
|
const smartRequestInstance = smartrequest.SmartRequest.create();
|
|
const response = await smartRequestInstance
|
|
.url('http://127.0.0.1:3000/someroute')
|
|
.headers({
|
|
'X-Forwarded-Proto': 'https',
|
|
'Content-Type': 'application/json',
|
|
})
|
|
.json({
|
|
someprop: 'hello world',
|
|
})
|
|
.post();
|
|
const responseBody = await response.json();
|
|
console.log('POST response:', responseBody);
|
|
expect(responseBody.message).toEqual('hi');
|
|
expect(responseBody.received.someprop).toEqual('hello world');
|
|
});
|
|
|
|
// Test GET route with params
|
|
tap.test('should handle a GET request with params', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/users/123');
|
|
const body = await response.json();
|
|
console.log('GET response:', body);
|
|
expect(body.userId).toEqual('123');
|
|
});
|
|
|
|
// Test static file serving
|
|
tap.test('should serve a static file', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/test.server.ts');
|
|
console.log('Static file status:', response.status);
|
|
expect(response.status).toEqual(200);
|
|
});
|
|
|
|
// Test CORS preflight
|
|
tap.test('should answer a preflight request', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/some/randompath/', {
|
|
method: 'OPTIONS',
|
|
});
|
|
console.log('Preflight headers:', Object.fromEntries(response.headers.entries()));
|
|
// CORS should return appropriate headers
|
|
expect(response.headers.get('access-control-allow-origin')).toBeDefined();
|
|
});
|
|
|
|
// Test sitemap endpoint
|
|
tap.test('should expose a sitemap', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/sitemap');
|
|
const text = await response.text();
|
|
console.log('Sitemap:', text);
|
|
expect(response.status).toEqual(200);
|
|
});
|
|
|
|
// Test robots.txt endpoint
|
|
tap.test('should expose robots.txt', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/robots.txt');
|
|
const text = await response.text();
|
|
console.log('Robots.txt:', text);
|
|
expect(response.status).toEqual(200);
|
|
});
|
|
|
|
// Test manifest endpoint
|
|
tap.test('should expose manifest.json', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/manifest.json');
|
|
const json = await response.json();
|
|
console.log('Manifest:', json);
|
|
expect(response.status).toEqual(200);
|
|
expect(json.name).toEqual('Test App');
|
|
});
|
|
|
|
// Test appversion endpoint
|
|
tap.test('should expose appversion', async () => {
|
|
const response = await fetch('http://127.0.0.1:3000/appversion');
|
|
const text = await response.text();
|
|
console.log('App version:', text);
|
|
expect(response.status).toEqual(200);
|
|
expect(text).toEqual('v3.2.1');
|
|
});
|
|
|
|
// ========
|
|
// Clean up
|
|
// ========
|
|
|
|
tap.test('should stop the server', async () => {
|
|
await testServer.stop();
|
|
});
|
|
|
|
export default tap.start();
|