function assert(condition, message) {
if (!condition) {
throw new Error(message || 'Assertion failed');
}
}
function assertEquals(actual, expected, message) {
if (actual !== expected) {
throw new Error(message || `Expected ${expected} but got ${actual}`);
}
}
function assertObjectEquals(actual, expected, message) {
const actualStr = JSON.stringify(actual);
const expectedStr = JSON.stringify(expected);
if (actualStr !== expectedStr) {
throw new Error(message || `Expected ${expectedStr} but got ${actualStr}`);
}
}
{
const req1 = new Request('https://example.com/api');
assertEquals(req1.method, 'GET');
assertEquals(req1.url, 'https://example.com/api');
assertObjectEquals(req1.headers, {});
const req2 = new Request('https://example.com/api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'value' })
});
assertEquals(req2.method, 'POST');
assertEquals(req2.url, 'https://example.com/api');
assertEquals(req2.body, '{"key":"value"}');
const req3 = new Request(req2);
assertEquals(req3.method, 'POST');
assertEquals(req3.url, 'https://example.com/api');
assertEquals(req3.body, '{"key":"value"}');
const req4 = new Request(req2, {
method: 'PUT',
body: JSON.stringify({ other: 'value' })
});
assertEquals(req4.method, 'PUT');
assertEquals(req4.url, 'https://example.com/api');
assertEquals(req4.body, '{"other":"value"}');
const req5 = req4.clone();
assertEquals(req5.method, 'PUT');
assertEquals(req5.url, 'https://example.com/api');
assertEquals(req5.body, '{"other":"value"}');
try {
new Request({});
assert(false, 'Should throw on invalid input');
} catch (e) {
assert(e instanceof TypeError);
}
}
{
let lastFetchCall = null;
globalThis.sendHttpRequest = async (method, url, headers, body, timeout_ms) => {
lastFetchCall = { method, url, headers, body, timeout_ms };
return {
status: 200,
headers: { 'content-type': 'application/json' }
};
};
await fetch('https://example.com/api');
assertObjectEquals(lastFetchCall, {
method: 'GET',
url: 'https://example.com/api',
headers: null,
body: null,
timeout_ms: null
});
const req = new Request('https://example.com/api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ test: true })
});
await fetch(req);
assertObjectEquals(lastFetchCall, {
method: 'POST',
url: 'https://example.com/api',
headers: { 'content-type': 'application/json' },
body: '{"test":true}',
timeout_ms: null
});
await fetch('https://example.com/api', {
method: 'PUT',
headers: { 'Authorization': 'Bearer token' },
body: 'test data',
timeout_ms: 5000
});
assertObjectEquals(lastFetchCall, {
method: 'PUT',
url: 'https://example.com/api',
headers: { 'authorization': 'Bearer token' },
body: 'test data',
timeout_ms: 5000
});
globalThis.sendHttpRequest = async () => {
throw new Error('timeout');
};
try {
await fetch('https://example.com/api');
assert(false, 'Should throw on timeout');
} catch (e) {
assert(e instanceof Error);
assert(e.message.includes('timeout'));
}
try {
await fetch({});
assert(false, 'Should throw on invalid input');
} catch (e) {
assert(e instanceof TypeError);
}
}