mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Allow IfNeededConverter to short-circuit.
This commit is contained in:
parent
dd9d873122
commit
a21532ebf8
@ -79,10 +79,11 @@
|
|||||||
|
|
||||||
{
|
{
|
||||||
"@id": "urn:solid-server:default:RepresentationConverter",
|
"@id": "urn:solid-server:default:RepresentationConverter",
|
||||||
"@type": "IfNeededConverter",
|
|
||||||
"IfNeededConverter:_converter": {
|
|
||||||
"@type": "WaterfallHandler",
|
"@type": "WaterfallHandler",
|
||||||
"WaterfallHandler:_handlers": [
|
"WaterfallHandler:_handlers": [
|
||||||
|
{
|
||||||
|
"@type": "IfNeededConverter"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"@id": "urn:solid-server:default:ContentTypeReplacer"
|
"@id": "urn:solid-server:default:ContentTypeReplacer"
|
||||||
},
|
},
|
||||||
@ -97,6 +98,5 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
import type { Representation } from '../../ldp/representation/Representation';
|
import type { Representation } from '../../ldp/representation/Representation';
|
||||||
import { getLoggerFor } from '../../logging/LogUtil';
|
import { getLoggerFor } from '../../logging/LogUtil';
|
||||||
import { InternalServerError } from '../../util/errors/InternalServerError';
|
import { InternalServerError } from '../../util/errors/InternalServerError';
|
||||||
|
import { UnsupportedAsyncHandler } from '../../util/UnsupportedAsyncHandler';
|
||||||
import { matchingMediaTypes } from './ConversionUtil';
|
import { matchingMediaTypes } from './ConversionUtil';
|
||||||
import { RepresentationConverter } from './RepresentationConverter';
|
import { RepresentationConverter } from './RepresentationConverter';
|
||||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||||
|
|
||||||
|
const EMPTY_CONVERTER = new UnsupportedAsyncHandler('The content type does not match the preferences');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link RepresentationConverter} that only converts representations
|
* A {@link RepresentationConverter} that only converts representations
|
||||||
* that are not compatible with the preferences.
|
* that are not compatible with the preferences.
|
||||||
@ -13,7 +16,7 @@ export class IfNeededConverter extends RepresentationConverter {
|
|||||||
private readonly converter: RepresentationConverter;
|
private readonly converter: RepresentationConverter;
|
||||||
protected readonly logger = getLoggerFor(this);
|
protected readonly logger = getLoggerFor(this);
|
||||||
|
|
||||||
public constructor(converter: RepresentationConverter) {
|
public constructor(converter: RepresentationConverter = EMPTY_CONVERTER) {
|
||||||
super();
|
super();
|
||||||
this.converter = converter;
|
this.converter = converter;
|
||||||
}
|
}
|
||||||
|
@ -100,4 +100,17 @@ describe('An IfNeededConverter', (): void => {
|
|||||||
expect(innerConverter.handle).toHaveBeenCalledTimes(1);
|
expect(innerConverter.handle).toHaveBeenCalledTimes(1);
|
||||||
expect(innerConverter.handle).toHaveBeenCalledWith(args);
|
expect(innerConverter.handle).toHaveBeenCalledWith(args);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not support conversion when there is no inner converter.', async(): Promise<void> => {
|
||||||
|
const emptyConverter = new IfNeededConverter();
|
||||||
|
const preferences = { type: { 'text/turtle': 0 }};
|
||||||
|
const args = { identifier, representation, preferences };
|
||||||
|
|
||||||
|
await expect(emptyConverter.canHandle(args)).rejects
|
||||||
|
.toThrow('The content type does not match the preferences');
|
||||||
|
await expect(emptyConverter.handle(args)).rejects
|
||||||
|
.toThrow('The content type does not match the preferences');
|
||||||
|
await expect(emptyConverter.handleSafe(args)).rejects
|
||||||
|
.toThrow('The content type does not match the preferences');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user