Psr17Factory example

use Symfony\Contracts\HttpClient\Test\TestHttpServer;

class Psr18ClientTest extends TestCase
{
    public static function setUpBeforeClass(): void
    {
        TestHttpServer::start();
    }

    public function testSendRequest()
    {
        $factory = new Psr17Factory();
        $client = new Psr18Client(new NativeHttpClient()$factory$factory);

        $response = $client->sendRequest($factory->createRequest('GET', 'http://localhost:8057'));

        $this->assertSame(200, $response->getStatusCode());
        $this->assertSame('application/json', $response->getHeaderLine('content-type'));

        $body = json_decode((string) $response->getBody(), true);

        $this->assertSame('HTTP/1.1', $body['SERVER_PROTOCOL']);
    }

    
'CONTENT_TYPE' => 'application/json',
        ];
        $request = new Request([][][][][]$headers, '{"city":"Paris"');
        $psrRequest = self::buildHttpMessageFactory()->createRequest($request);

        $this->assertNull($psrRequest->getParsedBody());
    }

    public static function provideFactories(): \Generator
    {
        yield 'Discovery' => [new PsrHttpFactory()];
        yield 'incomplete dependencies' => [new PsrHttpFactory(responseFactory: new Psr17Factory())];
        yield 'Nyholm' => [self::buildHttpMessageFactory()];
    }

    private static function buildHttpMessageFactory(): PsrHttpFactory
    {
        $factory = new Psr17Factory();

        return new PsrHttpFactory($factory$factory$factory$factory);
    }
}
private HttpClientInterface $client;
    private ResponseFactoryInterface $responseFactory;
    private StreamFactoryInterface $streamFactory;

    public function __construct(HttpClientInterface $client = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null)
    {
        $this->client = $client ?? HttpClient::create();
        $streamFactory ??= $responseFactory instanceof StreamFactoryInterface ? $responseFactory : null;

        if (null === $responseFactory || null === $streamFactory) {
            if (class_exists(Psr17Factory::class)) {
                $psr17Factory = new Psr17Factory();
            } elseif (class_exists(NyholmPsr17Factory::class)) {
                $psr17Factory = new NyholmPsr17Factory();
            } else {
                throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".');
            }

            $responseFactory ??= $psr17Factory;
            $streamFactory ??= $psr17Factory;
        }

        $this->responseFactory = $responseFactory;
        
private HttplugWaitLoop $waitLoop;

    public function __construct(HttpClientInterface $client = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null)
    {
        $this->client = $client ?? HttpClient::create();
        $streamFactory ??= $responseFactory instanceof StreamFactoryInterface ? $responseFactory : null;
        $this->promisePool = class_exists(Utils::class) ? new \SplObjectStorage() : null;

        if (null === $responseFactory || null === $streamFactory) {
            if (class_exists(Psr17Factory::class)) {
                $psr17Factory = new Psr17Factory();
            } elseif (class_exists(NyholmPsr17Factory::class)) {
                $psr17Factory = new NyholmPsr17Factory();
            } else {
                throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".');
            }

            $responseFactory ??= $psr17Factory;
            $streamFactory ??= $psr17Factory;
        }

        $this->responseFactory = $responseFactory;
        


        $psr7Requests = [
            (new Psr7Request('POST', 'http://tnyholm.se/foo/?bar=biz'))
                ->withQueryParams(['bar' => 'biz']),
            new Psr7Request('GET', 'https://hey-octave.com/'),
            new Psr7Request('GET', 'https://hey-octave.com:443/'),
            new Psr7Request('GET', 'https://hey-octave.com:4242/'),
            new Psr7Request('GET', 'http://hey-octave.com:80/'),
        ];

        $nyholmFactory = new Psr17Factory();
        $psr17Factory = new PsrHttpFactory($nyholmFactory$nyholmFactory$nyholmFactory$nyholmFactory);
        $symfonyFactory = new HttpFoundationFactory();

        return array_merge([
            [$sfRequest$psr17Factory$symfonyFactory],
        ]array_map(function D$psr7Request) use ($symfonyFactory$psr17Factory) {
            return [$psr7Request$symfonyFactory$psr17Factory];
        }$psr7Requests));
    }

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