test(core): internal utils
MODIFY
packages/core/src/+internal/testing/http.helper.ts
packages/core/src/+internal/testing/http.helper.ts
@@ -80,10 +80,10 @@ export const createMockEffectContext = () => {
};
export const createTestRoute = (opts?: { throwError?: boolean; delay?: number; method?: HttpMethod }) => {
- const routeMethod = opts?.method ?? 'GET';
+ const method = opts?.method ?? 'GET';
const routeDelay = opts?.delay ?? 0;
- const req = createHttpRequest(({ url: `/delay_${routeDelay}`, method: 'GET' }));
+ const req = createHttpRequest(({ url: `/delay_${routeDelay}`, method }));
const path = factorizeRegExpWithParams(`/delay_${routeDelay}`);
const effect: HttpEffect = req$ =>
@@ -96,7 +96,7 @@ export const createTestRoute = (opts?: { throwError?: boolean; delay?: number; m
const item: RoutingItem = {
regExp: path.regExp,
path: path.path,
- methods: { [routeMethod]: { effect, middlewares: [] } },
+ methods: { [method]: { effect, middlewares: [] } },
};
return { req, path, effect, item };

MODIFY
packages/core/src/+internal/testing/metadata.helper.ts
packages/core/src/+internal/testing/metadata.helper.ts
import { JSONSchema7 } from 'json-schema';
-import { getHeaderValue } from '../utils/http.util';
+import { getHeaderValueHead } from '../utils/http.util';
export interface TestingMetadata {
path?: string;
@@ -13,4 +13,4 @@ export const TESTING_REQUEST_ID_HEADER = 'X-Testing-Request-Id';
export const isTestingMetadataOn = () => process.env.MARBLE_TESTING_METADATA_ON === 'true';
-export const getTestingRequestIdHeader = getHeaderValue(TESTING_REQUEST_ID_HEADER);
+export const getTestingRequestIdHeader = getHeaderValueHead(TESTING_REQUEST_ID_HEADER);

MODIFY
packages/core/src/+internal/utils/error.util.ts
packages/core/src/+internal/utils/error.util.ts
@@ -5,4 +5,4 @@ export class ExtendableError extends Error {
}
export const isError = (data: any): data is Error =>
- !!data.stack && !!data.name;
+ !!data?.stack && !!data?.name;

MODIFY
packages/core/src/+internal/utils/http.util.ts
packages/core/src/+internal/utils/http.util.ts
+import { pipe } from 'fp-ts/lib/pipeable';
+import { toUndefined } from 'fp-ts/lib/Option';
import { HttpRequest } from '../../http/http.interface';
+import { getHead } from './array.util';
-export const getHeaderValue = <T extends string = string>(key: string) => (req: HttpRequest): T | undefined => {
+export const getHeaderValueHead = <T extends string = string>(key: string) => (req: HttpRequest): T | undefined => {
const header = req.headers[key.toLowerCase()];
return Array.isArray(header)
- ? header[0] as T
+ ? pipe(getHead(header), toUndefined) as T
: header as T;
};

MODIFY
packages/core/src/+internal/utils/spec/any.util.spec.ts
packages/core/src/+internal/utils/spec/any.util.spec.ts
-import { isNonNullable, isNullable } from '../any.util';
+import { isNonNullable, isNullable, getPortEnv } from '../any.util';
test('#isNonNullable', () => {
expect(isNonNullable({})).toBe(true);
@@ -19,3 +19,8 @@ test('#isNullable', () => {
expect(isNullable(null)).toBe(true);
expect(isNullable(undefined)).toBe(true);
});
+
+test('#getPortEnv', () => {
+ process.env.PORT = '8000';
+ expect(getPortEnv()).toEqual(8000);
+});

MODIFY
packages/core/src/+internal/utils/spec/array.util.spec.ts
packages/core/src/+internal/utils/spec/array.util.spec.ts
import { getOrElse } from 'fp-ts/lib/Option';
import { pipe } from 'fp-ts/lib/pipeable';
-import { getArrayFromEnum, getHead, getLast, filterArray, mapArray } from '../array.util';
+import { getArrayFromEnum, getHead, getLast, filterArray, mapArray, insertIf, insertIfElse } from '../array.util';
describe('Array util', () => {
test('#getArrayFromEnum returns array from simple enumerable', () => {
@@ -84,4 +84,14 @@ describe('Array util', () => {
// then
expect(result).toEqual([1, 2, 3, 4, 5]);
});
+
+ test('#insertIf inserts element conditionally', () => {
+ expect([ 1, ...insertIf(1 === 1)(2), 3]).toEqual([ 1, 2, 3 ]);
+ expect([ 1, ...insertIf(1 !== 1)(2), 3]).toEqual([ 1, 3 ]);
+ });
+
+ test('#insertIfElse inserts element conditionally or sets default', () => {
+ expect([ 1, ...insertIfElse(1 === 1)(2)(0), 3]).toEqual([ 1, 2, 3 ]);
+ expect([ 1, ...insertIfElse(1 !== 1)(2)(0), 3]).toEqual([ 1, 0, 3 ]);
+ });
});

ADD
packages/core/src/+internal/utils/spec/error.util.spec.ts
+import { isError } from '../error.util';
+
+describe('#isError', () => {
+ const cases: [any, boolean][] = [
+ [{}, false],
+ [new (class Foo { stack = [] }), false],
+ [new Error(), true],
+ [new Error('test'), true],
+ ];
+
+ test.each(cases)('given %p as argument, returns %p', (data, expected) =>
+ expect(isError(data)).toEqual(expected));
+});

ADD
packages/core/src/+internal/utils/spec/http.util.spec.ts
+import { createHttpRequest } from '../../testing';
+import { getHeaderValueHead } from '../http.util';
+
+test('#getHeaderValueHead', () => {
+ const headers = {
+ 'x-test-1': 'a',
+ 'x-test-2': ['b', 'c'],
+ }
+ const req = createHttpRequest({ headers });
+
+ expect(getHeaderValueHead('x-test-1')(req)).toEqual('a');
+ expect(getHeaderValueHead('x-test-2')(req)).toEqual('b');
+ expect(getHeaderValueHead('x-test-3')(req)).toBeUndefined();
+});

test(core): internal utils