132 lines
3.8 KiB
TypeScript
132 lines
3.8 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
import { MockServer } from './mock-server';
|
|
|
|
/**
|
|
* Tests for the MockServer utility
|
|
* Ensures our test infrastructure works correctly
|
|
*/
|
|
|
|
describe('MockServer', () => {
|
|
let mockServer: MockServer;
|
|
let baseUrl: string;
|
|
|
|
beforeAll(async () => {
|
|
mockServer = new MockServer();
|
|
await mockServer.start();
|
|
baseUrl = mockServer.getBaseUrl();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await mockServer.stop();
|
|
});
|
|
|
|
describe('Server lifecycle', () => {
|
|
test('should start and provide base URL', () => {
|
|
expect(baseUrl).toMatch(/^http:\/\/localhost:\d+$/);
|
|
expect(mockServer.getBaseUrl()).toBe(baseUrl);
|
|
});
|
|
|
|
test('should be reachable', async () => {
|
|
const response = await fetch(`${baseUrl}/`);
|
|
expect(response.ok).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Status endpoints', () => {
|
|
test('should return correct status codes', async () => {
|
|
const statusCodes = [200, 201, 400, 401, 403, 404, 500, 503];
|
|
|
|
for (const status of statusCodes) {
|
|
const response = await fetch(`${baseUrl}/status/${status}`);
|
|
expect(response.status).toBe(status);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('Headers endpoint', () => {
|
|
test('should echo request headers', async () => {
|
|
const response = await fetch(`${baseUrl}/headers`, {
|
|
headers: {
|
|
'X-Test-Header': 'test-value',
|
|
'User-Agent': 'MockServer-Test',
|
|
},
|
|
});
|
|
|
|
expect(response.ok).toBe(true);
|
|
const data = await response.json();
|
|
expect(data.headers).toHaveProperty('x-test-header', 'test-value');
|
|
expect(data.headers).toHaveProperty('user-agent', 'MockServer-Test');
|
|
});
|
|
});
|
|
|
|
describe('Basic auth endpoint', () => {
|
|
test('should authenticate valid credentials', async () => {
|
|
const username = 'testuser';
|
|
const password = 'testpass';
|
|
const credentials = btoa(`${username}:${password}`);
|
|
|
|
const response = await fetch(`${baseUrl}/basic-auth/${username}/${password}`, {
|
|
headers: {
|
|
Authorization: `Basic ${credentials}`,
|
|
},
|
|
});
|
|
|
|
expect(response.ok).toBe(true);
|
|
const data = await response.json();
|
|
expect(data.authenticated).toBe(true);
|
|
expect(data.user).toBe(username);
|
|
});
|
|
|
|
test('should reject invalid credentials', async () => {
|
|
const credentials = btoa('wrong:credentials');
|
|
|
|
const response = await fetch(`${baseUrl}/basic-auth/user/pass`, {
|
|
headers: {
|
|
Authorization: `Basic ${credentials}`,
|
|
},
|
|
});
|
|
|
|
expect(response.status).toBe(401);
|
|
});
|
|
|
|
test('should reject missing auth header', async () => {
|
|
const response = await fetch(`${baseUrl}/basic-auth/user/pass`);
|
|
expect(response.status).toBe(401);
|
|
});
|
|
});
|
|
|
|
describe('POST endpoint', () => {
|
|
test('should echo POST data', async () => {
|
|
const testData = {
|
|
message: 'Hello, MockServer!',
|
|
timestamp: Date.now(),
|
|
};
|
|
|
|
const response = await fetch(`${baseUrl}/post`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(testData),
|
|
});
|
|
|
|
expect(response.ok).toBe(true);
|
|
const data = await response.json();
|
|
expect(data.data).toEqual(testData);
|
|
expect(data.method).toBe('POST');
|
|
expect(data.headers).toHaveProperty('content-type', 'application/json');
|
|
});
|
|
});
|
|
|
|
describe('Default endpoint', () => {
|
|
test('should return request information', async () => {
|
|
const response = await fetch(`${baseUrl}/unknown-endpoint`);
|
|
|
|
expect(response.ok).toBe(true);
|
|
const data = await response.json();
|
|
expect(data.url).toBe(`${baseUrl}/unknown-endpoint`);
|
|
expect(data.method).toBe('GET');
|
|
expect(data.headers).toBeDefined();
|
|
});
|
|
});
|
|
});
|