imbo / behat-api-extension

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

Allow HTTP PATCH (and other HTTP methods) with form parameters #48

Closed Tartare2240 closed 7 years ago

Tartare2240 commented 7 years ago

I would like to be able to use a form with PATCH method. This PR just doesn't change the method if it is POST and PATCH.

There shouldn't be regressions except the case if someone called a PATCH method and expected a POST.

christeredvartsen commented 7 years ago

As a result of using HTTP PATCH with form parameters, the $_POST superglobal won't be populated right? So you simply fetch the key / value pairs from the request body?

I have a somewhat different proposal on how to fix this:

1) In the context class, introduce a boolean flag, protected $httpMethodSpecified = false;. This will only be updated to true after a call to setRequestMethod, with any given HTTP method.

2) Change requestPath($path, $method = 'GET') to requestPath($path, $method = null) and only call setRequestMethod if $method !== null. This won't change the current behavior as the request instance is initially created with GET in the setClient method: $this->request = new Request('GET', $client->getConfig('base_uri'));, so the current call to setRequestMethod('GET') in requestPath if no other method has been specified is superfluous.

3) Update the sendRequest method from doing if (!empty($this->requestOptions['form_params'])) to if (!empty($this->requestOptions['form_params']) && !$this->httpMethodSpecified). This way all HTTP methods can be used with form_params, and not only POST and PATCH, and the extension will use POST by default when using form parameters, if no other HTTP method has been specified, which I think is a sane default.

Do you think this is an OK solution?

Tartare2240 commented 7 years ago

I am using Symfony framework for my projects and all magic variables from session (as $_POST) are embedded in a Request object. I never did REST API without Symfony, and you just learnt me that PATCH method wasn't using the same or another superglobal.

I agree with your changes, I will fixup my commit.

Tartare2240 commented 7 years ago

Not what I expected... Sorry I'm new to contributing, and it looks I configured badly my git account. Tried to change the 1st commit author but it changed for every commit... I just don't want that this name appears

christeredvartsen commented 7 years ago

I fixed the author in 2336e3a7d1fd62c27cfa5ad15d0039a09802fdb2. It should be correct now?

Tartare2240 commented 7 years ago

Yes thanks a lot !

christeredvartsen commented 7 years ago

@Tartare2240 This functionality is present in the v2.0.1 release.