From 7cb6d8493668b6f2a5f85fb2db909914ce22d21d Mon Sep 17 00:00:00 2001 From: jannyHou Date: Tue, 12 Jun 2018 11:53:00 -0400 Subject: [PATCH] fix: test coverage --- packages/rest/src/coercion/rest-http-error.ts | 5 +++++ packages/rest/src/coercion/validator.ts | 12 ++++-------- packages/rest/src/parser.ts | 5 ++--- .../unit/coercion/invalid-spec.unit.test.ts | 18 ++++++++++++++++++ .../unit/coercion/paramStringToInteger.unit.ts | 4 ++-- .../unit/coercion/paramStringToNumber.unit.ts | 4 ++-- 6 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 packages/rest/test/unit/coercion/invalid-spec.unit.test.ts diff --git a/packages/rest/src/coercion/rest-http-error.ts b/packages/rest/src/coercion/rest-http-error.ts index 211f67f0f8fb..4b2fe34a89f6 100644 --- a/packages/rest/src/coercion/rest-http-error.ts +++ b/packages/rest/src/coercion/rest-http-error.ts @@ -8,4 +8,9 @@ export namespace RestHttpError { const msg = `Required parameter ${name} is missing!`; return new HttpErrors.BadRequest(msg); } + export function invalidParamLocation(location: string): HttpErrors.HttpError { + return new HttpErrors.NotImplemented( + 'Parameters with "in: ' + location + '" are not supported yet.', + ); + } } diff --git a/packages/rest/src/coercion/validator.ts b/packages/rest/src/coercion/validator.ts index 85c62d57f5e5..50fb8926728c 100644 --- a/packages/rest/src/coercion/validator.ts +++ b/packages/rest/src/coercion/validator.ts @@ -39,13 +39,9 @@ export class Validator { value: string | object | undefined, opts?: ValidationOptions, ) { - if (this.isAbsent(value)) { - if (this.isRequired(opts)) { - const name = this.ctx.parameterSpec.name; - throw RestHttpError.missingRequired(name); - } else { - return; - } + if (this.isAbsent(value) && this.isRequired(opts)) { + const name = this.ctx.parameterSpec.name; + throw RestHttpError.missingRequired(name); } } @@ -67,6 +63,6 @@ export class Validator { */ // tslint:disable-next-line:no-any isAbsent(value: any) { - return value === ''; + return value === '' || value === undefined; } } diff --git a/packages/rest/src/parser.ts b/packages/rest/src/parser.ts index d9b516c1ff0c..76eb338ea4ea 100644 --- a/packages/rest/src/parser.ts +++ b/packages/rest/src/parser.ts @@ -15,6 +15,7 @@ import {promisify} from 'util'; import {OperationArgs, Request, PathParameterValues} from './types'; import {ResolvedRoute} from './router/routing-table'; import {coerceParameter} from './coercion/coerce-parameter'; +import {RestHttpError} from './index'; type HttpError = HttpErrors.HttpError; // tslint:disable-next-line:no-any @@ -130,9 +131,7 @@ function getParamFromRequest( // TODO(jannyhou) to support `cookie`, // see issue https://github.com/strongloop/loopback-next/issues/997 default: - throw new HttpErrors.NotImplemented( - 'Parameters with "in: ' + spec.in + '" are not supported yet.', - ); + throw RestHttpError.invalidParamLocation(spec.in); } return result; } diff --git a/packages/rest/test/unit/coercion/invalid-spec.unit.test.ts b/packages/rest/test/unit/coercion/invalid-spec.unit.test.ts new file mode 100644 index 000000000000..4f29d0c16d24 --- /dev/null +++ b/packages/rest/test/unit/coercion/invalid-spec.unit.test.ts @@ -0,0 +1,18 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/rest +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {test} from './utils'; +import {RestHttpError} from '../../../'; +import {ParameterLocation} from '@loopback/openapi-v3-types'; + +const INVALID_PARAM = { + in: 'unknown', + name: 'aparameter', + schema: {type: 'unknown'}, +}; + +describe('throws error for invalid parameter spec', () => { + test(INVALID_PARAM, '', RestHttpError.invalidParamLocation('unknown')); +}); diff --git a/packages/rest/test/unit/coercion/paramStringToInteger.unit.ts b/packages/rest/test/unit/coercion/paramStringToInteger.unit.ts index 1eb692f824f7..11be18b16ef6 100644 --- a/packages/rest/test/unit/coercion/paramStringToInteger.unit.ts +++ b/packages/rest/test/unit/coercion/paramStringToInteger.unit.ts @@ -9,13 +9,13 @@ import {ParameterLocation} from '@loopback/openapi-v3-types'; const INT32_PARAM = { in: 'path', name: 'aparameter', - schema: {type: 'number', format: 'int32'}, + schema: {type: 'integer', format: 'int32'}, }; const INT64_PARAM = { in: 'path', name: 'aparameter', - schema: {type: 'number', format: 'int64'}, + schema: {type: 'integer', format: 'int64'}, }; describe('coerce param from string to integer', () => { diff --git a/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts b/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts index 8262cd5afef4..522b05ea1c8c 100644 --- a/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts +++ b/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts @@ -26,7 +26,7 @@ const FLOAT_PARAM = { name: 'aparameter', schema: { type: 'number', - float: 'float', + format: 'float', }, }; @@ -35,7 +35,7 @@ const DOUBLE_PARAM = { name: 'aparameter', schema: { type: 'number', - float: 'double', + format: 'double', }, };