Skip to content

Commit

Permalink
fix(package): In $input.path, if payload is empty, treat it as empty …
Browse files Browse the repository at this point in the history
…object
  • Loading branch information
ToQoz committed Dec 18, 2015
1 parent 0bcb8e2 commit 47cbd78
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ module.exports = function(parameters) {
_payload: payload.toString(),
path: function(path) {
var obj;
// if payload starts with `{` or `[` or `"`, treat as JSON
if (/^\s*(?:{|\[|")/.test(this._payload)) {
if (this._payload === '') {
// if payload is empty, treat it as empty object
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-91575d0e
obj = {};
} else if (/^\s*(?:{|\[|")/.test(this._payload)) {
// if payload starts with `{` or `[` or `"`, treat as JSON
obj = JSON.parse(this._payload);
} else {
// treat as string
Expand Down
5 changes: 5 additions & 0 deletions misc/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ module.exports = [
payload: "",
headers: {},
},
{
template: '$input.path(\'$\')',
payload: "",
headers: {},
},
{
template: '{"name": "$input.path(\'$\')"}',
payload: "name=toqoz",
Expand Down
8 changes: 8 additions & 0 deletions test/_.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('$input.path|$input.json', function() {
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-91575d0e
describe('H=`{}` P=`` ===> T=`$input.path(\'$\')`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate({template: "$input.path('$')", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-25c6993c
describe('H=`{}` P=`name=toqoz` ===> T=`{"name": "$input.path(\'$\')"}`', function() {
it('return name=toqoz', function() {
Expand Down
5 changes: 5 additions & 0 deletions test/_.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
`$input.json('$')`|`None`|``|`200`|`{}`

## example-91575d0e
Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
`$input.path('$')`|`None`|``|`200`|`{}`

## example-25c6993c
Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
Expand Down

0 comments on commit 47cbd78

Please sign in to comment.