Ejemplo n.º 1
0
 public function testConstructorCanAcceptAllMessageParts()
 {
     $uri = new Uri('http://example.com/');
     $body = new Stream('php://memory');
     $headers = ['x-foo' => ['bar']];
     $request = new Request($uri, 'POST', $body, $headers);
     $this->assertSame($uri, $request->getUri());
     $this->assertEquals('POST', $request->getMethod());
     $this->assertSame($body, $request->getBody());
     $testHeaders = $request->getHeaders();
     foreach ($headers as $key => $value) {
         $this->assertArrayHasKey($key, $testHeaders);
         $this->assertEquals($value, $testHeaders[$key]);
     }
 }
Ejemplo n.º 2
0
 /**
  * Build PSR Request.
  *
  * @return Psr\Http\Message\RequestInterface;
  */
 public function getRequest()
 {
     // Handle path parameters.
     $path_replacements = array();
     foreach ($this->parameters['path'] as $name => $value) {
         // We could coerce the value into a string, but it's not
         // clearly defined how we should do it.
         if (!is_scalar($value)) {
             throw new \RuntimeException('Path parameter "' . $name . '" not scalar.');
         }
         $path_replacements['{' . $name . '}'] = $value;
     }
     $url = $this->endpoint . strtr(ltrim($this->path, '/'), $path_replacements);
     // Handle query parameters.
     $query = $this->buildQuery($this->parameters['query']);
     $url = $url . '?' . $query;
     // There can only be one body.
     $body = reset($this->parameters['body']);
     if (!is_string($body)) {
         // Serialize if it's not a simple string.
         $body = $this->serializer->serialize($body);
     }
     $request = new Request($url, $this->method, new Stream('php://memory', 'w'), array());
     $request->getBody()->write($body);
     return $request;
 }