From ed287ffade1b5b3d4621f97ad559bb72479eaaf6 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Thu, 28 Oct 2021 15:35:48 +0200 Subject: [PATCH] test: Add content negotiation integration tests --- test/integration/ContentNegotiation.test.ts | 67 +++++++++++++++++++ .../RepresentationConverter.test.ts | 47 ------------- test/util/Util.ts | 1 + 3 files changed, 68 insertions(+), 47 deletions(-) create mode 100644 test/integration/ContentNegotiation.test.ts delete mode 100644 test/integration/RepresentationConverter.test.ts diff --git a/test/integration/ContentNegotiation.test.ts b/test/integration/ContentNegotiation.test.ts new file mode 100644 index 000000000..8573df15b --- /dev/null +++ b/test/integration/ContentNegotiation.test.ts @@ -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 => { + // Start the server + const instances = await instantiateFromConfig( + 'urn:solid-server:test:Instances', + getTestConfigPath('server-memory.json'), + getDefaultVariables(port, baseUrl), + ) as Record; + ({ 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 => { + 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 => { + const res = await fetch(`${baseUrl}${name}`, { headers: { accept }}); + expect(res.status).toBe(200); + expect(res.headers.get('Content-Type')).toBe(expected); + }); + }); +}); diff --git a/test/integration/RepresentationConverter.test.ts b/test/integration/RepresentationConverter.test.ts deleted file mode 100644 index 86c6c7413..000000000 --- a/test/integration/RepresentationConverter.test.ts +++ /dev/null @@ -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 => { - 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(' .\n'); - expect(result.metadata.contentType).toEqual('text/turtle'); - }); - - it('can convert from turtle to JSON-LD.', async(): Promise => { - const representation = new BasicRepresentation( - ' .', - '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'); - }); -}); diff --git a/test/util/Util.ts b/test/util/Util.ts index c87b934a4..40e652c00 100644 --- a/test/util/Util.ts +++ b/test/util/Util.ts @@ -5,6 +5,7 @@ import type { SystemError } from '../../src/util/errors/SystemError'; const portNames = [ // Integration 'Conditions', + 'ContentNegotiation', 'DynamicPods', 'Identity', 'LpdHandlerWithAuth',