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()); }
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()); } }
/** * 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; }
/** * 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; }
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)); }
/** * @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; }
/** * @param string $method * @return RequestInterface */ public function withMethod($method) { $this->request = $this->request->withMethod($method); return $this; }
public function withMethod($method) { $new = clone $this; $new->request = $this->request->withMethod($method); return $new; }
/** * {@inheritdoc} */ public function put(RequestInterface $request) { return $this->request($request->withMethod('PUT')); }