Closed frhd closed 9 years ago
Hey @frhd I think Interfake already can do what you may be trying to ultimately achieve.
I'm assuming your goal is to retrieve that data later? You can accomplish that with a bit of code like this:
interfake.post('/save').creates.get('/retrieve').body({ thedata : 'withvalues' });
Basically, by posting to /save
you're creating an endpoint at /retrieve
which returns the JS object in the body
call. Is that what you were trying to do?
@basicallydan But where is the data being stored? In your example the post data is not being saved in the body, and that's what I'm trying to achieve. Maybe it's possible, but I wasnt able to figure it out. I hope the reasoning behind why I wanna do this is obvious ;)
Something like this:
curl -H "Content-Type: application/json" -d '{"someData":"xyz"}' localhost:3003/save
and then retrieve the data with:
curl localhost:3003/retrieve
// response: {"someData":"xyz"}
@frhd That's cool. The data that is POSTed is thrown away, but the data set in the /retrieve
call in the example is just being stored in memory.
I see what you're trying to achieve now, I think, and it's not possible. I guess I had just expected that a major use case for Interfake was that the body that should be returned is already known, since I was using it primarily for testing things. Hmm. It should be easy enough to store it for return but I'm not sure exactly what approach to take off the top of my head.
You're more than welcome to give it a go in a fork if you like? Otherwise, I'll have to do this when I get a chance!
Thanks, I will look into it!
I think this should look like this:
interfake.post('/retrieve').echo();
Any thoughts, @frhd?
Just added this in v1.15.0 :)
@basicallydan Thanks for the great work! :)
interfake.post('/retrieve').echo();
works as expected. Post something to /retrieve
and it gets echoed back.
However, I'm not sure if that's exactly what I was talking about initially. The use case I had in mind was going beyond static API tests where you know in and out data in advance, but mirroring CRUD-like API behaviour in tests. Something along the lines of:
// post data, e.g. "1=item1"
interfake.post('/item/new').status(201).body({ created: true });
// return previous POST data, e.g. gets {"1":"item1"}
interfake.get('/items').echo();
I think I will try my hand at this, since my familiarity with node has slightly been improved over the year :)
It would be nice to actually post real json data in a HTTP Post and store it temporarily. Is it feasible in interfake?