mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.62k stars 1.08k forks source link

Mustache template unable to access body.<field> #1911

Open juber-nunes opened 1 month ago

juber-nunes commented 1 month ago

Describe the issue Mustache template unable to access body.

What you are trying to do Description of what you are trying to do so we can understand the context of the problem

MockServer version 5.15.0

To Reproduce Steps to reproduce the issue:

  1. How you are running MockServer (i.e maven plugin, docker, etc)
  2. Code you used to create expectations

    mockServerClient("localhost", 1081) .mockAnyResponse({ "httpRequest": { "method": "POST", "path": "/mock-endpoint", "headers": { "Content-Type": ["application/json"] }, "body": { "type": "JSON", }

    }, "httpResponseTemplate": { "template": JSON.stringify({ "statusCode": 200, "headers": { "Content-Type": ["application/json"] }, "body": { "id": "{{ uuid }}",
    "time": "{{ now_iso_8601 }}",
    'message': "{{{jsonPath request.body '$.message'}}}", 'to': '{{{ request.body.to }}}', 'from': '1111111111111' }, 'delay': { 'timeUnit': 'MILLISECONDS', } }), "templateType": "MUSTACHE",

    },

  3. What error you saw

Expected behaviour To return field

MockServer Log 2024-10-02 17:03:32 5.15.0 INFO 1081 request:

{ "method" : "POST", "path" : "/mock-endpoint", "headers" : { "user-agent" : [ "PostmanRuntime/7.42.0" ], "postman-token" : [ "d858c7d9-d7b9-4ff0-bea9-255399791bcf" ], "host" : [ "172.22.118.120:1081" ], "content-type" : [ "application/json" ], "content-length" : [ "152" ], "connection" : [ "keep-alive" ], "accept-encoding" : [ "gzip, deflate, br" ], "accept" : [ "/" ] }, "keepAlive" : true, "secure" : false, "protocol" : "HTTP_1_1", "localAddress" : "127.0.0.1:1081", "remoteAddress" : "127.0.0.1:37752", "body" : { "message" : "Your message content", "to" : [ "+1234567890" ], "from" : "+13473450776", "media" : [ ], "priority" : "default", "tags" : { } } }

matched expectation:

{ "httpRequest" : { "method" : "POST", "path" : "/mock-endpoint", "headers" : { "Content-Type" : [ "application/json" ] } }, "httpResponseTemplate" : { "template" : "{\"statusCode\":200,\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":{\"id\":\"{{ uuid }}\",\"time\":\"{{ now_iso_8601 }}\",\"message\":\"{{{jsonPath request.body '$.message'}}}\",\"to\":\"{{{ request.body.to }}}\",\"from\":\"1111111111111\"},\"delay\":{\"timeUnit\":\"MILLISECONDS\"}}", "templateType" : "MUSTACHE" }, "id" : "788c5024-0027-426a-a36e-859a4691f073", "priority" : 0, "timeToLive" : { "unlimited" : true }, "times" : { "unlimited" : true } }

2024-10-02 17:03:32 5.15.0 INFO 1081 generated output:

{"statusCode":200,"headers":{"Content-Type":["application/json"]},"body":{"id":"07348bcf-c348-4e22-adb2-f138cc348257","time":"2024-10-02T16:03:32.868205485Z","message":"","to":"","from":"1111111111111"},"delay":{"timeUnit":"MILLISECONDS"}}

from template:

{"statusCode":200,"headers":{"Content-Type":["application/json"]},"body":{"id":"{{ uuid }}","time":"{{ now_iso_8601 }}","message":"{{{jsonPath request.body '$.message'}}}","to":"{{{ request.body.to }}}","from":"1111111111111"},"delay":{"timeUnit":"MILLISECONDS"}}

for request:

{ "method" : "POST", "path" : "/mock-endpoint", "headers" : { "user-agent" : [ "PostmanRuntime/7.42.0" ], "postman-token" : [ "d858c7d9-d7b9-4ff0-bea9-255399791bcf" ], "host" : [ "172.22.118.120:1081" ], "content-type" : [ "application/json" ], "content-length" : [ "152" ], "connection" : [ "keep-alive" ], "accept-encoding" : [ "gzip, deflate, br" ], "accept" : [ "/" ] }, "keepAlive" : true, "secure" : false, "protocol" : "HTTP_1_1", "localAddress" : "127.0.0.1:1081", "remoteAddress" : "127.0.0.1:37752", "body" : { "message" : "Your message content", "to" : [ "+1234567890" ], "from" : "+13473450776", "media" : [ ], "priority" : "default", "tags" : { } } }

2024-10-02 17:03:32 5.15.0 INFO 1081 returning response:

{ "statusCode" : 200, "headers" : { "Content-Type" : [ "application/json" ] }, "body" : { "id" : "07348bcf-c348-4e22-adb2-f138cc348257", "time" : "2024-10-02T16:03:32.868205485Z", "message" : "", "to" : "", "from" : "1111111111111" }, "delay" : { "timeUnit" : "MILLISECONDS", "value" : 0 } }

for request:

{ "method" : "POST", "path" : "/mock-endpoint", "headers" : { "user-agent" : [ "PostmanRuntime/7.42.0" ], "postman-token" : [ "d858c7d9-d7b9-4ff0-bea9-255399791bcf" ], "host" : [ "172.22.118.120:1081" ], "content-type" : [ "application/json" ], "content-length" : [ "152" ], "connection" : [ "keep-alive" ], "accept-encoding" : [ "gzip, deflate, br" ], "accept" : [ "/" ] }, "keepAlive" : true, "secure" : false, "protocol" : "HTTP_1_1", "localAddress" : "127.0.0.1:1081", "remoteAddress" : "127.0.0.1:37752", "body" : { "message" : "Your message content", "to" : [ "+1234567890" ], "from" : "+13473450776", "media" : [ ], "priority" : "default", "tags" : { } } }

for action:

{ "template" : "{\"statusCode\":200,\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":{\"id\":\"{{ uuid }}\",\"time\":\"{{ now_iso_8601 }}\",\"message\":\"{{{jsonPath request.body '$.message'}}}\",\"to\":\"{{{ request.body.to }}}\",\"from\":\"1111111111111\"},\"delay\":{\"timeUnit\":\"MILLISECONDS\"}}", "templateType" : "MUSTACHE" }

from expectation:

788c5024-0027-426a-a36e-859a4691f073