本文整理汇总了PHP中Psr\Http\Message\RequestInterface::withMethod方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestInterface::withMethod方法的具体用法?PHP RequestInterface::withMethod怎么用?PHP RequestInterface::withMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\RequestInterface
的用法示例。
在下文中一共展示了RequestInterface::withMethod方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testMissingToken
public function testMissingToken()
{
$storage = [];
// <-- Missing token name and value
$request = $this->request->withMethod('POST')->withParsedBody(['csrf_name' => 'csrf_123', 'csrf_value' => 'xyz']);
$response = $this->response;
$next = function ($req, $res) {
return $res;
};
$mw = new Guard('csrf', $storage);
$newResponse = $mw($request, $response, $next);
$this->assertEquals(400, $newResponse->getStatusCode());
}
示例2: testWithMethod
public function testWithMethod()
{
$this->assertNotSame($this->request, $this->request->withMethod('post'));
$this->assertEquals('POST', $this->request->withMethod('post')->getMethod());
try {
$this->request->withMethod([]);
$this->fail();
} catch (InvalidArgumentException $e) {
$this->assertEquals('Unsupported HTTP method. It must be a string.', $e->getMessage());
}
try {
$this->request->withMethod('UNKNOWN');
$this->fail();
} catch (InvalidArgumentException $e) {
$this->assertEquals('Unsupported HTTP method. "UNKNOWN" provided.', $e->getMessage());
}
}
示例3: convertGetToPost
/**
* Converts default GET request to a POST request
*
* Avoiding length restriction in query
*
* @param RequestInterface $r GET request to be converted
* @return RequestInterface $req converted POST request
*/
public static function convertGetToPost(RequestInterface $r)
{
if ($r->getMethod() === 'POST') {
return $r;
}
$query = $r->getUri()->getQuery();
$req = $r->withMethod('POST')->withBody(Psr7\stream_for($query))->withHeader('Content-Length', strlen($query))->withHeader('Content-Type', 'application/x-www-form-urlencoded')->withUri($r->getUri()->withQuery(''));
return $req;
}
示例4: convertPostToGet
/**
* Converts a POST request to a GET request by moving POST fields into the
* query string.
*
* Useful for pre-signing query protocol requests.
*
* @param RequestInterface $request Request to clone
*
* @return RequestInterface
* @throws \InvalidArgumentException if the method is not POST
*/
public static function convertPostToGet(RequestInterface $request)
{
if ($request->getMethod() !== 'POST') {
throw new \InvalidArgumentException('Expected a POST request but ' . 'received a ' . $request->getMethod() . ' request.');
}
$sr = $request->withMethod('GET')->withBody(Psr7\stream_for(''))->withoutHeader('Content-Type')->withoutHeader('Content-Length');
// Move POST fields to the query if they are present
if ($request->getHeaderLine('Content-Type') === 'application/x-www-form-urlencoded') {
$body = (string) $request->getBody();
$sr = $sr->withUri($sr->getUri()->withQuery($body));
}
return $sr;
}
示例5: testExternalStorageOfAnArrayPersists
public function testExternalStorageOfAnArrayPersists()
{
$storage = [];
$request = $this->request->withMethod('POST')->withParsedBody(['csrf_name' => 'csrf_123', 'csrf_value' => 'xyz']);
$response = $this->response;
$next = function ($req, $res) {
return $res;
};
$mw = new Guard('csrf', $storage);
$this->assertEquals(0, count($storage));
$newResponse = $mw($request, $response, $next);
$this->assertEquals(1, count($storage));
}
示例6: getRequest
/**
* @return \Psr\Http\Message\RequestInterface
*/
public function getRequest()
{
$request = $this->request->withMethod($this->method);
$uri = $request->getUri()->withScheme($this->scheme)->withHost($this->host)->withPath($this->path)->withQuery($this->getQueryString());
$request = $request->withUri($uri);
$request = $request->withProtocolVersion($this->protocolVersion);
$request = $request->withHeader("Accept", "application/vnd.api+json");
$request = $request->withHeader("Content-Type", "application/vnd.api+json");
foreach ($this->headers as $name => $value) {
$request = $request->withHeader($name, $value);
}
$request->getBody()->write($this->body);
return $request;
}
示例7: withMethod
/**
* @param string $method
* @return RequestInterface
*/
public function withMethod($method)
{
$this->request = $this->request->withMethod($method);
return $this;
}
示例8: withMethod
public function withMethod($method)
{
$new = clone $this;
$new->request = $this->request->withMethod($method);
return $new;
}
示例9: put
/**
* {@inheritdoc}
*/
public function put(RequestInterface $request)
{
return $this->request($request->withMethod('PUT'));
}