/** * @test */ public function continuesOnStatusCode102() { $httpResponse = $this->createResponse(Http::line('HTTP/1.0 102 Processing') . Http::line('Host: localhost') . Http::emptyLine() . Http::line('HTTP/1.0 102 Processing') . Http::emptyLine() . Http::line('HTTP/1.1 404 Not Found') . Http::emptyLine() . 'foobar'); $headerList = $httpResponse->headers(); assert($headerList->get('Host'), equals('localhost')); assert($httpResponse->statusLine(), equals('HTTP/1.1 404 Not Found')); assert($httpResponse->httpVersion(), equals(new HttpVersion(1, 1))); assert($httpResponse->statusCode(), equals(404)); assert($httpResponse->reasonPhrase(), equals('Not Found')); assert($httpResponse->statusCodeClass(), equals(Http::STATUS_CLASS_ERROR_CLIENT)); assert($httpResponse->body(), equals('foobar')); }
/** * @test * @since 4.0.0 */ public function linesConvertsAllLines() { assert(Http::lines('HEAD /foo/resource HTTP/1.1', 'Host: example.com', 'Connection: close', '', 'bodyline1', 'bodyline2'), equals(Http::line('HEAD /foo/resource HTTP/1.1') . Http::line('Host: example.com') . Http::line('Connection: close') . Http::emptyLine() . 'bodyline1' . 'bodyline2')); }
/** * helper method to send the headers * * @param \stubbles\peer\Stream $socket output stream to write request to * @param string $method http method * @param string|\stubbles\peer\http\HttpVersion $version http version * @throws \InvalidArgumentException */ private function processHeader(Stream $socket, string $method, $version) { $version = HttpVersion::castFrom($version); if (!$version->equals(HttpVersion::HTTP_1_0) && !$version->equals(HttpVersion::HTTP_1_1)) { throw new \InvalidArgumentException('Invalid HTTP version ' . $version . ', please use either ' . HttpVersion::HTTP_1_0 . ' or ' . HttpVersion::HTTP_1_1); } $path = $this->httpUri->path(); if ($this->httpUri->hasQueryString() && $this->methodAllowsQueryString($method)) { $path .= '?' . $this->httpUri->queryString(); } $socket->write(Http::line($method . ' ' . $path . ' ' . $version)); $socket->write(Http::line('Host: ' . $this->httpUri->hostname())); foreach ($this->headers as $key => $value) { $socket->write(Http::line($key . ': ' . $value)); } $socket->write(Http::emptyLine()); }