Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Merge branch 'fix/body-path-number' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume committed Feb 21, 2022
2 parents a57f54f + ad19d68 commit 6463bec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mockoon/commons-server",
"description": "Mockoon's commons server library. Used in Mockoon desktop application and CLI.",
"version": "2.16.2",
"version": "2.16.3",
"author": {
"name": "Guillaume Monnet",
"email": "hi@255kb.dev",
Expand Down
4 changes: 2 additions & 2 deletions src/libs/templating-helpers/request-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const RequestHelpers = function (
}

// if no path has been provided we want the full raw body as is
if (!path) {
if (path == null) {
return new SafeString(request.body);
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export const RequestHelpers = function (

if (request.parsedBody) {
// if no path has been provided we want the full raw body as is
if (!path) {
if (path == null) {
return request.parsedBody;
}

Expand Down
34 changes: 34 additions & 0 deletions test/suites/templating-helpers/request-helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { EOL } from 'os';
import { TemplateParser } from '../../../src/libs/template-parser';

const requestMock = {
Expand Down Expand Up @@ -113,6 +114,28 @@ describe('Template parser', () => {
'"This \\n is a \\"message\\" with quotes."'
);
});

it('should return the enumerated objects when body contains a root array and a number is passed as path', () => {
const parseResult = TemplateParser(
'{{#repeat 2}}{{body @index}}{{/repeat}}',
{
parsedBody: [
{
id: 1,
name: 'John'
},
{
id: 2,
name: 'Doe'
}
]
} as any,
{} as any
);
expect(parseResult).to.be.equal(
`{"id":1,"name":"John"},${EOL}{"id":2,"name":"Doe"}${EOL}`
);
});
});

describe('Helper: bodyRaw', () => {
Expand Down Expand Up @@ -207,6 +230,17 @@ describe('Template parser', () => {
);
expect(parseResult).to.be.equal('string1string2');
});

it('should return the enumerated strings when body contains a root array and a number is passed as path', () => {
const parseResult = TemplateParser(
'{{#repeat 2}}{{bodyRaw @index}}{{/repeat}}',
{
parsedBody: ['string1', 'string2']
} as any,
{} as any
);
expect(parseResult).to.be.equal(`string1,${EOL}string2${EOL}`);
});
});

describe('Helper: queryParam', () => {
Expand Down

0 comments on commit 6463bec

Please sign in to comment.