This commit is contained in:
2025-07-28 22:37:36 +00:00
parent bc99aa3569
commit eb2ccd8d9f
21 changed files with 228 additions and 99 deletions

102
test/test.browser.ts Normal file
View File

@@ -0,0 +1,102 @@
import { tap, expect } from '@pushrocks/tapbundle';
// For browser tests, we need to import from a browser-safe path
// that doesn't trigger Node.js module imports
import { CoreRequest, CoreResponse } from '../ts/core/index.js';
import type { ICoreRequestOptions } from '../ts/core_base/types.js';
tap.test('browser: should request a JSON document over https', async () => {
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts/1');
const response = await request.fire();
expect(response).not.toBeNull();
expect(response).toHaveProperty('status');
expect(response.status).toEqual(200);
const data = await response.json();
expect(data).toHaveProperty('id');
expect(data.id).toEqual(1);
expect(data).toHaveProperty('title');
});
tap.test('browser: should handle CORS requests', async () => {
const options: ICoreRequestOptions = {
headers: {
'Accept': 'application/vnd.github.v3+json'
}
};
const request = new CoreRequest('https://api.github.com/users/github', options);
const response = await request.fire();
expect(response).not.toBeNull();
expect(response.status).toEqual(200);
const data = await response.json();
expect(data).toHaveProperty('login');
expect(data.login).toEqual('github');
});
tap.test('browser: should handle request timeouts', async () => {
let timedOut = false;
const options: ICoreRequestOptions = {
timeout: 1000
};
try {
const request = new CoreRequest('https://httpbin.org/delay/10', options);
await request.fire();
} catch (error) {
timedOut = true;
expect(error.message).toContain('timed out');
}
expect(timedOut).toEqual(true);
});
tap.test('browser: should handle POST requests with JSON', async () => {
const testData = {
title: 'foo',
body: 'bar',
userId: 1
};
const options: ICoreRequestOptions = {
method: 'POST',
requestBody: testData
};
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts', options);
const response = await request.fire();
expect(response.status).toEqual(201);
const responseData = await response.json();
expect(responseData).toHaveProperty('id');
expect(responseData.title).toEqual(testData.title);
expect(responseData.body).toEqual(testData.body);
expect(responseData.userId).toEqual(testData.userId);
});
tap.test('browser: should handle query parameters', async () => {
const options: ICoreRequestOptions = {
queryParams: {
foo: 'bar',
baz: 'qux'
}
};
const request = new CoreRequest('https://httpbin.org/get', options);
const response = await request.fire();
expect(response.status).toEqual(200);
const data = await response.json();
expect(data.args).toHaveProperty('foo');
expect(data.args.foo).toEqual('bar');
expect(data.args).toHaveProperty('baz');
expect(data.args.baz).toEqual('qux');
});
export default tap.start();

View File

@@ -1,8 +1,8 @@
import { tap, expect } from '@pushrocks/tapbundle';
import { SmartRequestClient } from '../ts/modern/index.js';
import { SmartRequestClient } from '../ts/client/index.js';
tap.test('modern: should request a html document over https', async () => {
tap.test('client: should request a html document over https', async () => {
const response = await SmartRequestClient.create()
.url('https://encrypted.google.com/')
.get();
@@ -14,7 +14,7 @@ tap.test('modern: should request a html document over https', async () => {
expect(text.length).toBeGreaterThan(0);
});
tap.test('modern: should request a JSON document over https', async () => {
tap.test('client: should request a JSON document over https', async () => {
const response = await SmartRequestClient.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.get();
@@ -24,7 +24,7 @@ tap.test('modern: should request a JSON document over https', async () => {
expect(body.id).toEqual(1);
});
tap.test('modern: should post a JSON document over http', async () => {
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')
@@ -37,7 +37,7 @@ tap.test('modern: should post a JSON document over http', async () => {
expect(body.json.text).toEqual('example_text');
});
tap.test('modern: should set headers correctly', async () => {
tap.test('client: should set headers correctly', async () => {
const customHeader = 'X-Custom-Header';
const headerValue = 'test-value';
@@ -54,7 +54,7 @@ tap.test('modern: should set headers correctly', async () => {
expect(body.headers[customHeader]).toEqual(headerValue);
});
tap.test('modern: should handle query parameters', async () => {
tap.test('client: should handle query parameters', async () => {
const params = { param1: 'value1', param2: 'value2' };
const response = await SmartRequestClient.create()
@@ -70,7 +70,7 @@ tap.test('modern: should handle query parameters', async () => {
expect(body.args.param2).toEqual('value2');
});
tap.test('modern: should handle timeout configuration', async () => {
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')
@@ -81,7 +81,7 @@ tap.test('modern: should handle timeout configuration', async () => {
expect(response.ok).toBeTrue();
});
tap.test('modern: should handle retry configuration', async () => {
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')