48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import { buildApp } from '../../src/app/build-app.js';
|
|
|
|
test('serves public image bytes with the stored safe MIME type', async (testContext) => {
|
|
const app = buildApp({
|
|
publicImageService: {
|
|
async getContent(id) {
|
|
assert.equal(id, 'image-1');
|
|
return { mimeType: 'image/webp', data: Buffer.from('webp-bytes') };
|
|
},
|
|
},
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/public/images/image-1',
|
|
});
|
|
|
|
assert.equal(response.statusCode, 200);
|
|
assert.equal(response.headers['content-type'], 'image/webp');
|
|
assert.equal(response.rawPayload.toString(), 'webp-bytes');
|
|
});
|
|
|
|
test('does not expose hidden image errors as internal failures', async (testContext) => {
|
|
const app = buildApp({
|
|
publicImageService: {
|
|
async getContent() {
|
|
const error = new Error('Image was not found.');
|
|
error.code = 'IMAGE_NOT_FOUND';
|
|
error.statusCode = 404;
|
|
throw error;
|
|
},
|
|
},
|
|
});
|
|
testContext.after(() => app.close());
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/public/images/hidden-image',
|
|
});
|
|
|
|
assert.equal(response.statusCode, 404);
|
|
assert.equal(response.json().error.code, 'IMAGE_NOT_FOUND');
|
|
});
|