feat: Support GET requests on subscription services

Doing a GET request on a subscription resource
will return the expected representation.
Content negotiation is supported.
This commit is contained in:
Joachim Van Herwegen
2023-01-31 11:18:35 +01:00
parent b2f4d7fb2d
commit 65860f77da
27 changed files with 446 additions and 211 deletions

View File

@@ -1,34 +1,56 @@
import 'jest-rdf';
import { DataFactory } from 'n3';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import type { Representation } from '../../../../src/http/representation/Representation';
import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier';
import {
AbsolutePathInteractionRoute,
} from '../../../../src/identity/interaction/routing/AbsolutePathInteractionRoute';
import type { NotificationChannelType } from '../../../../src/server/notifications/NotificationChannelType';
import { NotificationDescriber } from '../../../../src/server/notifications/NotificationDescriber';
import type {
RepresentationConverter,
RepresentationConverterArgs,
} from '../../../../src/storage/conversion/RepresentationConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { readableToString } from '../../../../src/util/StreamUtil';
import { NOTIFY } from '../../../../src/util/Vocabularies';
const { namedNode, quad } = DataFactory;
describe('A NotificationDescriber', (): void => {
const identifier: ResourceIdentifier = { path: 'http://example.com/foo' };
const route = new AbsolutePathInteractionRoute('http://example.com/.notifications/websockets/');
const type = 'http://www.w3.org/ns/solid/notifications#WebSocketSubscription2021';
const identifier: ResourceIdentifier = { path: 'http://example.com/solid/' };
const jsonld1 = { id: 'http://example.com/.notifications/websockets/' };
const jsonld2 = { id: 'http://example.com/.notifications/extra/' };
let converter: jest.Mocked<RepresentationConverter>;
let subscription1: jest.Mocked<NotificationChannelType>;
let subscription2: jest.Mocked<NotificationChannelType>;
let describer: NotificationDescriber;
beforeEach(async(): Promise<void> => {
describer = new NotificationDescriber(route, type);
subscription1 = {
getDescription: jest.fn().mockReturnValue(jsonld1),
} as any;
subscription2 = {
getDescription: jest.fn().mockReturnValue(jsonld2),
} as any;
converter = {
handleSafe: jest.fn(async({ representation }: RepresentationConverterArgs): Promise<Representation> => {
const jsonld = JSON.parse(await readableToString(representation.data));
return new BasicRepresentation([
quad(namedNode(jsonld.id), NOTIFY.terms.feature, NOTIFY.terms.rate),
], INTERNAL_QUADS);
}),
} as any;
describer = new NotificationDescriber(converter, [ subscription1, subscription2 ]);
});
it('outputs the expected quads.', async(): Promise<void> => {
const subscription = namedNode('http://example.com/.notifications/websockets/');
const quads = await describer.handle(identifier);
expect(quads).toBeRdfIsomorphic([
quad(namedNode(identifier.path), NOTIFY.terms.subscription, subscription),
quad(subscription, NOTIFY.terms.channelType, namedNode(type)),
quad(subscription, NOTIFY.terms.feature, NOTIFY.terms.accept),
quad(subscription, NOTIFY.terms.feature, NOTIFY.terms.endAt),
quad(subscription, NOTIFY.terms.feature, NOTIFY.terms.startAt),
quad(subscription, NOTIFY.terms.feature, NOTIFY.terms.rate),
quad(subscription, NOTIFY.terms.feature, NOTIFY.terms.state),
it('converts the JSON-LD to quads.', async(): Promise<void> => {
await expect(describer.handle(identifier)).resolves.toBeRdfIsomorphic([
quad(namedNode(identifier.path), NOTIFY.terms.subscription, namedNode(jsonld1.id)),
quad(namedNode(identifier.path), NOTIFY.terms.subscription, namedNode(jsonld2.id)),
quad(namedNode(jsonld1.id), NOTIFY.terms.feature, NOTIFY.terms.rate),
quad(namedNode(jsonld2.id), NOTIFY.terms.feature, NOTIFY.terms.rate),
]);
});
});