Extension pages with host permissions are treated as same-origin, so the target origin's cookies rode along on every request. OpenRouter's Clerk-protected analytics endpoint then tried to read the management key in the Authorization header as a session JWT and answered 403, breaking the spend total on the dashboard. Default the shared fetch to credentials "omit" (still overridable per call), and surface OpenRouter's own error message for failed requests instead of a generic status summary.
254 lines
7.5 KiB
JavaScript
254 lines
7.5 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import {
|
|
CORS_PROXY_HEADER,
|
|
HttpError,
|
|
NetworkError,
|
|
RequestAbortedError,
|
|
isCancellation,
|
|
proxiedUrl,
|
|
} from 'dashtab/services/http';
|
|
|
|
module('Unit | Service | http', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
let originalFetch;
|
|
|
|
hooks.beforeEach(function () {
|
|
originalFetch = globalThis.fetch;
|
|
});
|
|
|
|
hooks.afterEach(function () {
|
|
globalThis.fetch = originalFetch;
|
|
});
|
|
|
|
test('it exists', function (assert) {
|
|
let service = this.owner.lookup('service:http');
|
|
assert.ok(service);
|
|
});
|
|
|
|
test('it parses a successful JSON response', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
json: async () => ({ value: 42 }),
|
|
});
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
const data = await http.requestJson('https://example.com/data.json');
|
|
|
|
assert.deepEqual(data, { value: 42 });
|
|
});
|
|
|
|
test('it parses a successful text response', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
text: async () => '<html></html>',
|
|
});
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
const text = await http.requestText('https://example.com/');
|
|
|
|
assert.strictEqual(text, '<html></html>');
|
|
});
|
|
|
|
test('it returns a blob for a successful binary response', async function (assert) {
|
|
const expected = new Blob(['icon'], { type: 'image/png' });
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
blob: async () => expected,
|
|
});
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
const blob = await http.requestBlob('https://example.com/favicon.ico');
|
|
|
|
assert.strictEqual(blob, expected);
|
|
});
|
|
|
|
test('it throws HttpError on a non-ok response', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 401,
|
|
json: async () => ({}),
|
|
});
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
|
|
await assert.rejects(
|
|
http.requestJson('https://example.com/data.json'),
|
|
(err) => {
|
|
assert.true(err instanceof HttpError);
|
|
assert.strictEqual(err.status, 401);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
test('it captures the API error type on a failed response', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 403,
|
|
json: async () => ({
|
|
errors: ['You are not permitted to view the requested resource.'],
|
|
error_type: 'invalid_access',
|
|
}),
|
|
});
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
|
|
await assert.rejects(
|
|
http.requestJson('https://example.com/data.json'),
|
|
(err) => {
|
|
assert.true(err instanceof HttpError);
|
|
assert.strictEqual(err.status, 403);
|
|
assert.strictEqual(err.errorType, 'invalid_access');
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
test('it throws NetworkError when the request cannot be made', async function (assert) {
|
|
globalThis.fetch = async () => {
|
|
throw new TypeError('Failed to fetch');
|
|
};
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
|
|
await assert.rejects(
|
|
http.requestJson('https://example.com/data.json'),
|
|
(err) => {
|
|
assert.true(err instanceof NetworkError);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
test('it throws RequestAbortedError when the request is cancelled', async function (assert) {
|
|
globalThis.fetch = async () => {
|
|
const err = new Error('The operation was aborted');
|
|
err.name = 'AbortError';
|
|
throw err;
|
|
};
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
|
|
await assert.rejects(
|
|
http.requestJson('https://example.com/data.json'),
|
|
(err) => {
|
|
assert.true(err instanceof RequestAbortedError);
|
|
assert.false(err instanceof NetworkError);
|
|
assert.true(isCancellation(err));
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
test('isCancellation matches AbortError and RequestAbortedError only', function (assert) {
|
|
assert.true(isCancellation({ name: 'AbortError' }));
|
|
assert.true(isCancellation(new RequestAbortedError()));
|
|
assert.false(isCancellation(new NetworkError()));
|
|
assert.false(isCancellation(new HttpError(500)));
|
|
assert.false(isCancellation(null));
|
|
});
|
|
|
|
test('proxiedUrl prefixes absolute URLs with the proxy base', function (assert) {
|
|
const base = 'http://localhost:8081/';
|
|
|
|
assert.strictEqual(
|
|
proxiedUrl('https://forum.example.com/notifications.json', base),
|
|
'http://localhost:8081/https://forum.example.com/notifications.json'
|
|
);
|
|
assert.strictEqual(
|
|
proxiedUrl('http://example.com/data.json', base),
|
|
'http://localhost:8081/http://example.com/data.json'
|
|
);
|
|
});
|
|
|
|
test('proxiedUrl leaves relative, proxied and unconfigured URLs alone', function (assert) {
|
|
const base = 'http://localhost:8081/';
|
|
|
|
assert.strictEqual(proxiedUrl('/assets/app.js', base), '/assets/app.js');
|
|
assert.strictEqual(
|
|
proxiedUrl('http://localhost:8081/https://example.com', base),
|
|
'http://localhost:8081/https://example.com'
|
|
);
|
|
assert.strictEqual(
|
|
proxiedUrl('https://example.com/data.json', null),
|
|
'https://example.com/data.json'
|
|
);
|
|
});
|
|
|
|
test('requestJson routes through the proxy and adds the proxy header', async function (assert) {
|
|
let requestedUrl;
|
|
let requestedOptions;
|
|
globalThis.fetch = async (url, options) => {
|
|
requestedUrl = url;
|
|
requestedOptions = options;
|
|
return { ok: true, json: async () => ({}) };
|
|
};
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
http.corsProxyBase = 'http://localhost:8081/';
|
|
|
|
await http.requestJson('https://forum.example.com/notifications.json', {
|
|
headers: { 'User-Api-Key': 'key' },
|
|
});
|
|
|
|
assert.strictEqual(
|
|
requestedUrl,
|
|
'http://localhost:8081/https://forum.example.com/notifications.json'
|
|
);
|
|
assert.strictEqual(requestedOptions.headers[CORS_PROXY_HEADER], '1');
|
|
assert.strictEqual(requestedOptions.headers['User-Api-Key'], 'key');
|
|
});
|
|
|
|
test('requestJson goes direct without a proxy base', async function (assert) {
|
|
let requestedUrl;
|
|
let requestedOptions;
|
|
globalThis.fetch = async (url, options) => {
|
|
requestedUrl = url;
|
|
requestedOptions = options;
|
|
return { ok: true, json: async () => ({}) };
|
|
};
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
http.corsProxyBase = null;
|
|
|
|
await http.requestJson('https://example.com/data.json');
|
|
|
|
assert.strictEqual(requestedUrl, 'https://example.com/data.json');
|
|
assert.false(CORS_PROXY_HEADER in requestedOptions.headers);
|
|
});
|
|
|
|
test('requestJson omits credentials so target cookies are not sent', async function (assert) {
|
|
let requestedOptions;
|
|
globalThis.fetch = async (url, options) => {
|
|
requestedOptions = options;
|
|
return { ok: true, json: async () => ({}) };
|
|
};
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
http.corsProxyBase = null;
|
|
|
|
await http.requestJson('https://example.com/data.json');
|
|
|
|
assert.strictEqual(requestedOptions.credentials, 'omit');
|
|
});
|
|
|
|
test('requestJson lets callers override the credentials mode', async function (assert) {
|
|
let requestedOptions;
|
|
globalThis.fetch = async (url, options) => {
|
|
requestedOptions = options;
|
|
return { ok: true, json: async () => ({}) };
|
|
};
|
|
|
|
const http = this.owner.lookup('service:http');
|
|
http.corsProxyBase = null;
|
|
|
|
await http.requestJson('https://example.com/data.json', {
|
|
credentials: 'include',
|
|
});
|
|
|
|
assert.strictEqual(requestedOptions.credentials, 'include');
|
|
});
|
|
});
|