Behat / WebApiExtension

Behat extension to test web APIs
http://extensions.behat.org
MIT License
111 stars 114 forks source link

Duplicated Content-Type header provokes invalid request #44

Closed jawira closed 5 years ago

jawira commented 8 years ago

Hello, I have a problem the following feature:

  Scenario Outline: The POST Code response (Entity)
    Given I set header "Accept" with value "application/json"
    And  I set header "Content-Type" with value "application/json"
    When I send a POST request to "/api/users" with body:
    """
      <json>
    """
    Then the response code should be 201

    Examples:
      | json                                             |
      | {"first_name" : "Leigh", "last_name" : "Oros"}   |
      | {"first_name" : "Hope", "last_name" : "Perron"}  |
      | {"first_name" : "Allena", "last_name" : "Spece"} |

The test is correct for the first user (Leigh Oros), but the API throws an error for Hope Perron and Allena Speece. After introspecting the http request I detected that the error is triggered by the Accept and Content-Type headers.

For the first users the headers are:

Array
(
    [Host] => localhost
    [Accept] => application/json
    [Content-Type] => application/json
    [User-Agent] => Guzzle/5.3.0 curl/7.38.0 PHP/5.6.15
    [Content-Length] => 205
)

Until now API works correctly and everything is ok.

However, the request for the second user have the following headers

Array
(
    [Host] => localhost
    [Accept] => application/json, application/json
    [Content-Type] => application/json, application/json
    [User-Agent] => Guzzle/5.3.0 curl/7.38.0 PHP/5.6.15
    [Content-Length] => 205
)

And for third user:

Array
(
    [Host] => localhost
    [Accept] => application/json, application/json, application/json
    [Content-Type] => application/json, application/json, application/json
    [User-Agent] => Guzzle/5.3.0 curl/7.38.0 PHP/5.6.15
    [Content-Length] => 205
)

For second and third request, the answer from the API is:

{
  "type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
  "title":"Unsupported Media Type",
  "status":415,"detail":"Invalid content-type specified"
}

Cleary the headers are append to the existing ones, even if the value is the same. I was expecting that header would be reset for each Example, but is not the case.

Also I don't think that separating multiple mime times with commas are valid Accept and Content-Type headers.

For instance I have solved the issue checking if the header value is already present before adding it 9c602da70710ac70323e3fb6def4c2936c1e9afe however I'm not shure who's responsability is to check the validity of headers (WebApiExtension, Gizzle, Behat??). If the solution seems correct I want to make a pull request.

Any feedback will be appreciated.

jawira commented 8 years ago

The pull request with my fix : https://github.com/Behat/WebApiExtension/pull/47

jawira commented 5 years ago

Project seems abandoned. Closing issue.