moll / node-mitm

Intercept and mock outgoing Node.js network TCP connections and HTTP requests for testing. Intercepts and gives you a Net.Socket, Http.IncomingMessage and Http.ServerResponse to test and respond with. Super useful when testing code that hits remote servers.
Other
636 stars 48 forks source link

Additional properties to response #27

Closed G07cha closed 8 years ago

G07cha commented 8 years ago

I can't find how to set custom body for http response, for example if use res.write or res.end functions it will set only message property, how do I define other response properties?

moll commented 8 years ago

Which properties do you have in mind? Through HTTP you can send both headers with res.setHeader("X-Foo", "123") or body, with res.write.

G07cha commented 8 years ago

@moll I would like to send something like {"foo": "bar"} in body, but as I know res.write allow only string or buffer argument types.

moll commented 8 years ago

Yep. That's because HTTP, the underlying protocol, is text based.

You'll have to do this:

res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify({name: "John"}))

I don't know what's the best resource for reading more about HTTP and its idioms, but perhaps you're better at finding that out. ;)

G07cha commented 8 years ago

Thank you a lot, it's shame for me to don't know such fundamentals, I'm try to correct this as soon as possible, thanks again!

moll commented 8 years ago

You're welcome.