Inject JSON file as a body of a response

2,004 views
Skip to first unread message

sotoi...@gmail.com

unread,
Dec 29, 2016, 1:09:52 PM12/29/16
to mountebank-discuss
Sup guys!

Need some help here can't figure out how to read a JSON file which container the body of a response for any request to my impostor.

This is what I got so far:


```
{
"protocol": "http",
"port": 4542,
"requests": [],
"name": "settings",
"stubs": [
{
"responses": [
{
"inject": "<%- stringify(filename, 'services/settings/response.ejs') %>"
}
]
}
]
}
```

So far so good, and this is how my `response.ejs` file looks like:

```
function (request, state, logger) {
return {
headers: {
'Content-Type': 'application/json'
},
body: ?
};
}
```

The `body: ?` it's the bit I can't figure out I have try to render the file using `ejs` I tried using `fs` module but it's not accessible from there my JSON body response it's on a file on the same path than my `reponse.ejs` `services/settings/body.json`

Any ideas? I just run out of options lol :(

Thanks,

Israel Sotomayor Azcuna

unread,
Dec 31, 2016, 9:37:49 AM12/31/16
to mountebank-discuss
Any one? I'm running out of ideas I have been trying something like:

```
function (request, state, logger) {
  var fs = require('fs')

  return {
    headers: {
      'Content-Type': 'application/json'
    },
    body: fs.readFileSync(services/settings/body.json)
  };
}
```

However I don't have access to `fs` on that context neither I can require it :/

Thanks,

Brandon Byars

unread,
Dec 31, 2016, 10:47:17 AM12/31/16
to Israel Sotomayor Azcuna, mountebank-discuss
Hi there,
Sorry for the slow response. I starred it and forgot to circle back to it. I'll replay my understanding of the problem first before answering to make sure I'm answering the right question. Please correct me if I'm not.

You have an EJS templated config file that pulls in your inject function from response.ejs, and you want response.ejs to also pull in a templated file for the body. RIght?

If so, I think you can simply use either a straight EJS include, or use the mountebank stringify. e.g.

function (request, state, logger) {
  return {
    headers: {
      'Content-Type': 'application/json'
    },
    body: '<% include body.ejs %>'
  };
}

or to keep it on one line

function (request, state, logger) {
  return {
    headers: {
      'Content-Type': 'application/json'
    },
    body: '<%- stringify(filename, 'body.ejs') %>'
  };
}

Do neither of those work for you?
-Brandon


--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Israel Sotomayor Azcuna

unread,
Jan 1, 2017, 7:32:07 AM1/1/17
to mountebank-discuss, sotoi...@gmail.com
Hi Brandon! Thanks for answer me.

I tried both of your ideas and neither of the works :/ I did first the mountbank stringfy like:

```
function (request, state, logger) {
  return {
    headers: {
      'Content-Type': 'application/json'
    },
    body: '<%- stringify(filename, 'services/settings/body.ejs') %>'
  };
}
```

And I get the following error:

```
HTTP/1.1 500 Internal Server Error
Connection: keep-alive
Date: Sun, 01 Jan 2017 12:16:42 GMT
Transfer-Encoding: chunked
content-type: application/json

{
    "errors": [
        {
            "code": "invalid injection",
            "data": "Unexpected identifier",
            "message": "invalid response injection",
            "name": "Error",
            "source": "(function (request, state, logger) {\n  return {\n    headers: {\n      'Content-Type': 'application/json'\n    },\n    body: '<%- stringify(filename, 'services/settings/body.ejs') %>'\n  };\n}\n)(scope, injectState, logger, deferred.resolve);",
            "stack": "Error\n    at Object.from (/usr/lib/node_modules/mountebank/src/util/inherit.js:15:17)\n    at Object.InjectionError (/usr/lib/node_modules/mountebank/src/util/errors.js:13:30)\n    at inject (/usr/lib/node_modules/mountebank/src/models/responseResolver.js:45:40)\n    at processResponse (/usr/lib/node_modules/mountebank/src/models/responseResolver.js:164:20)\n    at Object.resolve (/usr/lib/node_modules/mountebank/src/models/responseResolver.js:191:16)\n    at Object.resolve (/usr/lib/node_modules/mountebank/src/models/stubRepository.js:77:18)\n    at EventEmitter.respond (/usr/lib/node_modules/mountebank/src/models/http/baseHttpServer.js:84:34)\n    at /usr/lib/node_modules/mountebank/src/models/abstractServer.js:96:35\n    at _fulfilled (/usr/lib/node_modules/mountebank/node_modules/q/q.js:834:54)\n    at self.promiseDispatch.done (/usr/lib/node_modules/mountebank/node_modules/q/q.js:863:30)"
        }
    ]
}
```

And using the EJS template I just get the same string as the body like:

```
function (request, state, logger) {
  return {
    headers: {
      'Content-Type': 'application/json'
    },
    body: '<% include body.ejs %>'
  };
}
```

```
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Date: Sun, 01 Jan 2017 12:05:56 GMT
Transfer-Encoding: chunked

<% include body.ejs %>

```

Am I missing something? I think that function on my `response.ejs` file it's just not being processed as an ejs template and then it's not even trying to inject it

> Note: I had body.json but just change it to body.ejs to follow your example

For the sake of clarify I'm adding this link to a gist with the files I'm using just in case you wanna check what exactly I'm trying to achieve https://gist.github.com/zot24/3cf6df65c8ae3e63c2deabc4ed7174b9

Thanks for your help Happy new year! ;)

Israel Sotomayor Azcuna

unread,
Jan 1, 2017, 8:56:35 AM1/1/17
to mountebank-discuss
Ok I have done some test and figure out that there was a problem with my JSON not escaping new lines characters  and double quotes so I manage do some progress but still not working as I expect.

Just for testing purposes I added manually the JSON body into the `response.ejs` function like:

```
function (request, state, logger) {
  return {
    headers: {
      'Content-Type': 'application/json'
    },
    body: '{\"data\":{\"type\":\"settings\",\"page_length\":100,\"email_smtp_port\":25,\"email_encryption\":\"none\",\"timezone\":\"UTC\",\"password_policy\":\"min_6_letters\"}}'
  };
}
```

That make it work however if I add that "JSON" with all those scape characters into a data.ejs file and try to inject/include/stringify them it doesn't work and fail with the same error I mentioned before `invalid injection`

On Thursday, 29 December 2016 18:09:52 UTC, Israel Sotomayor Azcuna wrote:

Brandon Byars

unread,
Jan 1, 2017, 2:47:34 PM1/1/17
to Israel Sotomayor Azcuna, mountebank-discuss
Thanks for the gist; that definitely helped troubleshoot the problem. This is definitely a bug in mountebank in that it's not supporting nested stringify functions. I just committed a fix and will release it next week.
-Brandon

--

Israel Sotomayor Azcuna

unread,
Jan 1, 2017, 3:04:08 PM1/1/17
to mountebank-discuss, sotoi...@gmail.com
Nice to hear about it! I was getting crazy :P

I ended with a temporary solution with kind of satisfy my needs at least for now just to let others know I'm just not using anymore the `request.ejs` and instead just inject the JSON body which is the bit that interest me the most atm

```
{
  "protocol": "http",
  "port": 4542,
  "requests": [],
  "name": "settings",
  "stubs": [
    {
      "responses": [
        {
          "is": {
            "statusCode": 200,
            "headers": {
              "Content-Type": "application/json"
            },
            "body": "<%- stringify(filename, 'services/settings/body.json') %>"
          }
        }
      ]
    }
  ]
}
```

Thanks Brandon

Just in case someone else reach this thread let them know this is the PR that fix the issue on the 1.8 version https://github.com/bbyars/mountebank/issues/183

Rohit Goyal

unread,
Sep 21, 2017, 8:42:31 AM9/21/17
to mountebank-discuss
hey Guys,

I am also facing similar problem. Did you resolve this? I checked the issue #183 https://github.com/bbyars/mountebank/issues/183

I have added my details to issue as well. 

Can someone help if know how to fix it?

Cheers!
Rohit 

Brandon Byars

unread,
Sep 23, 2017, 5:19:19 PM9/23/17
to Rohit Goyal, mountebank-discuss
Responded on the github issue, but I suspect it's a simple problem of being on an outdated version of mountebank. Let me know if that's not the case.

--
Reply all
Reply to author
Forward
0 new messages