2025-07-28 23:20:52 +00:00
|
|
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
2025-07-28 22:37:36 +00:00
|
|
|
|
|
|
|
// 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 = {
|
2025-07-29 13:49:50 +00:00
|
|
|
timeout: 100 // Very short timeout
|
2025-07-28 22:37:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2025-07-29 13:49:50 +00:00
|
|
|
// Use a URL that will likely take longer than 100ms
|
|
|
|
const request = new CoreRequest('https://jsonplaceholder.typicode.com/photos', options);
|
2025-07-28 22:37:36 +00:00
|
|
|
await request.fire();
|
|
|
|
} catch (error) {
|
|
|
|
timedOut = true;
|
2025-07-29 13:49:50 +00:00
|
|
|
// Different browsers might have different timeout error messages
|
|
|
|
expect(error.message.toLowerCase()).toMatch(/timeout|timed out|aborted/i);
|
2025-07-28 22:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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: {
|
2025-07-29 13:49:50 +00:00
|
|
|
userId: '2'
|
2025-07-28 22:37:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-07-29 13:49:50 +00:00
|
|
|
const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts', options);
|
2025-07-28 22:37:36 +00:00
|
|
|
const response = await request.fire();
|
|
|
|
|
|
|
|
expect(response.status).toEqual(200);
|
|
|
|
|
|
|
|
const data = await response.json();
|
2025-07-29 13:49:50 +00:00
|
|
|
expect(Array.isArray(data)).toBeTrue();
|
|
|
|
// Verify we got posts filtered by userId 2
|
|
|
|
if (data.length > 0) {
|
|
|
|
expect(data[0]).toHaveProperty('userId');
|
|
|
|
expect(data[0].userId).toEqual(2);
|
|
|
|
}
|
2025-07-28 22:37:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default tap.start();
|