imbo / behat-api-extension

API extension for Behat, used to ease testing of JSON-based APIs
MIT License
108 stars 42 forks source link

Multipart options should be cleared after a response. #69

Closed sebastiaanviaene closed 4 years ago

sebastiaanviaene commented 6 years ago
Scenario: Import excel file
    Given I am authenticated as "email21@panenco.com" with "password21" password
    #guzzle with take care of this header itself
    Given I remove request header "Content-Type"
    Given I attach "features/template.xlsx" to the request as "form[file]"
    When I request "api/v1/companies/imports" using HTTP "POST"
    Then the response reason phrase is "OK"
    Then I grab "id" from response as "session_id"
    Then the response body contains JSON:
     """
        {"columns":["firstName","lastName","email","title"],"count":29,"valid":true}
     """
    Given I clear request options
    Then I request "api/v1/imports/<<session_id>>/invites" using HTTP "GET"
    Then the response reason phrase is "OK"
    And the response body with "items" is a JSON array of length 0

The "Given I clear request options" step is something I had to add myself because my get request would return an error saying Invalid resource type: unknown type (InvalidArgumentException) The reason was that the multipart requestoptions were still set from the post request at the top of this scenario. I suggest including clearing the multipart requestoptions after each request.

This is the code I use to clear it :

public function clearOptions() {
        if(isset($this->requestOptions['multipart'])) {
            $this->requestOptions['multipart'] = [];
        }
    }

My temporary solution right now is to overwrite the "I Request with : path and :method" with my own version which includes this clear. But I would love to see it used by default.

sebastiaanviaene commented 6 years ago

to clarify, this is how I solved it now. Overriding the request step defintion.

public function requestPath($path, $method = null) {

        $this->setRequestPath($path);

        if (null !== $method) {
            $this->setRequestMethod($method);
        }

        $request = $this->sendRequest();

        if(isset($this->requestOptions['multipart'])) {
            $this->requestOptions['multipart'] = [];
        }

        return $request;
}
kopaygorodsky commented 6 years ago

I'll do PR tonight.

christeredvartsen commented 6 years ago

@kopaygorodsky Any progress?

kopaygorodsky commented 6 years ago

@christeredvartsen sory, had very busy week. Will do on this weekend.

christeredvartsen commented 4 years ago

Closing this because of lack of activity, and that I'm not sure if this is a bug or not. When making multiple requests within the same scenario, I'm not sure what the best behavior would be:

  1. Keep all request options as is
  2. Clear some request options after a request
  3. Clear all request options after a request

Feel free to reopen and continue the discussion if you are still using the project and would like this part to be improved.

milkovsky commented 1 year ago

Probably a separate step to clean the multipart options would help here.