Skip to content

Commit

Permalink
feat: Implement HEAD request support
Browse files Browse the repository at this point in the history
* feat: Implement HEAD request support

* feat: Integrate HEAD handler with other code

* fix: Improve test by using arrayifyStream

* fix: Use Promise chaining

* refactor: Unwrap destroy stream promise
  • Loading branch information
smessie committed Oct 6, 2020
1 parent 4d34cdd commit 0644f8d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/presets/ldp/operation-handler.json
Expand Up @@ -17,6 +17,12 @@
"@id": "urn:solid-server:default:ResourceStore_Patching"
}
},
{
"@type": "HeadOperationHandler",
"HeadOperationHandler:_store": {
"@id": "urn:solid-server:default:ResourceStore_Patching"
}
},
{
"@type": "PatchOperationHandler",
"PatchOperationHandler:_store": {
Expand Down
1 change: 1 addition & 0 deletions index.ts
Expand Up @@ -42,6 +42,7 @@ export * from './src/logging/WinstonLoggerFactory';
// LDP/Operations
export * from './src/ldp/operations/DeleteOperationHandler';
export * from './src/ldp/operations/GetOperationHandler';
export * from './src/ldp/operations/HeadOperationHandler';
export * from './src/ldp/operations/Operation';
export * from './src/ldp/operations/OperationHandler';
export * from './src/ldp/operations/PatchOperationHandler';
Expand Down
37 changes: 37 additions & 0 deletions src/ldp/operations/HeadOperationHandler.ts
@@ -0,0 +1,37 @@
import { Readable } from 'stream';
import type { ResourceStore } from '../../storage/ResourceStore';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import type { Operation } from './Operation';
import { OperationHandler } from './OperationHandler';
import type { ResponseDescription } from './ResponseDescription';

/**
* Handles HEAD {@link Operation}s.
* Calls the getRepresentation function from a {@link ResourceStore}.
*/
export class HeadOperationHandler extends OperationHandler {
private readonly store: ResourceStore;

public constructor(store: ResourceStore) {
super();
this.store = store;
}

public async canHandle(input: Operation): Promise<void> {
if (input.method !== 'HEAD') {
throw new UnsupportedHttpError('This handler only supports HEAD operations.');
}
}

public async handle(input: Operation): Promise<ResponseDescription> {
const body = await this.store.getRepresentation(input.target, input.preferences);

// Close the Readable as we will not return it.
body.data.destroy();
body.data = new Readable();
body.data._read = function(): void {
body.data.push(null);
};
return { identifier: input.target, body };
}
}
2 changes: 2 additions & 0 deletions test/configs/Util.ts
Expand Up @@ -14,6 +14,7 @@ import {
DeleteOperationHandler,
FileResourceStore,
GetOperationHandler,
HeadOperationHandler,
InMemoryResourceStore,
InteractionController,
MetadataController,
Expand Down Expand Up @@ -94,6 +95,7 @@ export const getPatchingStore = (store: ResourceStore): PatchingStore => {
export const getOperationHandler = (store: ResourceStore): CompositeAsyncHandler<Operation, ResponseDescription> => {
const handlers = [
new GetOperationHandler(store),
new HeadOperationHandler(store),
new PostOperationHandler(store),
new PutOperationHandler(store),
new PatchOperationHandler(store),
Expand Down
28 changes: 28 additions & 0 deletions test/unit/ldp/operations/HeadOperationHandler.test.ts
@@ -0,0 +1,28 @@
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { HeadOperationHandler } from '../../../../src/ldp/operations/HeadOperationHandler';
import type { Operation } from '../../../../src/ldp/operations/Operation';
import type { Representation } from '../../../../src/ldp/representation/Representation';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';

describe('A HeadOperationHandler', (): void => {
const store = {
getRepresentation: async(): Promise<Representation> => ({ binary: false, data: streamifyArray([ 1, 2, 3 ]) } as
Representation),
} as unknown as ResourceStore;
const handler = new HeadOperationHandler(store);

it('only supports HEAD operations.', async(): Promise<void> => {
await expect(handler.canHandle({ method: 'HEAD' } as Operation)).resolves.toBeUndefined();
await expect(handler.canHandle({ method: 'GET' } as Operation)).rejects.toThrow(UnsupportedHttpError);
await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
});

it('returns the representation from the store with the input identifier and empty data.', async(): Promise<void> => {
const result = await handler.handle({ target: { path: 'url' }} as Operation);
expect(result.identifier.path).toBe('url');
expect(result.body?.binary).toBe(false);
await expect(arrayifyStream(result.body!.data)).resolves.toEqual([]);
});
});

0 comments on commit 0644f8d

Please sign in to comment.