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.61k stars 1.08k forks source link

Add $import tool for Velocity templates #1493

Closed john-mcpeek closed 2 years ago

john-mcpeek commented 2 years ago

Describe the feature request Add $import tool for Velocity templates,

What you are trying to do By using $import.read() in json expectations (with Velocity templates) I could keep my response body contents in files. I recognize I can write Java to do this and I do, but this would greatly simplify what I have to do for that same result.

The solution you'd like Add org.apache.velocity.tools.view.tools.ImportTool in VelocityTemplateEngine in the static section under ToolManager.

Describe alternatives you've considered Any way I can use json expectations and pull the response body from files instead of in-lining the contents. I need to respond with XML and everything is difficult to maintain after it is escaped. Files tell the story better and people can understand them right out of the gate.

jamesdbloom commented 2 years ago

merged PR with fix so closing this issue, thanks @bhmohanr-techie

john-mcpeek commented 1 year ago

Hey James, I created this expectation and it doesn't seem to interpret the $import. I just get $import.read(‘/config/blah-blah.xml') as the body. Any idea what is wrong?

{
  "httpRequest": {
    "path": "/simpleSecond"
  },
  "httpResponseTemplate": {
    "template": "{ \"statusCode\": 200, \"body\": \"$import.read(‘/config/blah-blah.xml')\"}",
    "templateType": "VELOCITY"
  }
}

Thanks,

John

vivmat08 commented 1 year ago

Hey James, I've been having the same problem as john-mcpeek. Additionally, I've run into another problem too. I'd like the mockserver response to have the same path as the request, which means the the URL parameter in $import.read needs to be dynamic. But since we have to use single quotes, it's becoming a problem. curl -v -X PUT "http://localhost:8081/mockserver/expectation" -d '{ "httpRequest": { "queryStringParameters": { "format":"json" } }, "httpResponseTemplate": { "template": "{\"body\":\"$import.read('file\:///home/maitreya/mockserver${request.path}.json')\"}", "templateType":"VELOCITY" } }' This is the command line code I've written, but this just throws a bash error, and we can't use double quotes coz it messes up with the double quotes at the start. I've tried the same thing in JAVA too, but ran into the substitution in single quotes and the above problem. Can you provide some help? Thanks, Vivan