Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
withBody example
$client
->
wait
(
)
;
$this
->
assertTrue
(
$successCallableCalled
, '$promise->then() should have been called.'
)
;
$this
->
assertFalse
(
$failureCallableCalled
, 'Failure callable should not be called when request is successful.'
)
;
}
public
function
testPostRequest
(
)
{
$client
=
new
HttplugClient
(
new
NativeHttpClient
(
)
)
;
$request
=
$client
->
createRequest
(
'POST', 'http://localhost:8057/post'
)
->
withBody
(
$client
->
createStream
(
'foo=0123456789'
)
)
;
$response
=
$client
->
sendRequest
(
$request
)
;
$body
=
json_decode
(
(string)
$response
->
getBody
(
)
, true
)
;
$this
->
assertSame
(
[
'foo' => '0123456789', 'REQUEST_METHOD' => 'POST'
]
,
$body
)
;
}
public
function
testNetworkException
(
)
{
$client
=
new
HttplugClient
(
new
NativeHttpClient
(
)
)
;
foreach
(
$symfonyRequest
->headers->
all
(
)
as
$name
=>
$value
)
{
try
{
$request
=
$request
->
withHeader
(
$name
,
$value
)
;
}
catch
(
\InvalidArgumentException
$e
)
{
// ignore invalid header
}
}
$body
=
$this
->streamFactory->
createStreamFromResource
(
$symfonyRequest
->
getContent
(
true
)
)
;
$request
=
$request
->
withBody
(
$body
)
->
withUploadedFiles
(
$this
->
getFiles
(
$symfonyRequest
->files->
all
(
)
)
)
->
withCookieParams
(
$symfonyRequest
->cookies->
all
(
)
)
->
withQueryParams
(
$symfonyRequest
->query->
all
(
)
)
->
withParsedBody
(
$symfonyRequest
->request->
all
(
)
)
;
foreach
(
$symfonyRequest
->attributes->
all
(
)
as
$key
=>
$value
)
{
$request
=
$request
->
withAttribute
(
$key
,
$value
)
;
}
return
$request
;
}
public
function
__construct
(
private
readonly ResponseFactoryInterface
$responseFactory
,
private
readonly StreamFactoryInterface
$streamFactory
,
)
{
}
public
function
serverRequestAction
(
ServerRequestInterface
$request
)
: ResponseInterface
{
return
$this
->responseFactory
->
createResponse
(
)
->
withBody
(
$this
->streamFactory->
createStream
(
sprintf
(
'<html><body>%s</body></html>',
$request
->
getMethod
(
)
)
)
)
;
}
public
function
requestAction
(
RequestInterface
$request
)
: ResponseInterface
{
return
$this
->responseFactory
->
createResponse
(
)
->
withStatus
(
403
)
->
withBody
(
$this
->streamFactory->
createStream
(
sprintf
(
'<html><body>%s %s</body></html>',
$request
->
getMethod
(
)
,
$request
->
getBody
(
)
->
getContents
(
)
)
)
)
;
}
public
function
messageAction
(
MessageInterface
$request
)
: ResponseInterface
{
}
}
}
$body
=
$response
instanceof StreamableInterface ?
$response
->
toStream
(
false
)
: StreamWrapper::
createResource
(
$response
,
$this
->client
)
;
$body
=
$this
->streamFactory->
createStreamFromResource
(
$body
)
;
if
(
$body
->
isSeekable
(
)
)
{
$body
->
seek
(
0
)
;
}
return
$psrResponse
->
withBody
(
$body
)
;
}
catch
(
TransportExceptionInterface
$e
)
{
if
(
$e
instanceof \InvalidArgumentException
)
{
throw
new
Psr18RequestException
(
$e
,
$request
)
;
}
throw
new
Psr18NetworkException
(
$e
,
$request
)
;
}
}
public
function
createRequest
(
string
$method
,
$uri
)
: RequestInterface
{
$request
=
$this
->responseFactory->
createRequest
(
$method
,
$uri
)
;
}
elseif
(
class_exists
(
Psr17FactoryDiscovery::
class
)
)
{
$request
= Psr17FactoryDiscovery::
findRequestFactory
(
)
->
createRequest
(
$method
,
$uri
)
;
}
elseif
(
class_exists
(
Request::
class
)
)
{
$request
=
new
Request
(
$method
,
$uri
)
;
}
else
{
throw
new
\
LogicException
(
sprintf
(
'You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__
)
)
;
}
$request
=
$request
->
withProtocolVersion
(
$protocolVersion
)
->
withBody
(
$this
->
createStream
(
$body
?? ''
)
)
;
foreach
(
$headers
as
$name
=>
$value
)
{
$request
=
$request
->
withAddedHeader
(
$name
,
$value
)
;
}
return
$request
;
}
/** * @param string $content */
$body
=
json_decode
(
(string)
$response
->
getBody
(
)
, true
)
;
$this
->
assertSame
(
'HTTP/1.1',
$body
[
'SERVER_PROTOCOL'
]
)
;
}
public
function
testPostRequest
(
)
{
$factory
=
new
Psr17Factory
(
)
;
$client
=
new
Psr18Client
(
new
NativeHttpClient
(
)
,
$factory
,
$factory
)
;
$request
=
$factory
->
createRequest
(
'POST', 'http://localhost:8057/post'
)
->
withBody
(
$factory
->
createStream
(
'foo=0123456789'
)
)
;
$response
=
$client
->
sendRequest
(
$request
)
;
$body
=
json_decode
(
(string)
$response
->
getBody
(
)
, true
)
;
$this
->
assertSame
(
[
'foo' => '0123456789', 'REQUEST_METHOD' => 'POST'
]
,
$body
)
;
}
public
function
testNetworkException
(
)
{
$factory
=
new
Psr17Factory
(
)
;
$client
=
new
Psr18Client
(
new
NativeHttpClient
(
)
,
$factory
,
$factory
)
;
if
(
'json' ===
$format
)
{
$parsedBody
=
json_decode
(
$symfonyRequest
->
getContent
(
)
, true, 512, \JSON_BIGINT_AS_STRING
)
;
if
(
!\
is_array
(
$parsedBody
)
)
{
$parsedBody
= null;
}
}
else
{
$parsedBody
=
$symfonyRequest
->request->
all
(
)
;
}
$request
=
$request
->
withBody
(
$body
)
->
withUploadedFiles
(
$this
->
getFiles
(
$symfonyRequest
->files->
all
(
)
)
)
->
withCookieParams
(
$symfonyRequest
->cookies->
all
(
)
)
->
withQueryParams
(
$symfonyRequest
->query->
all
(
)
)
->
withParsedBody
(
$parsedBody
)
;
foreach
(
$symfonyRequest
->attributes->
all
(
)
as
$key
=>
$value
)
{
$request
=
$request
->
withAttribute
(
$key
,
$value
)
;
}
return
$request
;
}
$body
=
$this
->streamFactory->
createStreamFromResource
(
$response
->
toStream
(
false
)
)
;
}
elseif
(
!
$buffer
)
{
$body
=
$this
->streamFactory->
createStreamFromResource
(
StreamWrapper::
createResource
(
$response
,
$this
->client
)
)
;
}
else
{
$body
=
$this
->streamFactory->
createStream
(
$response
->
getContent
(
false
)
)
;
}
if
(
$body
->
isSeekable
(
)
)
{
$body
->
seek
(
0
)
;
}
return
$psrResponse
->
withBody
(
$body
)
;
}
}