ResponseStream example



        return new TraceableResponse($this->client, $this->client->request($method$url$options)$content$this->stopwatch?->start("$method $url", 'http_client'));
    }

    public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
    {
        if ($responses instanceof TraceableResponse) {
            $responses = [$responses];
        }

        return new ResponseStream(TraceableResponse::stream($this->client, $responses$timeout));
    }

    public function getTracedRequests(): array
    {
        return $this->tracedRequests->getArrayCopy();
    }

    public function reset(): void
    {
        if ($this->client instanceof ResetInterface) {
            $this->client->reset();
        }


        return new NativeResponse($this->multi, $contextimplode('', $url)$options$info$resolver$onProgress$this->logger);
    }

    public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
    {
        if ($responses instanceof NativeResponse) {
            $responses = [$responses];
        }

        return new ResponseStream(NativeResponse::stream($responses$timeout));
    }

    public function reset(): void
    {
        $this->multi->reset();
    }

    private static function getBodyAsString($body): string
    {
        if (\is_resource($body)) {
            return stream_get_contents($body);
        }


        return new AmpResponse($this->multi, $request$options$this->logger);
    }

    public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
    {
        if ($responses instanceof AmpResponse) {
            $responses = [$responses];
        }

        return new ResponseStream(AmpResponse::stream($responses$timeout));
    }

    public function reset(): void
    {
        $this->multi->dnsCache = [];

        foreach ($this->multi->pushedResponses as $authority => $pushedResponses) {
            foreach ($pushedResponses as [$pushedUrl$pushDeferred]) {
                $pushDeferred->fail(new CancelledException());

                $this->logger?->debug(sprintf('Unused pushed response: "%s"', $pushedUrl));
            }

        if ($responses instanceof CurlResponse) {
            $responses = [$responses];
        }

        if ($this->multi->handle instanceof \CurlMultiHandle) {
            $active = 0;
            while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->multi->handle, $active)) {
            }
        }

        return new ResponseStream(CurlResponse::stream($responses$timeout));
    }

    public function reset(): void
    {
        $this->multi->reset();
    }

    /** * Accepts pushed responses only if their headers related to authentication match the request. */
    private static function acceptPushForRequest(string $method, array $options, PushedResponse $pushedResponse): bool
    {


        return MockResponse::fromRequest($method$url$options$response);
    }

    public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
    {
        if ($responses instanceof ResponseInterface) {
            $responses = [$responses];
        }

        return new ResponseStream(MockResponse::stream($responses$timeout));
    }

    public function getRequestsCount(): int
    {
        return $this->requestsCount;
    }

    public function withOptions(array $options)static
    {
        $clone = clone $this;
        $clone->defaultOptions = self::mergeDefaultOptions($options$this->defaultOptions, true);

        
/** * @return AsyncResponse */
    abstract public function request(string $method, string $url, array $options = []): ResponseInterface;

    public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
    {
        if ($responses instanceof AsyncResponse) {
            $responses = [$responses];
        }

        return new ResponseStream(AsyncResponse::stream($responses$timeoutstatic::class));
    }
}
TXT;

        $chunk = new DataChunk(0, $data);
        $response = new MockResponse('', ['canceled' => false, 'http_method' => 'GET', 'url' => 'http://localhost:8080/events', 'response_headers' => ['content-type: text/event-stream']]);
        $responseStream = new ResponseStream((function D) use ($response$chunk) {
            yield $response => new FirstChunk();
            yield $response => $chunk;
            yield $response => new ErrorChunk(0, 'timeout');
        })());

        $hasCorrectHeaders = function D$options) {
            $this->assertSame(['Accept: text/event-stream', 'Cache-Control: no-cache']$options['headers']);

            return true;
        };

        
case 'testInformationalResponseStream':
                $client = $this->createMock(HttpClientInterface::class);
                $response = new MockResponse('Here the body', ['response_headers' => [
                    'HTTP/1.1 103 ',
                    'Link: </style.css>; rel=preload; as=style',
                    'HTTP/1.1 200 ',
                    'Date: foo',
                    'Content-Length: 13',
                ]]);
                $client->method('request')->willReturn($response);
                $client->method('stream')->willReturn(new ResponseStream((function D) use ($response) {
                    $chunk = $this->createMock(ChunkInterface::class);
                    $chunk->method('getInformationalStatus')
                        ->willReturn([103, ['link' => ['</style.css>; rel=preload; as=style', '</script.js>; rel=preload; as=script']]]);

                    yield $response => $chunk;

                    $chunk = $this->createMock(ChunkInterface::class);
                    $chunk->method('isFirst')->willReturn(true);

                    yield $response => $chunk;

                    
$mockResponses[] = $response;
            } else {
                $clientResponses[] = $response;
            }
        }

        if (!$mockResponses) {
            return $this->client->stream($clientResponses$timeout);
        }

        if (!$clientResponses) {
            return new ResponseStream(MockResponse::stream($mockResponses$timeout));
        }

        return new ResponseStream((function D) use ($mockResponses$clientResponses$timeout) {
            yield from MockResponse::stream($mockResponses$timeout);
            yield $this->client->stream($clientResponses$timeout);
        })());
    }

    /** * @return void */
    
Home | Imprint | This part of the site doesn't use cookies.