public function testWithoutHeaderDoesNothingIfHeaderDoesNotExist()
 {
     $this->assertFalse($this->message->hasHeader('X-Foo'));
     $message = $this->message->withoutHeader('X-Foo');
     $this->assertNotSame($this->message, $message);
     $this->assertFalse($message->hasHeader('X-Foo'));
 }
Beispiel #2
0
 protected function getHostString(Message\MessageInterface $message)
 {
     if ($message->hasHeader('host')) {
         return '';
     }
     return "\r\nHost: " . $message->getUri()->getHost();
 }
 /**
  * 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;
 }
Beispiel #4
0
/**
 * Returns the string representation of an HTTP message.
 *
 * @param MessageInterface $message Message to convert to a string.
 *
 * @return string
 */
function str(MessageInterface $message)
{
    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();
    } else {
        throw new \InvalidArgumentException('Unknown message type');
    }
    foreach ($message->getHeaders() as $name => $values) {
        $msg .= "\r\n{$name}: " . implode(', ', $values);
    }
    return "{$msg}\r\n\r\n" . $message->getBody();
}
Beispiel #5
0
 public function str(MessageInterface $message)
 {
     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 ($message->getBody()->getSize() < ini_get('memory_limit')) {
         $msg .= "\r\n\r\n" . $message->getBody();
     }
     return $msg;
 }
Beispiel #6
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->hasHeader($this->expected);
 }
Beispiel #8
0
 /**
  * {@inheritdoc}
  */
 public function hasHeader($header)
 {
     return $this->message->hasHeader($header);
 }
Beispiel #9
0
 public function has($header)
 {
     return $this->request->hasHeader($header);
 }
Beispiel #10
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;
 }