Merge branch 'main' into versions/6.0.0

This commit is contained in:
Joachim Van Herwegen
2022-09-29 15:31:42 +02:00
20 changed files with 474 additions and 156 deletions

View File

@@ -1,6 +1,7 @@
import 'jest-rdf';
import { Readable } from 'stream';
import arrayifyStream from 'arrayify-stream';
import fetch, { Headers } from 'cross-fetch';
import { DataFactory } from 'n3';
import rdfParser from 'rdf-parse';
import { PREFERRED_PREFIX_TERM, SOLID_META } from '../../../../src';
@@ -14,10 +15,25 @@ import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
const { namedNode, triple, literal, quad } = DataFactory;
describe('A RdfToQuadConverter', (): void => {
const converter = new RdfToQuadConverter();
const identifier: ResourceIdentifier = { path: 'path' };
// All of this is necessary to not break the cross-fetch imports that happen in `rdf-parse`
jest.mock('cross-fetch', (): any => {
const mock = jest.fn();
// Require the original module to not be mocked...
const originalFetch = jest.requireActual('cross-fetch');
return {
// eslint-disable-next-line @typescript-eslint/naming-convention
__esModule: true,
...originalFetch,
fetch: mock,
default: mock,
};
});
// Not mocking `fs` since this breaks the `rdf-parser` library
describe('A RdfToQuadConverter', (): void => {
const fetchMock: jest.Mock = fetch as any;
const converter = new RdfToQuadConverter();
const identifier: ResourceIdentifier = { path: 'http://example.com/resource' };
it('supports serializing as quads.', async(): Promise<void> => {
const types = rdfParser.getContentTypes()
.then((inputTypes): string[] => inputTypes.filter((type): boolean => type !== 'application/json'));
@@ -123,4 +139,43 @@ describe('A RdfToQuadConverter', (): void => {
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(BadRequestHttpError);
});
it('can use locally stored contexts.', async(): Promise<void> => {
const fetchedContext = {
'@context': {
'@version': 1.1,
test: 'http://example.com/context2#',
testPredicate2: { '@id': 'test:predicate2' },
},
};
// This depends on the fields needed by the `jsonld-context-parser` so could break if library changes
fetchMock.mockResolvedValueOnce({
json: (): any => fetchedContext,
status: 200,
ok: true,
headers: new Headers({ 'content-type': 'application/ld+json' }),
});
const contextConverter = new RdfToQuadConverter(
{ 'http://example.com/context.jsonld': '@css:test/assets/contexts/test.jsonld' },
);
const jsonld = {
'@context': [ 'http://example.com/context.jsonld', 'http://example.com/context2.jsonld' ],
'@id': 'http://example.com/resource',
testPredicate: 123,
testPredicate2: 456,
};
const representation = new BasicRepresentation(JSON.stringify(jsonld), 'application/ld+json');
const preferences: RepresentationPreferences = { type: { [INTERNAL_QUADS]: 1 }};
const result = await contextConverter.handle({ identifier, representation, preferences });
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://example.com/resource'),
namedNode('http://example.com/context#predicate'),
literal(123),
), triple(
namedNode('http://example.com/resource'),
namedNode('http://example.com/context2#predicate2'),
literal(456),
) ]);
});
});