test(core): #httpRequestMetadataStorage
MODIFY
packages/core/src/http/server/http.server.metadata.storage.ts
packages/core/src/http/server/http.server.metadata.storage.ts
-import * as R from 'fp-ts/lib/Reader';
-import { pipe } from 'fp-ts/lib/pipeable';
-import { reader } from '../../context/context.factory';
import { HttpRequestMetadata } from '../http.interface';
+import { createReader } from '../../context/context.reader.factory';
-export type ServerRequestMetadataStorage = ReturnType<typeof serverRequestMetadataStorage>;
+export type HttpRequestMetadataStorage = ReturnType<typeof httpRequestMetadataStorage>;
-export const serverRequestMetadataStorage = pipe(reader, R.map(() => {
+export const httpRequestMetadataStorage = createReader(() => {
const storage = new Map<string, HttpRequestMetadata>();
const set = (key: string | undefined, metadata: HttpRequestMetadata | undefined) => {
- if (key && metadata) {
- storage.set(key, metadata);
- }
+ if (key && metadata) storage.set(key, metadata);
}
const get = (key: string) => {
@@ -20,8 +16,12 @@ export const serverRequestMetadataStorage = pipe(reader, R.map(() => {
return metadata;
}
+ const size = () =>
+ storage.size;
+
return {
set,
get,
+ size,
}
-}));
+});

MODIFY
packages/core/src/http/server/http.server.tokens.ts
packages/core/src/http/server/http.server.tokens.ts
@@ -3,10 +3,10 @@ import { createContextToken } from '../../context/context.token.factory';
import { HttpServer, HttpRequest } from '../http.interface';
import { Routing } from '../router/http.router.interface';
import { AllServerEvents } from './http.server.event';
-import { ServerRequestMetadataStorage } from './http.server.metadata.storage';
+import { HttpRequestMetadataStorage } from './http.server.metadata.storage';
export const HttpServerEventStreamToken = createContextToken<Observable<AllServerEvents>>('HttpServerEventStreamToken');
export const HttpServerClientToken = createContextToken<HttpServer>('HttpServerClientToken');
export const HttpRoutingToken = createContextToken<Routing>('HttpServerRoutingToken');
-export const HttpRequestMetadataStorageToken = createContextToken<ServerRequestMetadataStorage>('HttpServerRequestMetadataStorageToken');
+export const HttpRequestMetadataStorageToken = createContextToken<HttpRequestMetadataStorage>('HttpServerRequestMetadataStorageToken');
export const HttpRequestBusToken = createContextToken<Subject<HttpRequest>>('HttpRequestBusToken');

MODIFY
packages/core/src/http/server/http.server.ts
packages/core/src/http/server/http.server.ts
@@ -14,7 +14,7 @@ import { logContext } from '../../context/context.logger';
import { ServerIO } from '../../listener/listener.interface';
import { subscribeServerEvents } from './http.server.event.subscriber';
import { HttpServerClientToken, HttpServerEventStreamToken, HttpRequestMetadataStorageToken, HttpRequestBusToken } from './http.server.tokens';
-import { serverRequestMetadataStorage } from './http.server.metadata.storage';
+import { httpRequestMetadataStorage } from './http.server.metadata.storage';
import { CreateServerConfig } from './http.server.interface';
const DEFAULT_HOSTNAME = '127.0.0.1';
@@ -28,7 +28,7 @@ export const createServer = async (config: CreateServerConfig) => {
const boundLogger = bindTo(LoggerToken)(isTestEnv() ? mockLogger : logger);
const boundHttpServerEvent = bindTo(HttpServerEventStreamToken)(() => serverEvent$);
const boundHttpServerClient = bindTo(HttpServerClientToken)(() => server);
- const boundHttpRequestMetadataStorage = bindTo(HttpRequestMetadataStorageToken)(serverRequestMetadataStorage);
+ const boundHttpRequestMetadataStorage = bindTo(HttpRequestMetadataStorageToken)(httpRequestMetadataStorage);
const boundHttpRequestBus = bindTo(HttpRequestBusToken)(() => new Subject<HttpRequest>());
const context = await flow(

ADD
packages/core/src/http/server/specs/http.server.metadata.storage.spec.ts
+import { createContext } from '../../../context/context.factory';
+import { createUuid } from '../../../+internal/utils';
+import { HttpRequestMetadata } from '../../http.interface';
+import { httpRequestMetadataStorage } from '../http.server.metadata.storage';
+
+describe('#httpRequestMetadataStorage', () => {
+ const storage = httpRequestMetadataStorage(createContext());
+
+ test('sets => gets => removes value from storage', () => {
+ const id = createUuid();
+ const metadata: HttpRequestMetadata = { test: 'test_data' };
+
+ expect(storage.size()).toEqual(0);
+
+ storage.set(id, metadata);
+
+ expect(storage.size()).toEqual(1);
+
+ const retrievedData = storage.get(id);
+
+ expect(retrievedData).toEqual(metadata);
+ expect(storage.size()).toEqual(0);
+ });
+});

test(core): #httpRequestMetadataStorage