ubirak / rest-api-behat-extension

Stuff to easily test your rest api with Behat
MIT License
38 stars 24 forks source link

#87 Step: When I attach the following files: #93

Closed bystro closed 6 years ago

bystro commented 6 years ago

I have tested the code with ZendFramework's AbstractRestfulControlle and tests passed https://github.com/zendframework/zend-mvc/blob/master/src/Controller/AbstractRestfulController.php

bystro commented 6 years ago

Ok. Pull request is ready for review.

tyx commented 6 years ago

Sorry to take time about this PR but I still not convinced we could do that.

How do you inform your backend that body is json as the content-type is multipart ? ping @shouze

bystro commented 6 years ago

The request is not being sent with "application/json" header!

Even if you mannualy set header to "application/json" https://github.com/ubirak/rest-api-behat-extension/pull/93/files#diff-7e75094618ac1c011c28df106748e912R46 the request still is being sent as '"multipart/form-data" https://github.com/ubirak/rest-api-behat-extension/pull/93/files#diff-7e75094618ac1c011c28df106748e912R64

bystro commented 6 years ago

Request behaves as request sent from HTML form.

tyx commented 6 years ago

Yes I got it and it is exactly my problem ;) You send a file and a json body. You inform your backend about the file but not the json body. So I still wondering how it could work in a real world app

bystro commented 6 years ago

Every json body attribute is being transformed into multipart/form-data https://github.com/ubirak/rest-api-behat-extension/pull/93/files#diff-9d1a35b8dac9c540c2aedf7c51d8e7e7R210

Json body is being sent as common HTML form fields with names and values form-data-field

request-transformation-into-multipart-form-data

bystro commented 6 years ago

See 'post_fields_count' equals 3 because fields are being sent as HTML form and fields are placed in $_POST superglobal. https://github.com/ubirak/rest-api-behat-extension/pull/93/files#diff-7e75094618ac1c011c28df106748e912R62

bystro commented 6 years ago

In my ZendFramework api apps it works out of the box(natively) because it handles this way https://github.com/zendframework/zend-mvc/blob/master/src/Controller/AbstractRestfulController.php#L469

If request is not with 'application/json' headers, then it gets body from $_POST.

tyx commented 6 years ago

Ok, thanks for the explanation I get your point.

So we clearly need to remove this magic stuff where we switch from json to array in fact.

I guess we had in a previous version the possibility to pass a body as an array (it would explain the outdated phpdoc you mentioned). We just need to get back this behavior.

I have some time this afternoon, I will have a deeper look to merge this PR without magic

bystro commented 6 years ago

"So we clearly need to remove this magic stuff where we switch from json to array in fact." What do you mean?

tyx commented 6 years ago

This line https://github.com/bystro/rest-api-behat-extension/blob/a7d35a3de1692eba9e4f0280186bc3b63537c7a3/src/Rest/RestApiBrowser.php#L211

We inject json in PyString and transform it in array without let people know

bystro commented 6 years ago

Oh, OK. Will you correct this ?

tyx commented 6 years ago

Yes I'm on it !

tyx commented 6 years ago

Closed in favor of #94