Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement HEAD request support
* 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
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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([]); | ||
}); | ||
}); |