basicallydan / interfake

:computer: Fake APIs for prototypes & automated tests.
Other
805 stars 39 forks source link

HTTP Post with json content #32

Closed frhd closed 9 years ago

frhd commented 10 years ago

It would be nice to actually post real json data in a HTTP Post and store it temporarily. Is it feasible in interfake?

basicallydan commented 10 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?

frhd commented 10 years ago

@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"}
basicallydan commented 10 years ago

@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!

frhd commented 10 years ago

Thanks, I will look into it!

basicallydan commented 9 years ago

I think this should look like this:

interfake.post('/retrieve').echo();

Any thoughts, @frhd?

basicallydan commented 9 years ago

Just added this in v1.15.0 :)

frhd commented 9 years ago

@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 :)