update
This commit is contained in:
95
test/test.node.ts
Normal file
95
test/test.node.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
|
||||
import { SmartRequestClient } from '../ts/client/index.js';
|
||||
|
||||
tap.test('client: should request a html document over https', async () => {
|
||||
const response = await SmartRequestClient.create()
|
||||
.url('https://encrypted.google.com/')
|
||||
.get();
|
||||
|
||||
expect(response).not.toBeNull();
|
||||
expect(response).toHaveProperty('status');
|
||||
expect(response.status).toBeGreaterThan(0);
|
||||
const text = await response.text();
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
tap.test('client: should request a JSON document over https', async () => {
|
||||
const response = await SmartRequestClient.create()
|
||||
.url('https://jsonplaceholder.typicode.com/posts/1')
|
||||
.get();
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toHaveProperty('id');
|
||||
expect(body.id).toEqual(1);
|
||||
});
|
||||
|
||||
tap.test('client: 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();
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toHaveProperty('json');
|
||||
expect(body.json).toHaveProperty('text');
|
||||
expect(body.json.text).toEqual('example_text');
|
||||
});
|
||||
|
||||
tap.test('client: 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();
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toHaveProperty('headers');
|
||||
|
||||
// Check if the header exists (case-sensitive)
|
||||
expect(body.headers).toHaveProperty(customHeader);
|
||||
expect(body.headers[customHeader]).toEqual(headerValue);
|
||||
});
|
||||
|
||||
tap.test('client: should handle query parameters', async () => {
|
||||
const params = { param1: 'value1', param2: 'value2' };
|
||||
|
||||
const response = await SmartRequestClient.create()
|
||||
.url('https://httpbin.org/get')
|
||||
.query(params)
|
||||
.get();
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toHaveProperty('args');
|
||||
expect(body.args).toHaveProperty('param1');
|
||||
expect(body.args.param1).toEqual('value1');
|
||||
expect(body.args).toHaveProperty('param2');
|
||||
expect(body.args.param2).toEqual('value2');
|
||||
});
|
||||
|
||||
tap.test('client: 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('ok');
|
||||
expect(response.ok).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('client: 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('ok');
|
||||
expect(response.ok).toBeTrue();
|
||||
});
|
||||
|
||||
tap.start();
|
Reference in New Issue
Block a user