fix: Let Representations always have a body

This is relevant when the request has a content-type
but no data.
This commit is contained in:
Joachim Van Herwegen
2021-10-11 15:21:40 +02:00
parent f1ef2ced03
commit 5613ff9e71
36 changed files with 95 additions and 63 deletions

View File

@@ -19,10 +19,6 @@ describe('A SparqlPatchModesExtractor', (): void => {
await expect(result).rejects.toThrow(NotImplementedHttpError);
await expect(result).rejects.toThrow('Cannot determine permissions of GET, only PATCH.');
result = extractor.canHandle({ ...operation, body: undefined });
await expect(result).rejects.toThrow(NotImplementedHttpError);
await expect(result).rejects.toThrow('Cannot determine permissions of PATCH operations without a body.');
result = extractor.canHandle({ ...operation, body: {} as SparqlUpdatePatch });
await expect(result).rejects.toThrow(NotImplementedHttpError);
await expect(result).rejects.toThrow('Cannot determine permissions of non-SPARQL patches.');

View File

@@ -19,16 +19,18 @@ describe('A RawBodyparser', (): void => {
});
it('returns empty output if there is no content length or transfer encoding.', async(): Promise<void> => {
input.request = guardedStreamFrom([ '' ]) as HttpRequest;
input.request = guardedStreamFrom([ 'data' ]) as HttpRequest;
input.request.headers = {};
await expect(bodyParser.handle(input)).resolves.toBeUndefined();
const result = await bodyParser.handle(input);
await expect(arrayifyStream(result.data)).resolves.toEqual([]);
});
// https://github.com/solid/community-server/issues/498
it('returns empty output if the content length is 0 and there is no content type.', async(): Promise<void> => {
input.request = guardedStreamFrom([ '' ]) as HttpRequest;
input.request = guardedStreamFrom([ 'data' ]) as HttpRequest;
input.request.headers = { 'content-length': '0' };
await expect(bodyParser.handle(input)).resolves.toBeUndefined();
const result = await bodyParser.handle(input);
await expect(arrayifyStream(result.data)).resolves.toEqual([]);
});
it('errors when a content length is specified without content type.', async(): Promise<void> => {
@@ -48,7 +50,7 @@ describe('A RawBodyparser', (): void => {
it('returns a Representation if there is empty data.', async(): Promise<void> => {
input.request = guardedStreamFrom([]) as HttpRequest;
input.request.headers = { 'content-length': '0', 'content-type': 'text/turtle' };
const result = (await bodyParser.handle(input))!;
const result = await bodyParser.handle(input);
expect(result).toEqual({
binary: true,
data: input.request,
@@ -60,7 +62,7 @@ describe('A RawBodyparser', (): void => {
it('returns a Representation if there is non-empty data.', async(): Promise<void> => {
input.request = guardedStreamFrom([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest;
input.request.headers = { 'transfer-encoding': 'chunked', 'content-type': 'text/turtle' };
const result = (await bodyParser.handle(input))!;
const result = await bodyParser.handle(input);
expect(result).toEqual({
binary: true,
data: input.request,

View File

@@ -1,5 +1,6 @@
import { DeleteOperationHandler } from '../../../../src/http/ldp/DeleteOperationHandler';
import type { Operation } from '../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { BasicConditions } from '../../../../src/storage/BasicConditions';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
@@ -7,10 +8,11 @@ import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplemen
describe('A DeleteOperationHandler', (): void => {
let operation: Operation;
const conditions = new BasicConditions({});
const body = new BasicRepresentation();
const store = {} as unknown as ResourceStore;
const handler = new DeleteOperationHandler(store);
beforeEach(async(): Promise<void> => {
operation = { method: 'DELETE', target: { path: 'http://test.com/foo' }, preferences: {}, conditions };
operation = { method: 'DELETE', target: { path: 'http://test.com/foo' }, preferences: {}, conditions, body };
store.deleteResource = jest.fn(async(): Promise<any> => undefined);
});

View File

@@ -1,5 +1,6 @@
import { GetOperationHandler } from '../../../../src/http/ldp/GetOperationHandler';
import type { Operation } from '../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import type { Representation } from '../../../../src/http/representation/Representation';
import { BasicConditions } from '../../../../src/storage/BasicConditions';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
@@ -9,11 +10,12 @@ describe('A GetOperationHandler', (): void => {
let operation: Operation;
const conditions = new BasicConditions({});
const preferences = {};
const body = new BasicRepresentation();
let store: ResourceStore;
let handler: GetOperationHandler;
beforeEach(async(): Promise<void> => {
operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, conditions };
operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
store = {
getRepresentation: jest.fn(async(): Promise<Representation> =>
({ binary: false, data: 'data', metadata: 'metadata' } as any)),

View File

@@ -1,6 +1,7 @@
import type { Readable } from 'stream';
import { HeadOperationHandler } from '../../../../src/http/ldp/HeadOperationHandler';
import type { Operation } from '../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import type { Representation } from '../../../../src/http/representation/Representation';
import { BasicConditions } from '../../../../src/storage/BasicConditions';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
@@ -10,12 +11,13 @@ describe('A HeadOperationHandler', (): void => {
let operation: Operation;
const conditions = new BasicConditions({});
const preferences = {};
const body = new BasicRepresentation();
let store: ResourceStore;
let handler: HeadOperationHandler;
let data: Readable;
beforeEach(async(): Promise<void> => {
operation = { method: 'HEAD', target: { path: 'http://test.com/foo' }, preferences, conditions };
operation = { method: 'HEAD', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
data = { destroy: jest.fn() } as any;
store = {
getRepresentation: jest.fn(async(): Promise<Representation> =>

View File

@@ -25,10 +25,8 @@ describe('A PatchOperationHandler', (): void => {
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
});
it('errors if there is no body or content-type.', async(): Promise<void> => {
operation.body!.metadata.contentType = undefined;
await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError);
delete operation.body;
it('errors if there is no content-type.', async(): Promise<void> => {
operation.body.metadata.contentType = undefined;
await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError);
});

View File

@@ -32,10 +32,8 @@ describe('A PostOperationHandler', (): void => {
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
});
it('errors if there is no body or content-type.', async(): Promise<void> => {
operation.body!.metadata.contentType = undefined;
await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError);
delete operation.body;
it('errors if there is no content-type.', async(): Promise<void> => {
operation.body.metadata.contentType = undefined;
await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError);
});

View File

@@ -26,10 +26,8 @@ describe('A PutOperationHandler', (): void => {
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
});
it('errors if there is no body or content-type.', async(): Promise<void> => {
operation.body!.metadata.contentType = undefined;
await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError);
delete operation.body;
it('errors if there is no content-type.', async(): Promise<void> => {
operation.body.metadata.contentType = undefined;
await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError);
});

View File

@@ -3,6 +3,7 @@ import { CredentialGroup } from '../../../../../src/authentication/Credentials';
import type { AclPermission } from '../../../../../src/authorization/permissions/AclPermission';
import { WebAclMetadataCollector } from '../../../../../src/http/ldp/metadata/WebAclMetadataCollector';
import type { Operation } from '../../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../../src/http/representation/BasicRepresentation';
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import { ACL, AUTH } from '../../../../../src/util/Vocabularies';
@@ -16,6 +17,7 @@ describe('A WebAclMetadataCollector', (): void => {
method: 'GET',
target: { path: 'http://test.com/foo' },
preferences: {},
body: new BasicRepresentation(),
};
metadata = new RepresentationMetadata();

View File

@@ -38,7 +38,12 @@ describe('An IdentityProviderHttpHandler', (): void => {
let handler: IdentityProviderHttpHandler;
beforeEach(async(): Promise<void> => {
operation = { method: 'GET', target: { path: 'http://test.com/idp' }, preferences: { type: { 'text/html': 1 }}};
operation = {
method: 'GET',
target: { path: 'http://test.com/idp' },
preferences: { type: { 'text/html': 1 }},
body: new BasicRepresentation(),
};
provider = {
callback: jest.fn(),

View File

@@ -43,6 +43,7 @@ describe('A SetupHttpHandler', (): void => {
method: 'GET',
target: { path: 'http://test.com/setup' },
preferences: { type: { 'text/html': 1 }},
body: new BasicRepresentation(),
};
errorHandler = { handleSafe: jest.fn(({ error }: ErrorHandlerArgs): ResponseDescription => ({

View File

@@ -5,6 +5,7 @@ import type { PermissionReader } from '../../../src/authorization/PermissionRead
import type { ModesExtractor } from '../../../src/authorization/permissions/ModesExtractor';
import { AccessMode } from '../../../src/authorization/permissions/Permissions';
import type { Operation } from '../../../src/http/Operation';
import { BasicRepresentation } from '../../../src/http/representation/BasicRepresentation';
import { AuthorizingHttpHandler } from '../../../src/server/AuthorizingHttpHandler';
import type { HttpRequest } from '../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../src/server/HttpResponse';
@@ -30,6 +31,7 @@ describe('An AuthorizingHttpHandler', (): void => {
target: { path: 'http://test.com/foo' },
method: 'GET',
preferences: {},
body: new BasicRepresentation(),
};
credentialsExtractor = {

View File

@@ -5,6 +5,7 @@ import type { ErrorHandler } from '../../../src/http/output/error/ErrorHandler';
import { OkResponseDescription } from '../../../src/http/output/response/OkResponseDescription';
import { ResponseDescription } from '../../../src/http/output/response/ResponseDescription';
import type { ResponseWriter } from '../../../src/http/output/ResponseWriter';
import { BasicRepresentation } from '../../../src/http/representation/BasicRepresentation';
import { RepresentationMetadata } from '../../../src/http/representation/RepresentationMetadata';
import type { HttpRequest } from '../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../src/server/HttpResponse';
@@ -15,7 +16,8 @@ describe('A ParsingHttpHandler', (): void => {
const request: HttpRequest = {} as any;
const response: HttpResponse = {} as any;
const preferences = { type: { 'text/html': 1 }};
const operation: Operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences };
const body = new BasicRepresentation();
const operation: Operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, body };
const errorResponse = new ResponseDescription(400);
let requestParser: jest.Mocked<RequestParser>;
let metadataCollector: jest.Mocked<OperationMetadataCollector>;

View File

@@ -164,6 +164,7 @@ describe('A DataAccessorBasedStore', (): void => {
metadata: new RepresentationMetadata(
{ [CONTENT_TYPE]: 'text/plain', [RDF.type]: DataFactory.namedNode(LDP.Resource) },
),
isEmpty: false,
};
});

View File

@@ -4,6 +4,7 @@ import arrayifyStream from 'arrayify-stream';
import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint';
import { DataFactory } from 'n3';
import type { Quad } from 'rdf-js';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { SparqlDataAccessor } from '../../../../src/storage/accessors/SparqlDataAccessor';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
@@ -69,11 +70,13 @@ describe('A SparqlDataAccessor', (): void => {
});
it('can only handle quad data.', async(): Promise<void> => {
await expect(accessor.canHandle({ binary: true, data, metadata })).rejects.toThrow(UnsupportedMediaTypeHttpError);
metadata.contentType = 'newInternalType';
await expect(accessor.canHandle({ binary: false, data, metadata })).rejects.toThrow(UnsupportedMediaTypeHttpError);
let representation = new BasicRepresentation(data, metadata, true);
await expect(accessor.canHandle(representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
representation = new BasicRepresentation(data, 'newInternalType', false);
await expect(accessor.canHandle(representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
representation = new BasicRepresentation(data, INTERNAL_QUADS, false);
metadata.contentType = INTERNAL_QUADS;
await expect(accessor.canHandle({ binary: false, data, metadata })).resolves.toBeUndefined();
await expect(accessor.canHandle(representation)).resolves.toBeUndefined();
});
it('returns the corresponding quads when data is requested.', async(): Promise<void> => {

View File

@@ -19,6 +19,7 @@ function getPatch(query: string): SparqlUpdatePatch {
data: guardedStreamFrom(prefixedQuery),
metadata: new RepresentationMetadata(),
binary: true,
isEmpty: false,
};
}

View File

@@ -26,7 +26,7 @@ describe('A ConvertingRouterRule', (): void => {
rule = new ConvertingRouterRule({ store: store1, supportChecker: checker1 }, defaultStore);
metadata = new RepresentationMetadata();
representation = { binary: true, data: 'data!' as any, metadata };
representation = { binary: true, data: 'data!' as any, metadata, isEmpty: false };
});
it('returns the corresponding store if it supports the input.', async(): Promise<void> => {