Files
webrequest/test/test.ts

79 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2023-07-27 13:59:21 +02:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-03-16 16:23:32 +01:00
import * as webrequest from '../ts/index.js';
2018-12-04 17:35:40 +01:00
2018-12-03 18:35:30 +01:00
// test dependencies
2023-07-09 17:24:52 +02:00
import * as typedserver from '@apiglobal/typedserver';
2018-12-03 18:35:30 +01:00
2023-07-09 17:24:52 +02:00
let testServer: typedserver.servertools.Server;
2018-12-03 18:35:30 +01:00
tap.test('setup test server', async () => {
2023-07-09 17:24:52 +02:00
testServer = new typedserver.servertools.Server({
2018-12-03 18:35:30 +01:00
cors: false,
forceSsl: false,
2020-10-09 10:14:45 +00:00
port: 2345,
2018-12-03 18:35:30 +01:00
});
testServer.addRoute(
'/apiroute1',
2023-07-09 17:24:52 +02:00
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(429);
res.end();
})
);
2018-12-03 18:35:30 +01:00
testServer.addRoute(
'/apiroute2',
2023-07-09 17:24:52 +02:00
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(500);
res.end();
})
);
2018-12-04 17:35:40 +01:00
testServer.addRoute(
'/apiroute3',
2023-07-09 17:24:52 +02:00
new typedserver.servertools.Handler('GET', (req, res) => {
res.status(200);
res.send({
2020-10-09 10:14:45 +00:00
hithere: 'hi',
});
})
);
2018-12-04 17:35:40 +01:00
2019-06-04 15:23:47 +02:00
await testServer.start();
});
2018-12-03 18:35:30 +01:00
2020-10-09 10:14:45 +00:00
tap.test('first test', async (tools) => {
2023-02-12 17:54:00 +01:00
const response = await (
await new webrequest.WebRequest().requestMultiEndpoint(
[
'http://localhost:2345/apiroute1',
'http://localhost:2345/apiroute2',
'http://localhost:2345/apiroute4',
'http://localhost:2345/apiroute3',
],
{
method: 'GET',
}
)
).json();
2018-12-04 17:35:40 +01:00
2019-09-27 20:16:28 +02:00
const response2 = await new webrequest.WebRequest().getJson('http://localhost:2345/apiroute3');
2020-10-15 18:46:21 +00:00
console.log('response 1: ' + JSON.stringify(response));
console.log('response 2: ' + JSON.stringify(response2));
2018-12-04 17:35:40 +01:00
2022-02-10 18:45:22 +01:00
expect(response).toHaveProperty('hithere'); //.to.equal('hi');
expect(response2).toHaveProperty('hithere'); //.to.equal('hi');
});
2018-12-04 17:35:40 +01:00
2022-05-29 20:22:42 +02:00
tap.test('should cache response', async () => {
const webrequestInstance = new webrequest.WebRequest();
const response = await webrequestInstance.getJson('http://localhost:2345/apiroute3', true);
expect(response).toHaveProperty('hithere');
await testServer.stop();
const response2 = await webrequestInstance.getJson('http://localhost:2345/apiroute3', true);
expect(response2).toHaveProperty('hithere');
2023-02-12 17:54:00 +01:00
});
2018-11-30 17:12:48 +01:00
tap.start();