feat: Add metadata to errors

This commit is contained in:
Joachim Van Herwegen
2023-07-25 14:10:46 +02:00
parent a333412e19
commit f373dff1d7
42 changed files with 455 additions and 419 deletions

View File

@@ -9,6 +9,7 @@ import { RepresentationMetadata } from '../../../../../src/http/representation/R
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import { BadRequestHttpError } from '../../../../../src/util/errors/BadRequestHttpError';
import { UnsupportedMediaTypeHttpError } from '../../../../../src/util/errors/UnsupportedMediaTypeHttpError';
import { ContentType } from '../../../../../src/util/Header';
import { guardedStreamFrom } from '../../../../../src/util/StreamUtil';
const { namedNode, quad } = DataFactory;
@@ -24,11 +25,10 @@ describe('A SparqlUpdateBodyParser', (): void => {
await expect(bodyParser.canHandle(input)).rejects.toThrow(UnsupportedMediaTypeHttpError);
input.metadata.contentType = 'text/plain';
await expect(bodyParser.canHandle(input)).rejects.toThrow(UnsupportedMediaTypeHttpError);
input.metadata.contentType = 'application/sparql-update;charset=utf-8';
const contentType = new ContentType('application/sparql-update');
input.metadata.contentTypeObject = contentType;
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
input.metadata.contentType = 'application/sparql-update ; foo=bar';
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
input.metadata.contentType = 'application/sparql-update';
contentType.parameters = { charset: 'utf-8' };
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
});

View File

@@ -11,6 +11,7 @@ import type {
RepresentationConverter,
RepresentationConverterArgs,
} from '../../../../../src/storage/conversion/RepresentationConverter';
import type { HttpError } from '../../../../../src/util/errors/HttpError';
import { NotFoundHttpError } from '../../../../../src/util/errors/NotFoundHttpError';
import { HTTP, XSD } from '../../../../../src/util/Vocabularies';
import literal = DataFactory.literal;
@@ -33,7 +34,7 @@ async function expectValidArgs(args: RepresentationConverterArgs, stack?: string
describe('A ConvertingErrorHandler', (): void => {
// The error object can get modified by the handler
let error: Error;
let error: HttpError;
let stack: string | undefined;
const request = {} as HttpRequest;
let converter: jest.Mocked<RepresentationConverter>;

View File

@@ -2,6 +2,7 @@ import { createResponse } from 'node-mocks-http';
import { ContentTypeMetadataWriter } from '../../../../../src/http/output/metadata/ContentTypeMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { HttpResponse } from '../../../../../src/server/HttpResponse';
import { ContentType } from '../../../../../src/util/Header';
describe('A ContentTypeMetadataWriter', (): void => {
const writer = new ContentTypeMetadataWriter();
@@ -18,18 +19,12 @@ describe('A ContentTypeMetadataWriter', (): void => {
});
it('adds a Content-Type header with parameters if present.', async(): Promise<void> => {
const metadata = new RepresentationMetadata('text/plain; charset=utf-8');
const metadata = new RepresentationMetadata(new ContentType('text/plain', { charset: 'utf-8' }));
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({
'content-type': 'text/plain; charset=utf-8',
});
const metadata2 = new RepresentationMetadata('text/plain; charset="utf-8"');
await expect(writer.handle({ response, metadata: metadata2 })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({
'content-type': 'text/plain; charset=utf-8',
});
});
it('adds a Content-Type header without parameters.', async(): Promise<void> => {

View File

@@ -8,6 +8,7 @@ describe('A RedirectResponseDescription', (): void => {
it('has status the code and location of the error.', async(): Promise<void> => {
const description = new RedirectResponseDescription(error);
expect(description.metadata?.get(SOLID_HTTP.terms.location)?.value).toBe(error.location);
expect(description.metadata).toBe(error.metadata);
expect(description.statusCode).toBe(error.statusCode);
});
});

View File

@@ -2,8 +2,8 @@ import 'jest-rdf';
import type { BlankNode } from 'n3';
import { DataFactory } from 'n3';
import type { NamedNode, Quad } from 'rdf-js';
import { ContentType } from '../../../../src';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { ContentType } from '../../../../src/util/Header';
import { CONTENT_TYPE_TERM, SOLID_META, RDFS } from '../../../../src/util/Vocabularies';
const { defaultGraph, literal, namedNode, quad } = DataFactory;
@@ -308,14 +308,14 @@ describe('A RepresentationMetadata', (): void => {
it('has a shorthand for Content-Type as string.', async(): Promise<void> => {
expect(metadata.contentType).toBeUndefined();
expect(metadata.contentTypeObject).toBeUndefined();
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
expect(metadata.contentTypeObject).toEqual({
value: 'text/plain',
parameters: {
charset: 'utf-8',
test: 'value1',
},
});
metadata.contentType = 'text/plain';
expect(metadata.contentTypeObject).toEqual({ value: 'text/plain', parameters: {}});
});
it('errors trying to set a Content-Type with parameters using a string.', async(): Promise<void> => {
expect((): void => {
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
}).toThrow(Error);
});
it('has a shorthand for Content-Type as object.', async(): Promise<void> => {
@@ -341,7 +341,10 @@ describe('A RepresentationMetadata', (): void => {
it('can properly clear the Content-Type parameters explicitly.', async(): Promise<void> => {
expect(metadata.contentType).toBeUndefined();
expect(metadata.contentTypeObject).toBeUndefined();
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
metadata.contentTypeObject = new ContentType('text/plain', {
charset: 'utf-8',
test: 'value1',
});
metadata.contentType = undefined;
expect(metadata.contentType).toBeUndefined();
expect(metadata.contentTypeObject).toBeUndefined();
@@ -353,7 +356,10 @@ describe('A RepresentationMetadata', (): void => {
it('can properly clear the Content-Type parameters implicitly.', async(): Promise<void> => {
expect(metadata.contentType).toBeUndefined();
expect(metadata.contentTypeObject).toBeUndefined();
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
metadata.contentTypeObject = new ContentType('text/plain', {
charset: 'utf-8',
test: 'value1',
});
metadata.contentType = 'text/turtle';
expect(metadata.contentType).toBe('text/turtle');
expect(metadata.contentTypeObject).toEqual({
@@ -368,7 +374,10 @@ describe('A RepresentationMetadata', (): void => {
it('can return invalid parameters when too many quads are present.', async(): Promise<void> => {
expect(metadata.contentType).toBeUndefined();
expect(metadata.contentTypeObject).toBeUndefined();
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
metadata.contentTypeObject = new ContentType('text/plain', {
charset: 'utf-8',
test: 'value1',
});
const param = metadata.quads(null, SOLID_META.terms.value)[0].subject;
metadata.addQuad(param as BlankNode, SOLID_META.terms.value, 'anomaly');
expect(metadata.contentTypeObject?.parameters).toMatchObject({ invalid: '' });