Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend\Test PUT method with data has no content #3904

Closed
wants to merge 1 commit into from

Conversation

four43
Copy link

@four43 four43 commented Feb 26, 2013

The Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase may have a bug when using the HTTP method PUT with data. Testing a RESTful API's update method:

$this->dispatch('/v1/endpoint/1', 'PUT', array('name' => 'My Name'));
$this->assertResponseStatusCode(200);

The 3rd argument to dispatch, the $params for the request, don't seem to get transformed into the request in anyway. They seem to just get dropped.The Zend\Test\PHPUnit\Controller\AbstractControllerTestCase::url() method just seems to look for "GET" and "POST" methods) The result is that the update($id, $data) method of my Zend\Mvc\Controller\AbstractRestfulController never receives any data. If I run the method via curl, everything checks out. Can anyone else confirm this? I don't find this to be expected behavior.

Thanks,
-Seth

EDIT: Setting the content seems to work a little better.

$this->getRequest()->setContent(json_encode(array('name' => 'My Name')));

Is this the recommended way to use these test methods? Maybe all that is wrong is the need to add to the documentation a bit. I can certainly help with that.

@@ -377,7 +377,7 @@ public function requestHasContentType(Request $request, $contentType = '')
$requestedContentType = trim($requestedContentType);
if (array_key_exists($contentType, $this->contentTypes)) {
foreach ($this->contentTypes[$contentType] as $contentTypeValue) {
if (stripos($contentTypeValue, $requestedContentType) === 0) {
if (stripos($contentTypeValue, $requestedContentType) !== false) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing me the issue. It was looking for only 'json' as defined by CONTENT_TYPE_JSON. It wasn't looking through the whole string to find the 'json' at the end. The tests are using 'application\hal+json' and failing.

@blanchonvincent
Copy link
Contributor

@cr125rider with Zend\Test and url() or dispatch() method, you can only override $_GET and $_POST. PUT method use "php://input'", so

$this->dispatch('/v1/endpoint/1', 'PUT', array('name' => 'My Name'));

the 3rd param is useless. I will change this to set the request content to use with PUT method.

@blanchonvincent
Copy link
Contributor

@cr125rider look #3916

@four43
Copy link
Author

four43 commented Feb 27, 2013

Hey that looks great. Thanks a lot for looking at this. Even more descriptive messages are returned when something isn't quite right which is always good.

I am still open to changes for the best way to add json content to the testing request. I guess I originally had 2 issues, the adding the put params you fixed. The other being the request interpreted as json, which I think I fixed (?) or at least kind of found the area that may need modifications. I'll wait on that for someone a little more experienced with this part of the code.

Thanks for your help @blanchonvincent.

@weierophinney
Copy link
Member

I've merged #3916 as a better approach.

@cr125rider If you are expecting JSON via PUT or POST, the appropriate mechanism is to use setContent() with the actual JSON string, and to ensure the Content-Type header is set correctly. That will more accurately simulate the execution path of an actual request.

@four43
Copy link
Author

four43 commented Mar 13, 2013

@weierophinney I worked my way into that solution... eventually :) . Thanks for your help.

weierophinney added a commit to zendframework/zend-test that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants