87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import { tap, expect } from '@pushrocks/tapbundle';
|
|
|
|
import { SmartRequestClient } from '../ts/modern/index.js';
|
|
|
|
tap.test('modern: should request a html document over https', async () => {
|
|
const response = await SmartRequestClient.create()
|
|
.url('https://encrypted.google.com/')
|
|
.get();
|
|
|
|
expect(response).toHaveProperty('body');
|
|
});
|
|
|
|
tap.test('modern: should request a JSON document over https', async () => {
|
|
const response = await SmartRequestClient.create()
|
|
.url('https://jsonplaceholder.typicode.com/posts/1')
|
|
.get();
|
|
|
|
expect(response.body).toHaveProperty('id');
|
|
expect(response.body.id).toEqual(1);
|
|
});
|
|
|
|
tap.test('modern: should post a JSON document over http', async () => {
|
|
const testData = { text: 'example_text' };
|
|
const response = await SmartRequestClient.create()
|
|
.url('https://httpbin.org/post')
|
|
.json(testData)
|
|
.post();
|
|
|
|
expect(response.body).toHaveProperty('json');
|
|
expect(response.body.json).toHaveProperty('text');
|
|
expect(response.body.json.text).toEqual('example_text');
|
|
});
|
|
|
|
tap.test('modern: should set headers correctly', async () => {
|
|
const customHeader = 'X-Custom-Header';
|
|
const headerValue = 'test-value';
|
|
|
|
const response = await SmartRequestClient.create()
|
|
.url('https://httpbin.org/headers')
|
|
.header(customHeader, headerValue)
|
|
.get();
|
|
|
|
|
|
expect(response.body).toHaveProperty('headers');
|
|
|
|
// Check if the header exists (case-sensitive)
|
|
expect(response.body.headers).toHaveProperty(customHeader);
|
|
expect(response.body.headers[customHeader]).toEqual(headerValue);
|
|
});
|
|
|
|
tap.test('modern: should handle query parameters', async () => {
|
|
const params = { param1: 'value1', param2: 'value2' };
|
|
|
|
const response = await SmartRequestClient.create()
|
|
.url('https://httpbin.org/get')
|
|
.query(params)
|
|
.get();
|
|
|
|
expect(response.body).toHaveProperty('args');
|
|
expect(response.body.args).toHaveProperty('param1');
|
|
expect(response.body.args.param1).toEqual('value1');
|
|
expect(response.body.args).toHaveProperty('param2');
|
|
expect(response.body.args.param2).toEqual('value2');
|
|
});
|
|
|
|
tap.test('modern: should handle timeout configuration', async () => {
|
|
// This test just verifies that the timeout method doesn't throw
|
|
const client = SmartRequestClient.create()
|
|
.url('https://httpbin.org/get')
|
|
.timeout(5000);
|
|
|
|
const response = await client.get();
|
|
expect(response).toHaveProperty('body');
|
|
});
|
|
|
|
tap.test('modern: should handle retry configuration', async () => {
|
|
// This test just verifies that the retry method doesn't throw
|
|
const client = SmartRequestClient.create()
|
|
.url('https://httpbin.org/get')
|
|
.retry(1);
|
|
|
|
const response = await client.get();
|
|
expect(response).toHaveProperty('body');
|
|
});
|
|
|
|
tap.start();
|