public function testParsingHeaderWithInvalidNameShouldThrowException() { $msg = 'Invalid header name "bad boy header"'; $this->setExpectedException('Wookieb\\ZorroRPC\\Exception\\InvalidHeaderException', $msg); $headers = 'bad boy header:test'; Parser::parseHeaders($headers); }
/** * Set particular header value * * @param string|array $name string or array of headers where keys are treated as header names * @param string $value * * @return self * @throws \Wookieb\ZorroRPC\Exception\InvalidHeaderException */ public function set($name, $value = null) { if (is_array($name)) { foreach ($name as $headerName => $headerValue) { $this->set($headerName, $headerValue); } return $this; } if (!Parser::isValidHeaderName($name)) { throw new InvalidHeaderException('Invalid header name "' . $name . '"'); } $this->headers[strtolower($name)] = $value; return $this; }
/** * {@inheritDoc} */ public function receiveRequest() { try { $message = $this->socket->recvMulti(); } catch (\Exception $e) { throw new TransportException('Unable to receive request', null, $e); } $this->waitingForResponse = true; $requestType = $message[0]; if (!MessageTypes::isValid($requestType)) { throw new FormatException('Invalid request type "' . $requestType . '"', $message); } $request = new Request($requestType); $request->setHeaders(new Headers(Parser::parseHeaders(@$message[1]))); if ($requestType !== MessageTypes::PING) { if (empty($message[2])) { throw new FormatException('Method name is empty', $message); } $request->setMethodName($message[2]); $arguments = array_slice($message, 3); $request->setArgumentsBody($arguments); } return $request; }
/** * {@inheritDoc} */ public function receiveResponse() { try { $result = $this->socket->recvMulti(); } catch (\ZMQSocketException $e) { throw new TransportException('Cannot receive response', 0, $e); } if ($result === false) { throw new TimeoutException('Timeout (' . $this->getTimeout() . 's) reached'); } if (!isset($result[0])) { throw new FormatException('Invalid response - no response type', $result); } if (!isset($result[1])) { throw new FormatException('Invalid response - no headers', $result); } $response = new Response($result[0]); $response->setHeaders(new Headers(Parser::parseHeaders($result[1]))); if (!MessageTypes::isResponseTypeWithResult($response->getType())) { return $response; } if (!isset($result[2])) { throw new FormatException('Invalid response - no response body', $result); } return $response->setResultBody($result[2]); }