mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add utility functions for generating error classes
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import 'jest-rdf';
|
||||
import { DataFactory } from 'n3';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError';
|
||||
import { ForbiddenHttpError } from '../../../../src/util/errors/ForbiddenHttpError';
|
||||
import type { HttpErrorOptions } from '../../../../src/util/errors/HttpError';
|
||||
import { HttpError } from '../../../../src/util/errors/HttpError';
|
||||
import { generateHttpErrorUri } from '../../../../src/util/errors/HttpError';
|
||||
import type { HttpErrorClass } from '../../../../src/util/errors/HttpError';
|
||||
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
||||
import { MethodNotAllowedHttpError } from '../../../../src/util/errors/MethodNotAllowedHttpError';
|
||||
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
|
||||
@@ -12,16 +14,11 @@ import { PreconditionFailedHttpError } from '../../../../src/util/errors/Precond
|
||||
import { UnauthorizedHttpError } from '../../../../src/util/errors/UnauthorizedHttpError';
|
||||
import { UnprocessableEntityHttpError } from '../../../../src/util/errors/UnprocessableEntityHttpError';
|
||||
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
|
||||
|
||||
// Only used to make typings easier in the tests
|
||||
class FixedHttpError extends HttpError {
|
||||
public constructor(message?: string, options?: HttpErrorOptions) {
|
||||
super(0, '', message, options);
|
||||
}
|
||||
}
|
||||
import { SOLID_ERROR } from '../../../../src/util/Vocabularies';
|
||||
const { literal, namedNode, quad } = DataFactory;
|
||||
|
||||
describe('HttpError', (): void => {
|
||||
const errors: [string, number, typeof FixedHttpError][] = [
|
||||
const errors: [string, number, HttpErrorClass][] = [
|
||||
[ 'BadRequestHttpError', 400, BadRequestHttpError ],
|
||||
[ 'UnauthorizedHttpError', 401, UnauthorizedHttpError ],
|
||||
[ 'ForbiddenHttpError', 403, ForbiddenHttpError ],
|
||||
@@ -48,6 +45,10 @@ describe('HttpError', (): void => {
|
||||
expect(constructor.isInstance(instance)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('has a URI.', (): void => {
|
||||
expect(constructor.uri).toEqualRdfTerm(generateHttpErrorUri(statusCode));
|
||||
});
|
||||
|
||||
it(`has name ${name}.`, (): void => {
|
||||
expect(instance.name).toBe(name);
|
||||
});
|
||||
@@ -75,5 +76,12 @@ describe('HttpError', (): void => {
|
||||
it('sets the details.', (): void => {
|
||||
expect(instance.details).toBe(options.details);
|
||||
});
|
||||
|
||||
it('generates metadata.', (): void => {
|
||||
const subject = namedNode('subject');
|
||||
expect(instance.generateMetadata(subject)).toBeRdfIsomorphic([
|
||||
quad(subject, SOLID_ERROR.terms.errorResponse, constructor.uri),
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
import { FoundHttpError } from '../../../../src/util/errors/FoundHttpError';
|
||||
import type { HttpErrorOptions } from '../../../../src/util/errors/HttpError';
|
||||
import { generateHttpErrorUri } from '../../../../src/util/errors/HttpError';
|
||||
import { MovedPermanentlyHttpError } from '../../../../src/util/errors/MovedPermanentlyHttpError';
|
||||
import { RedirectHttpError } from '../../../../src/util/errors/RedirectHttpError';
|
||||
import type { RedirectHttpErrorClass } from '../../../../src/util/errors/RedirectHttpError';
|
||||
|
||||
// Used to make sure the RedirectHttpError constructor also gets called in a test.
|
||||
class FixedRedirectHttpError extends RedirectHttpError {
|
||||
public static readonly statusCode = 0;
|
||||
public static readonly uri = generateHttpErrorUri(0);
|
||||
|
||||
public constructor(location: string, message?: string, options?: HttpErrorOptions) {
|
||||
super(0, location, '', message, options);
|
||||
super(0, 'RedirectHttpError', location, message, options);
|
||||
}
|
||||
}
|
||||
|
||||
describe('RedirectHttpError', (): void => {
|
||||
const errors: [string, number, typeof FixedRedirectHttpError][] = [
|
||||
const errors: [string, number, RedirectHttpErrorClass][] = [
|
||||
[ 'RedirectHttpError', 0, FixedRedirectHttpError ],
|
||||
[ 'MovedPermanentlyHttpError', 301, MovedPermanentlyHttpError ],
|
||||
[ 'FoundHttpError', 302, FoundHttpError ],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user