CommunitySolidServer/test/integration/GuardedStream.test.ts
Joachim Van Herwegen 5613ff9e71 fix: Let Representations always have a body
This is relevant when the request has a content-type
but no data.
2021-10-12 13:30:06 +02:00

62 lines
1.9 KiB
TypeScript

import {
RepresentationMetadata,
TypedRepresentationConverter,
readableToString,
ChainedConverter,
guardedStreamFrom,
RdfToQuadConverter,
BasicRepresentation,
getLoggerFor,
INTERNAL_QUADS,
} from '../../src';
import type { Representation,
RepresentationConverterArgs,
Logger } from '../../src';
jest.mock('../../src/logging/LogUtil', (): any => {
const logger: Logger =
{ error: jest.fn(), debug: jest.fn(), warn: jest.fn(), info: jest.fn(), log: jest.fn() } as any;
return { getLoggerFor: (): Logger => logger };
});
const logger: jest.Mocked<Logger> = getLoggerFor('GuardedStream') as any;
class DummyConverter extends TypedRepresentationConverter {
public constructor() {
super('*/*', 'custom/type');
}
public async getInputTypes(): Promise<Record<string, number>> {
return { [INTERNAL_QUADS]: 1 };
}
public async getOutputTypes(): Promise<Record<string, number>> {
return { 'x/x': 1 };
}
public async handle({ representation }: RepresentationConverterArgs): Promise<Representation> {
const data = guardedStreamFrom([ 'dummy' ]);
const metadata = new RepresentationMetadata(representation.metadata, 'x/x');
return { binary: true, data, metadata, isEmpty: false };
}
}
describe('A chained converter where data gets ignored', (): void => {
const identifier = { path: 'http://test.com/' };
const rep = new BasicRepresentation('<a:b> <a:b> c!', identifier, 'text/turtle');
const converter = new ChainedConverter([
new RdfToQuadConverter(),
new DummyConverter(),
]);
it('does not throw on async crash.', async(): Promise<void> => {
jest.useFakeTimers();
const result = await converter.handleSafe({ identifier, representation: rep, preferences: { type: { 'x/x': 1 }}});
expect(await readableToString(result.data)).toBe('dummy');
jest.advanceTimersByTime(1000);
expect(logger.error).toHaveBeenCalledTimes(1);
});
});