finished http-client tests

This commit is contained in:
Bojan Kucera 2025-06-04 16:54:11 -04:00
parent 557c157228
commit 1899078523
4 changed files with 204 additions and 46 deletions

View file

@ -1,7 +1,23 @@
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
import { HttpClient } from '../src/index.js';
import type { RateLimitConfig } from '../src/types.js';
import { MockServer } from './mock-server.js';
// Global mock server instance
let mockServer: MockServer;
let mockServerBaseUrl: string;
beforeAll(async () => {
// Start mock server
mockServer = new MockServer();
await mockServer.start();
mockServerBaseUrl = mockServer.getBaseUrl();
});
afterAll(async () => {
// Stop mock server
await mockServer.stop();
});
describe('HttpClient Error Handling Integration', () => {
let client: HttpClient;
@ -34,25 +50,15 @@ describe('HttpClient Error Handling Integration', () => {
console.warn('URL validation test skipped');
}
});
test('should handle server errors (5xx)', async () => {
try {
await expect(
client.get('https://httpbin.org/status/500')
).rejects.toThrow();
} catch (e) {
if ((e as Error).message.includes('ENOTFOUND') ||
(e as Error).message.includes('Failed to fetch')) {
console.warn('Network connectivity issue detected - skipping test');
} else {
throw e;
}
}
test('should handle server errors (5xx)', async () => {
await expect(
client.get(`${mockServerBaseUrl}/status/500`)
).rejects.toThrow();
});
test('should treat 404 as error by default', async () => {
await expect(
client.get('https://httpbin.org/status/404')
client.get(`${mockServerBaseUrl}/status/404`)
).rejects.toThrow();
});
@ -62,36 +68,27 @@ describe('HttpClient Error Handling Integration', () => {
});
// This should not throw because we're allowing 404
const response = await customClient.get('https://httpbin.org/status/404');
const response = await customClient.get(`${mockServerBaseUrl}/status/404`);
expect(response.status).toBe(404);
});
});
describe('HttpClient Authentication Integration', () => {
test('should handle basic authentication', async () => {
try {
const client = new HttpClient({
timeout: 10000 // Longer timeout for network stability
});
const response = await client.get('https://httpbin.org/basic-auth/user/passwd', {
auth: {
username: 'user',
password: 'passwd'
}
});
expect(response.status).toBe(200);
expect(response.data).toHaveProperty('authenticated', true);
expect(response.data).toHaveProperty('user', 'user');
} catch (e) {
if ((e as Error).message.includes('ENOTFOUND') ||
(e as Error).message.includes('Failed to fetch')) {
console.warn('Network connectivity issue detected - skipping test');
} else {
throw e;
const client = new HttpClient({
timeout: 10000 // Longer timeout for network stability
});
const response = await client.get(`${mockServerBaseUrl}/basic-auth/user/passwd`, {
auth: {
username: 'user',
password: 'passwd'
}
}
});
expect(response.status).toBe(200);
expect(response.data).toHaveProperty('authenticated', true);
expect(response.data).toHaveProperty('user', 'user');
});
test('should handle bearer token authentication', async () => {
@ -101,24 +98,23 @@ describe('HttpClient Authentication Integration', () => {
'Authorization': `Bearer ${token}`
}
});
const response = await client.get('https://httpbin.org/headers');
const response = await client.get(`${mockServerBaseUrl}/headers`);
expect(response.status).toBe(200);
expect(response.data.headers).toHaveProperty('Authorization', `Bearer ${token}`);
expect(response.data.headers).toHaveProperty('authorization', `bearer ${token}`);
});
test('should handle custom authentication headers', async () => {
const apiKey = 'api-key-123456';
const client = new HttpClient({
defaultHeaders: {
'X-API-Key': apiKey
'x-api-key': apiKey
}
});
const response = await client.get('https://httpbin.org/headers');
const response = await client.get(`${mockServerBaseUrl}/headers`);
expect(response.status).toBe(200);
expect(response.data.headers).toHaveProperty('X-Api-Key', apiKey);
expect(response.data.headers).toHaveProperty('x-api-key', apiKey);
});
});