feat(docs): Enhance documentation and tests with modern API usage examples and migration guide
This commit is contained in:
@ -14,10 +14,12 @@ tap.test('should request a JSON document over https', async () => {
|
||||
});
|
||||
|
||||
tap.test('should post a JSON document over http', async () => {
|
||||
await expectAsync(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
|
||||
const testData = { text: 'example_text' };
|
||||
await expectAsync(smartrequest.postJson('https://httpbin.org/post', { requestBody: testData }))
|
||||
.property('body')
|
||||
.property('md5')
|
||||
.toEqual('fa4c6baa0812e5b5c80ed8885e55a8a6');
|
||||
.property('json')
|
||||
.property('text')
|
||||
.toEqual('example_text');
|
||||
});
|
||||
|
||||
tap.test('should safe get stuff', async () => {
|
86
test/test.modern.ts
Normal file
86
test/test.modern.ts
Normal file
@ -0,0 +1,86 @@
|
||||
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();
|
Reference in New Issue
Block a user