// Copyright IBM Corp. and LoopBack contributors 2019. 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 {Context} from '@loopback/core'; import { OperationObject, ParameterObject, RequestBodyObject, SchemaObject, } from '@loopback/openapi-v3'; import { ShotRequestOptions, expect, stubExpressContext, } from '@loopback/testlab'; import { JsonBodyParser, PathParameterValues, RawBodyParser, Request, RequestBodyParser, RequestBodyParserOptions, RestHttpErrors, Route, StreamBodyParser, TextBodyParser, UrlEncodedBodyParser, createResolvedRoute, parseOperationArgs, } from '../..'; describe('operationArgsParser', () => { let requestBodyParser: RequestBodyParser; before(givenRequestBodyParser); it('parses path parameters', async () => { const req = givenRequest(); const spec = givenOperationWithParameters([ { name: 'id', type: 'number', in: 'path', required: true, }, ]); const route = givenResolvedRoute(spec, {id: 1}); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([1]); }); it('parses body parameter', async () => { const req = givenRequest({ url: '/', payload: {key: 'value'}, }); const spec = givenOperationWithRequestBody({ description: 'data', content: {'application/json': {schema: {type: 'object'}}}, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([{key: 'value'}]); }); it('parses nullable body parameter', async () => { const req = givenRequest({ url: '/', payload: {key: null}, }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'application/json': { schema: { type: 'object', properties: { key: { type: 'string', nullable: true, }, }, }, }, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([{key: null}]); }); it('parses body parameter for urlencoded', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, payload: 'key=value', }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'application/x-www-form-urlencoded': {schema: {type: 'object'}}, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([{key: 'value'}]); }); it('parses body parameter for urlencoded with simple types', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, payload: 'key1=value&key2=1&key3=true', }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'application/x-www-form-urlencoded': { schema: { type: 'object', properties: { key1: {type: 'string'}, key2: {type: 'number'}, key3: {type: 'boolean'}, }, }, }, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([{key1: 'value', key2: 1, key3: true}]); }); it('parses body parameter for urlencoded with number[] types', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, payload: 'key=1&key=2', }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'application/x-www-form-urlencoded': { schema: { type: 'object', properties: { key: {type: 'array', items: {type: 'number'}}, }, }, }, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([{key: [1, 2]}]); }); it('parses body parameter for urlencoded with complex types', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, payload: 'name=IBM%20HQ&location[lat]=0.741895&location[lng]=-73.989308&tags[0]=IT&tags[1]=NY', }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'application/x-www-form-urlencoded': { schema: { type: 'object', properties: { name: {type: 'string'}, location: { type: 'object', properties: { lat: {type: 'number'}, lng: {type: 'number'}, }, }, tags: { type: 'array', items: {type: 'string'}, }, }, }, }, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([ { name: 'IBM HQ', location: {lat: 0.741895, lng: -73.989308}, tags: ['IT', 'NY'], }, ]); }); it('parses body parameter for urlencoded with string[] types', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, payload: 'key1=value1&key1=value2', }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'application/x-www-form-urlencoded': { schema: { type: 'object', properties: { key1: {type: 'array', items: {type: 'string'}}, }, }, }, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql([{key1: ['value1', 'value2']}]); }); it('parses body parameter for text data', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'text/plain', }, payload: 'plain-text', }); const spec = givenOperationWithRequestBody({ description: 'data', content: { 'text/plain': {schema: {type: 'string'}}, }, }); const route = givenResolvedRoute(spec); const args = await parseOperationArgs(req, route, requestBodyParser); expect(args).to.eql(['plain-text']); }); it('parses body parameter for html data', async () => { const req = givenRequest({ url: '/', headers: { 'Content-Type': 'text/html', }, payload: '