Files
webrequest/test/test.all.ts

61 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { webrequest } from '../ts/index.js';
// Simple smoke tests for v4 API
// Test 1: Basic fetch-compatible API
tap.test('should work as fetch replacement', async () => {
const response = await webrequest('https://httpbin.org/get', {
method: 'GET',
});
expect(response).toBeInstanceOf(Response);
expect(response.ok).toEqual(true);
console.log('API response status:', response.status);
});
// Test 2: JSON convenience method
tap.test('should support getJson convenience method', async () => {
interface HttpBinResponse {
url: string;
headers: Record<string, string>;
}
const data = await webrequest.getJson<HttpBinResponse>('https://httpbin.org/get');
console.log('getJson url:', data.url);
expect(data).toHaveProperty('url');
expect(data).toHaveProperty('headers');
});
// Test 3: POST with JSON
tap.test('should support postJson', async () => {
interface PostResponse {
json: any;
url: string;
}
const data = await webrequest.postJson<PostResponse>(
'https://httpbin.org/post',
{ test: 'data' }
);
expect(data).toHaveProperty('url');
expect(data).toHaveProperty('json');
console.log('postJson works');
});
// Test 4: Caching
tap.test('should support caching', async () => {
const data1 = await webrequest.getJson('https://httpbin.org/get', {
cacheStrategy: 'cache-first'
});
const data2 = await webrequest.getJson('https://httpbin.org/get', {
cacheStrategy: 'cache-first'
});
expect(data1).toHaveProperty('url');
expect(data2).toHaveProperty('url');
console.log('Caching works');
});
export default tap.start();