Skip to content

Commit

Permalink
fix: test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Jun 12, 2018
1 parent 9f599cb commit 7cb6d84
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
5 changes: 5 additions & 0 deletions packages/rest/src/coercion/rest-http-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
}
}
12 changes: 4 additions & 8 deletions packages/rest/src/coercion/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -67,6 +63,6 @@ export class Validator {
*/
// tslint:disable-next-line:no-any
isAbsent(value: any) {
return value === '';
return value === '' || value === undefined;
}
}
5 changes: 2 additions & 3 deletions packages/rest/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
18 changes: 18 additions & 0 deletions packages/rest/test/unit/coercion/invalid-spec.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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: <ParameterLocation>'unknown',
name: 'aparameter',
schema: {type: 'unknown'},
};

describe('throws error for invalid parameter spec', () => {
test(INVALID_PARAM, '', RestHttpError.invalidParamLocation('unknown'));
});
4 changes: 2 additions & 2 deletions packages/rest/test/unit/coercion/paramStringToInteger.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {ParameterLocation} from '@loopback/openapi-v3-types';
const INT32_PARAM = {
in: <ParameterLocation>'path',
name: 'aparameter',
schema: {type: 'number', format: 'int32'},
schema: {type: 'integer', format: 'int32'},
};

const INT64_PARAM = {
in: <ParameterLocation>'path',
name: 'aparameter',
schema: {type: 'number', format: 'int64'},
schema: {type: 'integer', format: 'int64'},
};

describe('coerce param from string to integer', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/test/unit/coercion/paramStringToNumber.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const FLOAT_PARAM = {
name: 'aparameter',
schema: {
type: 'number',
float: 'float',
format: 'float',
},
};

Expand All @@ -35,7 +35,7 @@ const DOUBLE_PARAM = {
name: 'aparameter',
schema: {
type: 'number',
float: 'double',
format: 'double',
},
};

Expand Down

0 comments on commit 7cb6d84

Please sign in to comment.