test: Add content negotiation integration tests

This commit is contained in:
Ruben Verborgh 2021-10-28 15:35:48 +02:00 committed by Joachim Van Herwegen
parent d52aa94e53
commit ed287ffade
3 changed files with 68 additions and 47 deletions

View File

@ -0,0 +1,67 @@
import assert from 'assert';
import fetch from 'cross-fetch';
import type { App } from '../../src/init/App';
import { getPort } from '../util/Util';
import { getDefaultVariables, getTestConfigPath, instantiateFromConfig } from './Config';
const port = getPort('ContentNegotiation');
const baseUrl = `http://localhost:${port}`;
const documents = [
[ '/turtle', 'text/turtle', '# Test' ],
[ '/markdown', 'text/markdown', '# Test' ],
];
const cases: [string, string, string][] = [
[ '/turtle', 'text/turtle', '' ],
[ '/turtle', 'text/turtle', '*/*' ],
[ '/turtle', 'text/turtle', 'text/html,*/*;q=0.1' ],
[ '/turtle', 'application/json', 'application/json' ],
[ '/turtle', 'application/ld+json', 'application/ld+json' ],
[ '/turtle', 'application/octet-stream', 'application/octet-stream' ],
[ '/markdown', 'text/markdown', '' ],
[ '/markdown', 'text/markdown', '*/*' ],
[ '/markdown', 'text/markdown', 'text/html,text/markdown' ],
[ '/markdown', 'text/markdown', 'text/markdown;q=0.9, text/html;q=0.1' ],
[ '/markdown', 'text/html', 'text/html' ],
[ '/markdown', 'text/html', 'text/html,*/*;q=0.8' ],
[ '/markdown', 'text/html', 'text/markdown;q=0.1, text/html;q=0.9' ],
[ '/markdown', 'application/octet-stream', 'application/octet-stream' ],
];
describe('Content negotiation', (): void => {
let app: App;
beforeAll(async(): Promise<void> => {
// Start the server
const instances = await instantiateFromConfig(
'urn:solid-server:test:Instances',
getTestConfigPath('server-memory.json'),
getDefaultVariables(port, baseUrl),
) as Record<string, any>;
({ app } = instances);
await app.start();
// Create documents
for (const [ slug, contentType, body ] of documents) {
const res = await fetch(`${baseUrl}${slug}`, {
method: 'PUT',
headers: { 'content-type': contentType },
body,
});
assert.strictEqual(res.status, 201);
}
});
afterAll(async(): Promise<void> => {
await app.stop();
});
describe.each(cases)('a request for %s', (name, expected, accept): void => {
it(`results in ${expected} in response to Accept: ${accept}`, async(): Promise<void> => {
const res = await fetch(`${baseUrl}${name}`, { headers: { accept }});
expect(res.status).toBe(200);
expect(res.headers.get('Content-Type')).toBe(expected);
});
});
});

View File

@ -1,47 +0,0 @@
import { BasicRepresentation } from '../../src/http/representation/BasicRepresentation';
import { ChainedConverter } from '../../src/storage/conversion/ChainedConverter';
import { QuadToRdfConverter } from '../../src/storage/conversion/QuadToRdfConverter';
import { RdfToQuadConverter } from '../../src/storage/conversion/RdfToQuadConverter';
import { readableToString } from '../../src/util/StreamUtil';
describe('A ChainedConverter', (): void => {
const converters = [
new RdfToQuadConverter(),
new QuadToRdfConverter(),
];
const converter = new ChainedConverter(converters);
it('can convert from JSON-LD to turtle.', async(): Promise<void> => {
const representation = new BasicRepresentation(
'{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}',
'application/ld+json',
);
const result = await converter.handleSafe({
representation,
preferences: { type: { 'text/turtle': 1 }},
identifier: { path: 'path' },
});
await expect(readableToString(result.data)).resolves.toEqual('<http://test.com/s> <http://test.com/p> <http://test.com/o>.\n');
expect(result.metadata.contentType).toEqual('text/turtle');
});
it('can convert from turtle to JSON-LD.', async(): Promise<void> => {
const representation = new BasicRepresentation(
'<http://test.com/s> <http://test.com/p> <http://test.com/o>.',
'text/turtle',
);
const result = await converter.handleSafe({
representation,
preferences: { type: { 'application/ld+json': 1 }},
identifier: { path: 'path' },
});
expect(JSON.parse(await readableToString(result.data))).toEqual(
[{ '@id': 'http://test.com/s', 'http://test.com/p': [{ '@id': 'http://test.com/o' }]}],
);
expect(result.metadata.contentType).toEqual('application/ld+json');
});
});

View File

@ -5,6 +5,7 @@ import type { SystemError } from '../../src/util/errors/SystemError';
const portNames = [
// Integration
'Conditions',
'ContentNegotiation',
'DynamicPods',
'Identity',
'LpdHandlerWithAuth',