fix: Enable strict TypeScript settings

This commit is contained in:
Joachim Van Herwegen
2020-07-24 09:27:44 +02:00
parent 4001050588
commit dcff424f58
28 changed files with 115 additions and 105 deletions

View File

@@ -1,5 +1,8 @@
import { Authorizer } from '../../../src/authorization/Authorizer';
import { CredentialsExtractor } from '../../../src/authentication/CredentialsExtractor';
import { HttpRequest } from '../../../src/server/HttpRequest';
import { HttpResponse } from '../../../src/server/HttpResponse';
import { Operation } from '../../../src/ldp/operations/Operation';
import { OperationHandler } from '../../../src/ldp/operations/OperationHandler';
import { PermissionsExtractor } from '../../../src/ldp/permissions/PermissionsExtractor';
import { RequestParser } from '../../../src/ldp/http/RequestParser';
@@ -36,30 +39,30 @@ describe('An AuthenticatedLdpHandler', (): void => {
it('can check if it handles input.', async(): Promise<void> => {
const handler = new AuthenticatedLdpHandler(args);
await expect(handler.canHandle({ request: null, response: null })).resolves.toBeUndefined();
await expect(handler.canHandle({ request: {} as HttpRequest, response: {} as HttpResponse })).resolves.toBeUndefined();
});
it('can handle input.', async(): Promise<void> => {
const handler = new AuthenticatedLdpHandler(args);
await expect(handler.handle({ request: 'request' as any, response: 'response' as any })).resolves.toEqual(undefined);
await expect(handler.handle({ request: 'request' as any, response: 'response' as any })).resolves.toBeUndefined();
expect(responseFn).toHaveBeenCalledTimes(1);
expect(responseFn).toHaveBeenLastCalledWith({ response: 'response', description: 'operation' as any });
expect(responseFn).toHaveBeenLastCalledWith({ response: 'response', result: 'operation' as any });
});
it('sends an error to the output if a handler does not support the input.', async(): Promise<void> => {
args.requestParser = new StaticAsyncHandler(false, null);
args.requestParser = new StaticAsyncHandler(false, {} as Operation);
const handler = new AuthenticatedLdpHandler(args);
await expect(handler.handle({ request: 'request' as any, response: null })).resolves.toEqual(undefined);
await expect(handler.handle({ request: 'request' as any, response: {} as HttpResponse })).resolves.toBeUndefined();
expect(responseFn).toHaveBeenCalledTimes(1);
expect(responseFn.mock.calls[0][0].error).toBeInstanceOf(Error);
expect(responseFn.mock.calls[0][0].result).toBeInstanceOf(Error);
});
it('errors if the response writer does not support the result.', async(): Promise< void> => {
args.responseWriter = new StaticAsyncHandler(false, null);
args.responseWriter = new StaticAsyncHandler(false, undefined);
const handler = new AuthenticatedLdpHandler(args);
await expect(handler.handle({ request: 'request' as any, response: null })).rejects.toThrow(Error);
await expect(handler.handle({ request: 'request' as any, response: {} as HttpResponse })).rejects.toThrow(Error);
});
});

View File

@@ -41,7 +41,7 @@ describe('A SimpleBodyparser', (): void => {
it('returns a stream of quads if there was data.', async(): Promise<void> => {
const input = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest;
input.headers = { 'content-type': 'text/turtle' };
const result = await bodyParser.handle(input);
const result = (await bodyParser.handle(input))!;
expect(result).toEqual({
data: expect.any(Readable),
dataType: 'quad',
@@ -61,7 +61,7 @@ describe('A SimpleBodyparser', (): void => {
it('throws an UnsupportedHttpError on invalid triple data when reading the stream.', async(): Promise<void> => {
const input = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>' ]) as HttpRequest;
input.headers = { 'content-type': 'text/turtle' };
const result = await bodyParser.handle(input);
const result = (await bodyParser.handle(input))!;
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
});
});

View File

@@ -29,6 +29,10 @@ describe('A SimpleRequestParser', (): void => {
await expect(requestParser.canHandle({ url: 'url' } as any)).rejects.toThrow('Missing method.');
});
it('errors if called without method.', async(): Promise<void> => {
await expect(requestParser.handle({ url: 'url' } as any)).rejects.toThrow('Missing method.');
});
it('returns the output of all input parsers after calling handle.', async(): Promise<void> => {
await expect(requestParser.handle({ url: 'url', method: 'GET' } as any)).resolves.toEqual({
method: 'GET',

View File

@@ -14,20 +14,14 @@ describe('A SimpleResponseWriter', (): void => {
response = createResponse({ eventEmitter: EventEmitter });
});
it('can handle input that has at least a description or an error.', async(): Promise<void> => {
await expect(writer.canHandle({ response, description: {} as ResponseDescription })).resolves.toBeUndefined();
await expect(writer.canHandle({ response, error: {} as Error })).resolves.toBeUndefined();
await expect(writer.canHandle({ response })).rejects.toThrow(UnsupportedHttpError);
});
it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => {
await expect(writer.canHandle({ response, description: { body: { dataType: 'quad' }} as ResponseDescription })).rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, description: { body: { dataType: 'string' }} as ResponseDescription })).resolves.toBeUndefined();
await expect(writer.canHandle({ response, description: { body: { dataType: 'binary' }} as ResponseDescription })).resolves.toBeUndefined();
await expect(writer.canHandle({ response, result: { body: { dataType: 'quad' }} as ResponseDescription })).rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { dataType: 'string' }} as ResponseDescription })).resolves.toBeUndefined();
await expect(writer.canHandle({ response, result: { body: { dataType: 'binary' }} as ResponseDescription })).resolves.toBeUndefined();
});
it('responds with status code 200 and a location header if there is a description.', async(): Promise<void> => {
await writer.handle({ response, description: { identifier: { path: 'path' }}});
await writer.handle({ response, result: { identifier: { path: 'path' }}});
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(200);
expect(response._getHeaders()).toMatchObject({ location: 'path' });
@@ -51,7 +45,7 @@ describe('A SimpleResponseWriter', (): void => {
done();
});
await writer.handle({ response, description: { identifier: { path: 'path' }, body }});
await writer.handle({ response, result: { identifier: { path: 'path' }, body }});
});
it('responds with a content-type if the metadata has it.', async(done): Promise<void> => {
@@ -73,11 +67,11 @@ describe('A SimpleResponseWriter', (): void => {
done();
});
await writer.handle({ response, description: { identifier: { path: 'path' }, body }});
await writer.handle({ response, result: { identifier: { path: 'path' }, body }});
});
it('responds with 500 if an error if there is an error.', async(): Promise<void> => {
await writer.handle({ response, error: new Error('error') });
await writer.handle({ response, result: new Error('error') });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(500);
expect(response._getData()).toMatch('Error: error');
@@ -85,7 +79,7 @@ describe('A SimpleResponseWriter', (): void => {
it('responds with the given statuscode if there is an HttpError.', async(): Promise<void> => {
const error = new UnsupportedHttpError('error');
await writer.handle({ response, error });
await writer.handle({ response, result: error });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(error.statusCode);
expect(response._getData()).toMatch('UnsupportedHttpError: error');

View File

@@ -16,6 +16,10 @@ describe('A SimplePostOperationHandler', (): void => {
await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
});
it('errors if no body is present.', async(): Promise<void> => {
await expect(handler.handle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
});
it('adds the given representation to the store and returns the new identifier.', async(): Promise<void> => {
await expect(handler.handle({ method: 'POST', body: { dataType: 'test' }} as Operation)).resolves.toEqual({ identifier: { path: 'newPath' }});
});