mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-12-05 18:55:32 +03:00
621 lines
19 KiB
TypeScript
621 lines
19 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { POST } from '../../../../../../../src/routes/api/internal/diagnostics/http/+server';
|
|
|
|
// Mock the Request constructor for testing
|
|
const createMockRequest = (body: any) => ({
|
|
json: vi.fn().mockResolvedValue(body)
|
|
} as unknown as Request);
|
|
|
|
describe('HTTP Diagnostics API Endpoint', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('POST Handler - Basic Validation', () => {
|
|
it('should reject missing action', async () => {
|
|
const request = createMockRequest({
|
|
url: 'https://example.com'
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(400);
|
|
expect(error.body.message).toBe('Missing required parameters: action and url');
|
|
}
|
|
});
|
|
|
|
it('should reject missing url', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers'
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(400);
|
|
expect(error.body.message).toBe('Missing required parameters: action and url');
|
|
}
|
|
});
|
|
|
|
it('should reject invalid URL format', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'invalid-url'
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(400);
|
|
expect(error.body.message).toBe('Invalid URL format');
|
|
}
|
|
});
|
|
|
|
it('should reject unknown action', async () => {
|
|
const request = createMockRequest({
|
|
action: 'unknown-action',
|
|
url: 'https://example.com'
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(400);
|
|
expect(error.body.message).toBe('Unknown action: unknown-action');
|
|
}
|
|
});
|
|
|
|
it('should accept valid HTTP URL', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'http://example.com'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
}, 15000);
|
|
|
|
it('should accept valid HTTPS URL', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://example.com'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - Headers Action', () => {
|
|
it('should return headers for valid URL', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('status');
|
|
expect(data).toHaveProperty('statusText');
|
|
expect(data).toHaveProperty('headers');
|
|
expect(data).toHaveProperty('timings');
|
|
expect(data).toHaveProperty('url');
|
|
expect(typeof data.status).toBe('number');
|
|
expect(typeof data.headers).toBe('object');
|
|
}, 15000);
|
|
|
|
it('should handle custom headers', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/headers',
|
|
headers: {
|
|
'X-Test-Header': 'test-value'
|
|
}
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('headers');
|
|
}, 15000);
|
|
|
|
it('should handle different HTTP methods', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/post',
|
|
method: 'POST'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('status');
|
|
expect(data.status).toBe(200);
|
|
}, 15000);
|
|
|
|
it('should include content size when available', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('size');
|
|
if (data.size !== null) {
|
|
expect(typeof data.size).toBe('number');
|
|
}
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - Redirect Trace Action', () => {
|
|
it('should trace redirects', async () => {
|
|
const request = createMockRequest({
|
|
action: 'redirect-trace',
|
|
url: 'https://httpbin.org/redirect/2'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('finalStatus');
|
|
expect(data).toHaveProperty('finalUrl');
|
|
expect(data).toHaveProperty('redirectChain');
|
|
expect(data).toHaveProperty('totalRedirects');
|
|
expect(data).toHaveProperty('timings');
|
|
expect(Array.isArray(data.redirectChain)).toBe(true);
|
|
expect(typeof data.totalRedirects).toBe('number');
|
|
}, 15000);
|
|
|
|
it('should handle no redirects', async () => {
|
|
const request = createMockRequest({
|
|
action: 'redirect-trace',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.totalRedirects).toBe(0);
|
|
expect(Array.isArray(data.redirectChain)).toBe(true);
|
|
}, 15000);
|
|
|
|
it('should respect maxRedirects parameter', async () => {
|
|
const request = createMockRequest({
|
|
action: 'redirect-trace',
|
|
url: 'https://httpbin.org/redirect/5',
|
|
maxRedirects: 2
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should throw error due to too many redirects
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(500);
|
|
expect(error.body.message).toContain('Too many redirects');
|
|
}
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - Security Action', () => {
|
|
it('should analyze security headers', async () => {
|
|
const request = createMockRequest({
|
|
action: 'security',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('url');
|
|
expect(data).toHaveProperty('status');
|
|
expect(data).toHaveProperty('headers');
|
|
expect(data).toHaveProperty('analysis');
|
|
expect(data).toHaveProperty('timings');
|
|
expect(typeof data.headers).toBe('object');
|
|
expect(typeof data.analysis).toBe('object');
|
|
}, 15000);
|
|
|
|
it('should provide security analysis', async () => {
|
|
const request = createMockRequest({
|
|
action: 'security',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.analysis).toBeDefined();
|
|
// The analysis object structure depends on the security headers found
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - CORS Check Action', () => {
|
|
it('should check CORS configuration', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cors-check',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('preflight');
|
|
expect(data).toHaveProperty('origin');
|
|
expect(data).toHaveProperty('analysis');
|
|
expect(data.preflight).toHaveProperty('status');
|
|
expect(data.preflight).toHaveProperty('allowed');
|
|
expect(data.analysis).toHaveProperty('corsEnabled');
|
|
expect(data.analysis).toHaveProperty('allowsOrigin');
|
|
}, 15000);
|
|
|
|
it('should use custom origin', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cors-check',
|
|
url: 'https://httpbin.org/get',
|
|
headers: {
|
|
origin: 'https://mycustomorigin.com'
|
|
}
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.origin).toBe('https://mycustomorigin.com');
|
|
}, 15000);
|
|
|
|
it('should handle CORS preflight failure', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cors-check',
|
|
url: 'https://httpbin.org/status/404'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('preflight');
|
|
expect(data).toHaveProperty('analysis');
|
|
// Should handle gracefully even if preflight fails
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - Performance Action', () => {
|
|
it('should analyze performance metrics', async () => {
|
|
const request = createMockRequest({
|
|
action: 'perf',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('url');
|
|
expect(data).toHaveProperty('status');
|
|
expect(data).toHaveProperty('timings');
|
|
expect(data).toHaveProperty('performance');
|
|
expect(data.performance).toHaveProperty('isHTTPS');
|
|
expect(data.performance).toHaveProperty('hasCompression');
|
|
expect(typeof data.performance.isHTTPS).toBe('boolean');
|
|
}, 15000);
|
|
|
|
it('should detect HTTPS correctly', async () => {
|
|
const request = createMockRequest({
|
|
action: 'perf',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.performance.isHTTPS).toBe(true);
|
|
}, 15000);
|
|
|
|
it('should include timing information', async () => {
|
|
const request = createMockRequest({
|
|
action: 'perf',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.timings).toBeDefined();
|
|
expect(typeof data.timings).toBe('object');
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - Compression Action', () => {
|
|
it('should test compression support', async () => {
|
|
const request = createMockRequest({
|
|
action: 'compression',
|
|
url: 'https://httpbin.org/gzip'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('url');
|
|
expect(data).toHaveProperty('uncompressed');
|
|
expect(data).toHaveProperty('serverCompression');
|
|
expect(data).toHaveProperty('compressionResults');
|
|
expect(data).toHaveProperty('bestCompression');
|
|
expect(data).toHaveProperty('timings');
|
|
expect(Array.isArray(data.compressionResults)).toBe(true);
|
|
}, 20000);
|
|
|
|
it('should detect server compression', async () => {
|
|
const request = createMockRequest({
|
|
action: 'compression',
|
|
url: 'https://httpbin.org/gzip'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.serverCompression).toHaveProperty('enabled');
|
|
expect(data.serverCompression).toHaveProperty('encoding');
|
|
expect(typeof data.serverCompression.enabled).toBe('boolean');
|
|
}, 20000);
|
|
|
|
it('should test multiple compression encodings', async () => {
|
|
const request = createMockRequest({
|
|
action: 'compression',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.compressionResults.length).toBeGreaterThan(0);
|
|
|
|
// Should test gzip, br, deflate
|
|
const encodings = data.compressionResults.map((r: any) => r.encoding);
|
|
expect(encodings).toContain('gzip');
|
|
expect(encodings).toContain('br');
|
|
expect(encodings).toContain('deflate');
|
|
}, 20000);
|
|
|
|
it('should calculate compression ratios', async () => {
|
|
const request = createMockRequest({
|
|
action: 'compression',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
data.compressionResults.forEach((result: any) => {
|
|
expect(result).toHaveProperty('encoding');
|
|
expect(result).toHaveProperty('supported');
|
|
expect(result).toHaveProperty('ratio');
|
|
expect(typeof result.ratio).toBe('number');
|
|
});
|
|
}, 20000);
|
|
});
|
|
|
|
describe('POST Handler - Cookie Security Action', () => {
|
|
it('should analyze cookie security', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cookie-security',
|
|
url: 'https://httpbin.org/cookies/set/test/value'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('url');
|
|
expect(data).toHaveProperty('status');
|
|
expect(data).toHaveProperty('totalCookies');
|
|
expect(data).toHaveProperty('secureCookies');
|
|
expect(data).toHaveProperty('httpOnlyCookies');
|
|
expect(data).toHaveProperty('cookies');
|
|
expect(data).toHaveProperty('recommendations');
|
|
expect(data).toHaveProperty('summary');
|
|
expect(Array.isArray(data.cookies)).toBe(true);
|
|
expect(Array.isArray(data.recommendations)).toBe(true);
|
|
}, 15000);
|
|
|
|
it('should handle no cookies', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cookie-security',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.totalCookies).toBe(0);
|
|
expect(data.cookies).toHaveLength(0);
|
|
}, 15000);
|
|
|
|
it('should provide security recommendations', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cookie-security',
|
|
url: 'https://httpbin.org/cookies/set/test/value'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(Array.isArray(data.recommendations)).toBe(true);
|
|
expect(data.summary).toBeDefined();
|
|
}, 15000);
|
|
|
|
it('should calculate security scores', async () => {
|
|
const request = createMockRequest({
|
|
action: 'cookie-security',
|
|
url: 'https://httpbin.org/cookies/set/test/value'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
if (data.totalCookies > 0) {
|
|
expect(data.securityScore).toBeDefined();
|
|
expect(typeof data.securityScore).toBe('number');
|
|
}
|
|
}, 15000);
|
|
});
|
|
|
|
describe('POST Handler - Error Handling', () => {
|
|
it('should handle malformed JSON', async () => {
|
|
const request = {
|
|
json: vi.fn().mockRejectedValue(new Error('Invalid JSON'))
|
|
} as unknown as Request;
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(500);
|
|
expect(error.body.message).toContain('Invalid JSON');
|
|
}
|
|
});
|
|
|
|
it('should handle network timeout', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/delay/15',
|
|
timeout: 1000
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(408);
|
|
expect(error.body.message).toBe('Request timeout');
|
|
}
|
|
}, 10000);
|
|
|
|
it('should handle non-existent host', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://nonexistent-host-12345.invalid'
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should not reach here
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(500);
|
|
expect(error.body.message).toContain('fetch failed');
|
|
}
|
|
}, 10000);
|
|
|
|
it('should handle connection errors gracefully', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/status/500'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
// Should handle server errors gracefully
|
|
expect(response.status).toBe(200);
|
|
|
|
const data = await response.json();
|
|
expect(data.status).toBe(500);
|
|
}, 10000);
|
|
});
|
|
|
|
describe('POST Handler - Parameter Validation', () => {
|
|
it('should use default values for optional parameters', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/get'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
|
|
// Default method should be GET, timeout should be 10000, etc.
|
|
const data = await response.json();
|
|
expect(data).toHaveProperty('status');
|
|
}, 15000);
|
|
|
|
it('should respect custom timeout', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/delay/1',
|
|
timeout: 500
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should timeout
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(408);
|
|
}
|
|
}, 10000);
|
|
|
|
it('should respect custom method', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/put',
|
|
method: 'PUT'
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
}, 15000);
|
|
|
|
it('should respect custom headers', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/headers',
|
|
headers: {
|
|
'Authorization': 'Bearer test-token',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
const response = await POST({ request } as any);
|
|
expect(response.status).toBe(200);
|
|
}, 15000);
|
|
|
|
it('should respect maxRedirects parameter', async () => {
|
|
const request = createMockRequest({
|
|
action: 'headers',
|
|
url: 'https://httpbin.org/redirect/1',
|
|
maxRedirects: 0
|
|
});
|
|
|
|
try {
|
|
await POST({ request } as any);
|
|
expect(false).toBe(true); // Should throw error due to redirect limit
|
|
} catch (error: any) {
|
|
expect(error.status).toBe(500);
|
|
expect(error.body.message).toContain('Too many redirects');
|
|
}
|
|
}, 15000);
|
|
});
|
|
}); |