Example #1
0
 protected function getHostString(Message\MessageInterface $message)
 {
     if ($message->hasHeader('host')) {
         return '';
     }
     return "\r\nHost: " . $message->getUri()->getHost();
 }
 /**
  * Store a message in the cache.
  * 
  * @param string           $key     The message key
  * @param MessageInterface $message
  */
 private function saveIntoCache($key, MessageInterface $message)
 {
     if ($this->cache) {
         $item = $this->cache->getItem($key);
         $item->set([$message->getHeaders(), (string) $message->getBody()]);
         $this->cache->save($item);
     }
 }
Example #3
0
 /**
  * @param Psr\Http\Message\MessageInterface $message
  * 
  * @return string
  */
 private static function printHeaders(MessageInterface $message)
 {
     $headers = [];
     foreach ($message->getHeaders() as $name => $values) {
         $headers[] = $name . ': ' . implode(', ', $values);
     }
     return implode("\n", $headers);
 }
 private function addJsonToHttpMessage(MessageInterface $message, array $dataToTurnIntoJson, bool $castToObject = false)
 {
     if ($castToObject) {
         $dataToTurnIntoJson = (object) $dataToTurnIntoJson;
     }
     $json = json_encode($dataToTurnIntoJson);
     $message->getBody()->write($json);
     return $message;
 }
 /**
  * @inheritdoc
  */
 protected function deserialize(MessageInterface $message, $format)
 {
     if ($this->class) {
         $content = $message->getBody()->getContents();
         $parsedResponse = SerializerBuilder::create()->build()->deserialize($content, $this->class, $format, $this->context);
         return $parsedResponse;
     } else {
         return null;
     }
 }
 /**
  * Returns the "best match" of the given $priorities. Will return null in case
  * no match could be identified or a string containing the best matching Accept
  * header.
  *
  * @param MessageInterface $request
  * @param array $priorities A set of priorities.
  * @return null|string
  */
 public function getBestMatch(MessageInterface $request, array $priorities = array())
 {
     if (!$request->hasHeader('Accept')) {
         return null;
     }
     $header = $this->negotiator->getBest(implode(',', $request->getHeader('Accept')), $priorities);
     if ($header instanceof AcceptHeader) {
         return $header->getValue();
     }
     return null;
 }
 /**
  * Add the message body if the stream is seekable.
  *
  * @param MessageInterface $request
  * @param string           $message
  *
  * @return string
  */
 private function addBody(MessageInterface $request, $message)
 {
     $stream = $request->getBody();
     if (!$stream->isSeekable() || $this->maxBodyLength === 0) {
         // Do not read the stream
         $message .= "\n";
     } else {
         $message .= "\n" . mb_substr($stream->__toString(), 0, $this->maxBodyLength);
         $stream->rewind();
     }
     return $message;
 }
Example #8
0
 /**
  * @inheritdoc
  */
 public function parse(MessageInterface $message)
 {
     $content = $message->getBody()->getContents();
     if ($this->removeNamespacePrefixes) {
         $content = preg_replace('/(<\\/?)(\\w+:)/', '$1', $content);
     }
     if ($this->class) {
         $parsedResponse = SerializerBuilder::create()->build()->deserialize($content, $this->class, 'xml', $this->context);
     } else {
         $parsedResponse = simplexml_load_string($content);
     }
     return $parsedResponse;
 }
 /**
  * Validate an HTTP message content-type against a message definition
  *
  * @param MessageInterface $message
  * @param MessageDefinition $definition
  *
  * @return bool Is valid
  */
 public function validateContentType(MessageInterface $message, MessageDefinition $definition)
 {
     $isValid = true;
     $contentType = $message->getHeaderLine('Content-Type');
     if (!in_array($contentType, $definition->getContentTypes())) {
         $isValid = false;
         if ($contentType === '') {
             $violationMessage = 'Content-Type should not be empty';
             $constraint = 'required';
         } else {
             $violationMessage = sprintf('%s is not a supported content type, supported: %s', $message->getHeaderLine('Content-Type'), implode(', ', $definition->getContentTypes()));
             $constraint = 'enum';
         }
         $this->addViolation(new ConstraintViolation('Content-Type', $violationMessage, $constraint, 'header'));
     }
     return $isValid;
 }
Example #10
0
 /**
  * @param MessageInterface $message
  *
  * @codeCoverageIgnore
  * @return string
  */
 public function str(MessageInterface $message) : string
 {
     if ($message instanceof RequestInterface) {
         $msg = trim($message->getMethod() . ' ' . $message->getRequestTarget()) . ' HTTP/' . $message->getProtocolVersion();
         if (!$message->hasHeader('host')) {
             $msg .= "\r\nHost: " . $message->getUri()->getHost();
         }
     } elseif ($message instanceof ResponseInterface) {
         $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' . $message->getStatusCode() . ' ' . $message->getReasonPhrase();
     }
     foreach ($message->getHeaders() as $name => $values) {
         $msg .= "\r\n{$name}: " . implode(', ', $values);
     }
     if (ini_get('memory_limit') < 0 || $message->getBody()->getSize() < ini_get('memory_limit')) {
         $msg .= "\r\n\r\n" . $message->getBody();
     }
     return $msg;
 }
Example #11
0
 public function testWithBody()
 {
     $mockBody = Mockery::mock(StreamInterface::class);
     $messageWithBody = $this->message->withBody($mockBody);
     static::assertNull($this->message->getBody());
     // return null
     // The body MUST be a StreamInterface object.
     static::assertNotSame($this->message, $messageWithBody);
     // This method MUST be implemented in such a way as to retain the
     // immutability of the message, and MUST return a new instance that has the
     // new body stream.
     static::assertInstanceOf(StreamInterface::class, $messageWithBody->getBody());
     static::assertSame($mockBody, $messageWithBody->getBody());
 }
Example #12
0
 /**
  * Creates a Body instance to execute assertions in the body.
  *
  * @return Stream
  */
 public function assertBody()
 {
     return new Stream($this->message->getBody(), $this);
 }
Example #13
0
 /**
  * Asserts that the response does not contain the specified header.
  *
  * @param MessageInterface $message
  * @param string $name
  */
 public function assertHasNotHeader(MessageInterface $message, $name)
 {
     $this->assertFalse($message->hasHeader($name), "The message contains the header {$name}");
 }
 protected function runMatches(MessageInterface $message)
 {
     return $message->getProtocolVersion() == $this->expected;
 }
Example #15
0
 /**
  * Parse the response from the API.
  * 
  * @param MessageInterface $response
  * @return Response
  * @codeCoverageIgnore
  */
 public function toResponse(MessageInterface $response)
 {
     $json = $response->getBody()->getContents();
     $toArray = json_decode($json, true);
     return new Response($toArray);
 }
 /**
  * Adds request id to request/response and returns new instance.
  *
  * @param MessageInterface $message
  *
  * @return MessageInterface
  *
  * @throws \InvalidArgumentException
  */
 public function decorate(MessageInterface $message)
 {
     return $message->withHeader($this->headerName, $this->idProvider->getRequestId());
 }
Example #17
0
 public function set($header, $value)
 {
     $this->request = $this->request->withHeader($header, $value);
 }
Example #18
0
 /**
  * Method to add a Cache-Control header to the provided PSR-7 message.
  *
  * @link https://tools.ietf.org/html/rfc7234#section-5.2
  *
  * @param MessageInterface $message PSR-7 message to add the Cache-Control header to
  * @param CacheControl $cacheControl Cache-Control object to add to the message
  * @return MessageInterface The PSR-7 message with the added Cache-Control header
  * @throws InvalidArgumentException If the Cache-Control header is invalid
  */
 public function withCacheControl(MessageInterface $message, CacheControl $cacheControl)
 {
     return $message->withHeader('Cache-Control', (string) $cacheControl);
 }
Example #19
0
 protected function runMatches(MessageInterface $message)
 {
     return $this->expected == $message->getHeaderLine($this->key);
 }
 private function addJsonToHttpMessage(MessageInterface $message, array $dataToTurnIntoJson)
 {
     $json = json_encode($dataToTurnIntoJson);
     $message->getBody()->write($json);
     return $message;
 }
Example #21
0
 /**
  * Marshal the host and port from HTTP headers and/or the PHP environment
  *
  * @param array $server
  * @param MessageInterface $request
  * @return array Array with two members, host and port, at indices 0 and 1, respectively
  * @deprecated as of 0.7.0; use marshalHostAndPortFromHeaders() instead.
  */
 public static function marshalHostAndPort(stdClass $accumulator, array $server, MessageInterface $request)
 {
     return self::marshalHostAndPortFromHeaders($accumulator, $server, $request->getHeaders());
 }
Example #22
0
 protected function runMatches(MessageInterface $message)
 {
     return (string) $message->getBody() == $this->expected;
 }
 /**
  * @param MessageInterface $message
  * @param string $headerName
  * @param string $value
  *
  * @return bool
  */
 private function hasHeaderContains(MessageInterface $message, $headerName, $value)
 {
     return strpos($message->getHeaderLine($headerName), $value) !== false;
 }
 /**
  * Get body with headers.
  *
  * @param MessageInterface $stream
  *
  * @return string
  */
 public function getBodyWithHeaders(MessageInterface $stream)
 {
     $result = sprintf('%s%s%s', $this->getString($stream->getHeaders()), PHP_EOL, $stream->getBody()->getContents());
     return $result;
 }
Example #25
0
 private function headers(MessageInterface $message)
 {
     $result = '';
     foreach ($message->getHeaders() as $name => $values) {
         $result .= $name . ': ' . implode(', ', $values) . "\r\n";
     }
     return trim($result);
 }
Example #26
0
 /**
  * {@inheritdoc}
  */
 public function withBody(StreamInterface $body)
 {
     $new = clone $this;
     $new->message = $this->message->withBody($body);
     return $new;
 }
 public function testWithAddedHeaderAllowsHeaderContinuations()
 {
     $message = $this->message->withAddedHeader('X-Foo-Bar', "value,\r\n second value");
     $this->assertEquals("value,\r\n second value", $message->getHeaderLine('X-Foo-Bar'));
 }
Example #28
0
 protected function runMatches(MessageInterface $message)
 {
     return $message->hasHeader($this->expected);
 }
Example #29
0
/**
 * Attempts to rewind a message body and throws an exception on failure.
 *
 * The body of the message will only be rewound if a call to `tell()` returns a
 * value other than `0`.
 *
 * @param MessageInterface $message Message to rewind
 *
 * @throws \RuntimeException
 */
function rewind_body(MessageInterface $message)
{
    $body = $message->getBody();
    if ($body->tell()) {
        $body->rewind();
    }
}
Example #30
0
 /**
  * Parse the body of a PSR-7 message, into a PHP array.
  * TODO: We really need to find a package to do this. It is built into the
  * Guzzle client as helper methods, but this is not specifically a client
  * function.
  *
  * @param $message MessageInterface
  * @return array|mixed
  */
 public static function parseBody(MessageInterface $message)
 {
     // If a ServerRequest object, then parsing will be handled (and cached if necessary)
     // by the implementation.
     if ($message instanceof ServerRequestInterface) {
         return $message->getParsedBody();
     }
     $data = [];
     if ($message->hasHeader('Content-Type')) {
         // Sage Pay returns responses generally with JSON, but the notify callback is Form URL
         // encoded, so we need to parse both.
         if ($message->getHeaderLine('Content-Type') === 'application/x-www-form-urlencoded') {
             parse_str((string) $message->getBody(), $data);
         } elseif ($message->getHeaderLine('Content-Type') === 'application/json') {
             $data = json_decode((string) $message->getBody(), true);
         }
     }
     return $data;
 }