ubirak / rest-api-behat-extension

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

When I send a POST request to "/api/request" with files: #87

Closed bystro closed 6 years ago

bystro commented 6 years ago

It seems to be useful to test requests with files included.

I use https://github.com/danielm/uploader in my projects. Dmuploader posts files to my REST API ex. /api/order/attachment-file API responses success or false information.

Very precursory scenario format ...

Scenario: Privileged user attaches the file to the order 
When I send a POST request to "/api/order/attachment-file" with files:
"""
        {
            "files" : [{"fixtures/test-file1.png", "fixtures/test-file2.png"}],
        }
 """
Then the response status code should be 200
And the response should be in JSON
And the JSON should be equal to:
      """
        {
          "success": true,
          "data": 
              {
                  "success_message": "Files attached succesfully"
              }
      }
      """

What do you think?

tyx commented 6 years ago

Why not !

But I just guess we should use table notation instead of string notation, like

When I send a POST request to "/api/order/attachment-file" with files attached:
    | files                   |
    | fixtures/test-file1.png |
    | fixtures/test-file2.png |

It would be more simple to use and to code !

bystro commented 6 years ago

I wonder, how to make a mixture with posting files and body using existing "I send a POST request to "echo" with body:". ?

Wnen I send a POST request to "echo" with body:
"""
        {
            "username" : "pablo",
            "password": "money"
        }
        """
     with files attached:
    | files                   |
    | fixtures/test-file1.png |
    | fixtures/test-file2.png |
stephpy commented 6 years ago

Not sure if Behat syntax accepts it. Did you test it ?

bystro commented 6 years ago

I haven't test it yet. I'm looking for notation with only one "send request to".

It would be very ackward if I would sent it as following...

Wnen I send a POST request to "echo" with body:
"""
 {
     "username" : "pablo",
     "password": "money"
 }
"""
And When I send a POST request to "echo" with files attached:
 | files                   |
 | fixtures/test-file1.png |
 | fixtures/test-file2.png |
stephpy commented 6 years ago

We can consider this:

Wnen I send a POST request to "echo" with:
    | body                   | {.........}
    | files[0]                | ....
    | files[1]                | ....
bystro commented 6 years ago

Have anyone checked if and how it is implemented at other extensions?

tyx commented 6 years ago

The only option we get I guess is to have 2 steps with a first one that push state (like headers)

bystro commented 6 years ago

@tyx could you write an example?

tyx commented 6 years ago
Given I attach files:
 | files                   |
 | fixtures/test-file1.png |
 | fixtures/test-file2.png |
When I send a POST request to "echo":
"""
 {
     "username" : "pablo",
     "password": "money"
 }
"""
bystro commented 6 years ago

I'm working on the issue today.

The problem is to send payload with json and files(multipart/form-data) together. When I'm sending payload using "Content-Type: multipart/form-data; boundary=..." header, the json payload does not exists in a request body - "Wnen I send a POST request to "echo" with body:".

bystro commented 6 years ago

https://stackoverflow.com/questions/48009305/how-to-post-http-request-with-json-and-files-payload

tyx commented 6 years ago

indeed ! And it makes sense as we should use a different content type.

I just had a look and in one app we used base_64 workaround.

bystro commented 6 years ago

I think that If files are included, json attributes should be placed in multipart/form-data body.

But a big quession is if most restful frameworks will handle this like ZendFramework does https://github.com/zendframework/zend-mvc/blob/master/src/Controller/AbstractRestfulController.php#L471

tyx commented 6 years ago

But how could you guess you add json in body without correct content type?

tyx commented 6 years ago

Fixed by #94