mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
test: Add WebSockets integration test.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
59f99e1728
commit
16d447f221
@@ -1,4 +1,6 @@
|
||||
import { join } from 'path';
|
||||
import * as Path from 'path';
|
||||
import { Loader } from 'componentsjs';
|
||||
import type {
|
||||
BodyParser,
|
||||
DataAccessor,
|
||||
@@ -173,3 +175,18 @@ export const getBasicRequestParser = (bodyParsers: BodyParser[] = []): BasicRequ
|
||||
*/
|
||||
export const getWebAclAuthorizer = (store: ResourceStore, aclManager = new UrlBasedAclManager()): WebAclAuthorizer =>
|
||||
new WebAclAuthorizer(aclManager, store);
|
||||
|
||||
/**
|
||||
* Returns a component instantiated from a Components.js configuration.
|
||||
*/
|
||||
export const instantiateFromConfig = async(componentUrl: string, configFile: string,
|
||||
variables?: Record<string, any>): Promise<any> => {
|
||||
// Initialize the Components.js loader
|
||||
const mainModulePath = Path.join(__dirname, '../../');
|
||||
const loader = new Loader({ mainModulePath });
|
||||
await loader.registerAvailableModuleResources();
|
||||
|
||||
// Instantiate the component from the config
|
||||
const configPath = Path.join(__dirname, configFile);
|
||||
return loader.instantiateFromUrl(componentUrl, configPath, undefined, { variables });
|
||||
};
|
||||
|
||||
48
test/configs/websockets.json
Normal file
48
test/configs/websockets.json
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld",
|
||||
"import": [
|
||||
"files-scs:config/presets/http.json",
|
||||
"files-scs:config/presets/ldp/credentials-extractor.json",
|
||||
"files-scs:config/presets/ldp/metadata-handler.json",
|
||||
"files-scs:config/presets/ldp/operation-handler.json",
|
||||
"files-scs:config/presets/ldp/permissions-extractor.json",
|
||||
"files-scs:config/presets/ldp/response-writer.json",
|
||||
"files-scs:config/presets/ldp/request-parser.json",
|
||||
"files-scs:config/presets/ldp/websockets.json",
|
||||
"files-scs:config/presets/representation-conversion.json",
|
||||
"files-scs:config/presets/storage/backend/storage-memory.json",
|
||||
"files-scs:config/presets/storage/routing/no-routing.json",
|
||||
"files-scs:config/presets/storage-wrapper.json",
|
||||
"files-scs:config/presets/cli-params.json"
|
||||
],
|
||||
"@graph": [
|
||||
{
|
||||
"@id": "urn:solid-server:default:HttpHandler",
|
||||
"@type": "AuthenticatedLdpHandler",
|
||||
"AuthenticatedLdpHandler:_args_requestParser": {
|
||||
"@id": "urn:solid-server:default:RequestParser"
|
||||
},
|
||||
"AuthenticatedLdpHandler:_args_credentialsExtractor": {
|
||||
"@id": "urn:solid-server:default:CredentialsExtractor"
|
||||
},
|
||||
"AuthenticatedLdpHandler:_args_permissionsExtractor": {
|
||||
"@id": "urn:solid-server:default:PermissionsExtractor"
|
||||
},
|
||||
"AuthenticatedLdpHandler:_args_authorizer": {
|
||||
"@type": "AllowEverythingAuthorizer"
|
||||
},
|
||||
"AuthenticatedLdpHandler:_args_operationHandler": {
|
||||
"@id": "urn:solid-server:default:OperationHandler"
|
||||
},
|
||||
"AuthenticatedLdpHandler:_args_responseWriter": {
|
||||
"@id": "urn:solid-server:default:ResponseWriter"
|
||||
}
|
||||
},
|
||||
{
|
||||
"@id": "urn:solid-server:default:RoutingResourceStore",
|
||||
"PassthroughStore:_source": {
|
||||
"@id": "urn:solid-server:default:MemoryResourceStore"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
84
test/integration/WebSocketsProtocol.test.ts
Normal file
84
test/integration/WebSocketsProtocol.test.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import type { Server } from 'http';
|
||||
import fetch from 'cross-fetch';
|
||||
import WebSocket from 'ws';
|
||||
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
||||
import { instantiateFromConfig } from '../configs/Util';
|
||||
|
||||
const port = 6001;
|
||||
const baseUrl = `http://localhost:${port}/`;
|
||||
|
||||
describe('A server with the Solid WebSockets API', (): void => {
|
||||
let server: Server;
|
||||
|
||||
beforeAll(async(): Promise<void> => {
|
||||
const factory = await instantiateFromConfig(
|
||||
'urn:solid-server:default:ServerFactory', 'websockets.json', {
|
||||
'urn:solid-server:default:variable:port': port,
|
||||
'urn:solid-server:default:variable:base': baseUrl,
|
||||
},
|
||||
) as HttpServerFactory;
|
||||
server = factory.startServer(port);
|
||||
});
|
||||
|
||||
afterAll(async(): Promise<void> => {
|
||||
await new Promise((resolve, reject): void => {
|
||||
server.close((error): void => error ? reject(error) : resolve());
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a 200.', async(): Promise<void> => {
|
||||
const response = await fetch(baseUrl);
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
it('sets the Updates-Via header.', async(): Promise<void> => {
|
||||
const response = await fetch(baseUrl);
|
||||
expect(response.headers.get('Updates-Via')).toBe(`ws://localhost:${port}`);
|
||||
});
|
||||
|
||||
describe('when a WebSocket client connects', (): void => {
|
||||
let client: WebSocket;
|
||||
const messages = new Array<string>();
|
||||
|
||||
beforeAll(async(): Promise<void> => {
|
||||
client = new WebSocket(`ws://localhost:${port}`, [ 'solid/0.1.0-alpha' ]);
|
||||
client.on('message', (message: string): any => messages.push(message));
|
||||
await new Promise((resolve): any => client.on('open', resolve));
|
||||
});
|
||||
|
||||
afterAll((): void => {
|
||||
client.close();
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
messages.length = 0;
|
||||
});
|
||||
|
||||
it('sends the protocol version.', (): void => {
|
||||
expect(messages).toEqual([
|
||||
'protocol solid/0.1.0-alpha',
|
||||
'warning Unstandardized protocol version, proceed with care',
|
||||
]);
|
||||
});
|
||||
|
||||
describe('when the client subscribes to a resource', (): void => {
|
||||
beforeAll(async(): Promise<void> => {
|
||||
client.send(`sub ${baseUrl}my-resource`);
|
||||
await new Promise((resolve): any => client.once('message', resolve));
|
||||
});
|
||||
|
||||
it('acknowledges the subscription.', async(): Promise<void> => {
|
||||
expect(messages).toEqual([ `ack ${baseUrl}my-resource` ]);
|
||||
});
|
||||
|
||||
it('notifies the client of resource updates.', async(): Promise<void> => {
|
||||
await fetch(`${baseUrl}my-resource`, {
|
||||
method: 'PUT',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: '{}',
|
||||
});
|
||||
expect(messages).toEqual([ `pub ${baseUrl}my-resource` ]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user