MCPcopy
hub / github.com/loopbackio/loopback-next / testCoercion

Function testCoercion

packages/rest/src/__tests__/unit/coercion/utils.ts:58–107  ·  view source on GitHub ↗
(config: TestArgs<T>)

Source from the content-addressed store, hash-verified

56};
57
58export async function testCoercion<T>(config: TestArgs<T>) {
59 /* istanbul ignore next */
60 try {
61 const pathParams: PathParameterValues = {};
62 let url = '/';
63 const spec = givenOperationWithParameters([config.paramSpec]);
64 const route = givenResolvedRoute(spec, pathParams);
65
66 switch (config.paramSpec.in) {
67 case 'path':
68 pathParams.aparameter = config.rawValue;
69 break;
70 case 'query':
71 {
72 const q = qs.stringify(
73 {aparameter: config.rawValue},
74 {encodeValuesOnly: true},
75 );
76 url += `?${q}`;
77 }
78 break;
79 case 'header':
80 case 'cookie':
81 throw new Error(
82 `testCoercion does not yet support in:${config.paramSpec.in}`,
83 );
84 default:
85 // An invalid param spec. Pass it through as an empty request
86 // to allow the tests to verify how invalid param spec is handled
87 break;
88 }
89
90 // Create the request after url is fully populated so that request.query
91 // is parsed
92 const req = givenRequest({url});
93
94 const requestBodyParser = new RequestBodyParser();
95 if (config.expectError) {
96 await expect(
97 parseOperationArgs(req, route, requestBodyParser),
98 ).to.be.rejectedWith(config.expectedResult as string | RegExp);
99 } else {
100 const args = await parseOperationArgs(req, route, requestBodyParser);
101 expect(args).to.eql([config.expectedResult]);
102 }
103 } catch (err) {
104 err.stack += config.caller;
105 throw err;
106 }
107}
108
109export function test<T>(
110 paramSpec: ParameterObject,

Callers 1

testFunction · 0.85

Calls 6

parseOperationArgsFunction · 0.85
rejectedWithMethod · 0.80
eqlMethod · 0.80
givenResolvedRouteFunction · 0.70
givenRequestFunction · 0.70

Tested by

no test coverage detected