Skip to content

Commit

Permalink
Merge 5b969c3 into 2e21ccc
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 10, 2020
2 parents 2e21ccc + 5b969c3 commit 66f6de9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/Http/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ abstract class ServerRequestFactory implements ServerRequestFactoryInterface
* @param array $body $_POST superglobal
* @param array $cookies $_COOKIE superglobal
* @param array $files $_FILES superglobal
* @param string $input php://input Used to stub request streams in testing.
* @return \Cake\Http\ServerRequest
* @throws \InvalidArgumentException for invalid file values
*/
Expand All @@ -57,7 +58,8 @@ public static function fromGlobals(
?array $query = null,
?array $body = null,
?array $cookies = null,
?array $files = null
?array $files = null,
?string $input = null
): ServerRequest {
$server = normalizeServer($server ?: $_SERVER);
$uri = static::createUri($server);
Expand All @@ -79,6 +81,7 @@ public static function fromGlobals(
'base' => $uri->base,
'session' => $session,
'mergeFilesAsObjects' => Configure::read('App.uploadedFilesAsObjects', true),
'input' => $input,
]);

return $request;
Expand Down
36 changes: 22 additions & 14 deletions src/TestSuite/IntegrationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,19 +592,6 @@ protected function _buildRequest(string $url, $method, $data = []): array
}

parse_str($query, $queryData);
$props = [
'url' => $url,
'session' => $session,
'query' => $queryData,
'files' => [],
];
if (is_string($data)) {
$props['input'] = $data;
} else {
$data = $this->_addTokens($tokenUrl, $data);
$props['post'] = $this->_castToString($data);
}
$props['cookies'] = $this->_cookie;

$env = [
'REQUEST_METHOD' => $method,
Expand All @@ -627,7 +614,28 @@ protected function _buildRequest(string $url, $method, $data = []): array
}
unset($this->_request['headers']);
}
$props['environment'] = $env;
$props = [
'url' => $url,
'session' => $session,
'query' => $queryData,
'files' => [],
'environment' => $env,
];

if (is_string($data)) {
$props['input'] = $data;
} elseif (
is_array($data) &&
isset($props['environment']['CONTENT_TYPE']) &&
$props['environment']['CONTENT_TYPE'] === 'application/x-www-form-urlencoded'
) {
$props['input'] = http_build_query($data);
} else {
$data = $this->_addTokens($tokenUrl, $data);
$props['post'] = $this->_castToString($data);
}

$props['cookies'] = $this->_cookie;
$props = Hash::merge($props, $this->_request);

return $props;
Expand Down
11 changes: 2 additions & 9 deletions src/TestSuite/MiddlewareDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Cake\Http\ServerRequestFactory;
use Cake\Routing\Router;
use Cake\Routing\RoutingApplicationInterface;
use Laminas\Diactoros\Stream;
use LogicException;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -162,17 +161,11 @@ protected function _createRequest(array $spec): ServerRequest
$spec['query'],
$spec['post'],
$spec['cookies'],
$spec['files']
$spec['files'],
$spec['input'] ?? null
);
$request = $request->withAttribute('session', $spec['session']);

if (isset($spec['input'])) {
$stream = new Stream('php://memory', 'rw');
$stream->write($spec['input']);
$stream->rewind();
$request = $request->withBody($stream);
}

return $request;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,24 @@ public function testPostDataHttpServer()
$this->assertHeader('X-Middleware', 'true');
}

/**
* Test that the PSR7 requests receive put data
*
* @return void
*/
public function testPutDataFormUrlEncoded()
{
$this->configRequest([
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);
$this->put('/request_action/post_pass', ['title' => 'value']);
$this->assertResponseOk();
$data = json_decode('' . $this->_response->getBody());
$this->assertSame('value', $data->title);
}

/**
* Test that the uploaded files are passed correctly to the request
*
Expand Down

0 comments on commit 66f6de9

Please sign in to comment.