Support other Content-Types
Hello,
I'd like to use interfake to simulate an API I'm implementing but I think I can't use more than one Content-Type for the same HTTP resource. In my case, I'd like to answer the same data but in different formats, depending on Accept header sent by client -- this is needed because some of my API clients use msgpack to serialize data and others simply use JSON.
I've done a quick look into interfake's code but as I'm new to nodejs world, I didn't start implementing this feature by now, but I think we'll need to modify expectationsLookup and Route.simpleHash to implement this feature. What do you think?
Note: thanks for writing this awesome project! :)
Hey @turicas thanks for the suggestion. I think it's a sensible one, not entirely how I'd approach implementing it though.
I was a bit hesitant because I don't want to expand Interfake's scope too much but I can see how it could be useful, and fairly simple to change the response format.
For msgpack especially, it might be possible to simply use this encoder/decoder, msgpack-js as some kind of middleware for the body.
I'm all for it! You're welcome to give it a go if you like, but for now I'll put a looking-for-contributor label on it. If you get to it, say so, but if not I'll give it a go unless someone else does!
@basicallydan, first of all, thanks! :-)
One possible solution is to create a http-proxy on top of Interfake that does all the content-type thing, so we don't need to modify Interfake internals.
Unfortunately by now I don't enough time and knowledge on NodeJS/Express to do it quickly.
@turicas No prob!
Would that http-proxy module work for the case you're talking about? It sounds like the data would need to be transformed, and it might be easy enough to do it within Interfake. If http-proxy can do it without changing how the data is stored in memory then it should be just a matter of detecting the accept header type and transforming it. Does that sound about right?
Yep! The idea is to put on the proxy module the responsability on detecting the Accept header and serializing (from JSON) to other formats if needed before delivering the response to the client.