fix: 提前读取响应数据造成数据错误的问题 (#634)

* fix: 提前读取响应数据造成数据错误的问题
This commit is contained in:
yansongda 2022-07-21 22:42:59 +08:00 committed by GitHub
parent 1ad9a4c09a
commit ac29721c5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 7 deletions

View File

@ -2,7 +2,7 @@
### fixed
- fix: 提前读取响应数据造成验签错误的问题(#633)
- fix: 提前读取响应数据造成数据错误的问题(#633, #634)
## v3.1.7

View File

@ -20,7 +20,7 @@ class ArrayParser implements ParserInterface
throw new InvalidResponseException(Exception::RESPONSE_NONE);
}
$contents = $response->getBody()->getContents();
$contents = (string) $response->getBody();
$result = json_decode($contents, true);

View File

@ -33,7 +33,7 @@ class CallbackPlugin implements PluginInterface
/* @phpstan-ignore-next-line */
verify_wechat_sign($rocket->getDestinationOrigin(), $rocket->getParams());
$body = json_decode($rocket->getDestination()->getBody()->getContents(), true);
$body = json_decode((string) $rocket->getDestination()->getBody(), true);
$rocket->setDirection(NoHttpRequestParser::class)->setPayload(new Collection($body));
@ -57,7 +57,7 @@ class CallbackPlugin implements PluginInterface
throw new InvalidParamsException(Exception::REQUEST_NULL_ERROR);
}
$contents = $request->getBody()->getContents();
$contents = (string) $request->getBody();
$rocket->setDestination($request->withBody(Utils::streamFor($contents)))
->setDestinationOrigin($request->withBody(Utils::streamFor($contents)))

View File

@ -100,7 +100,7 @@ abstract class AbstractProvider implements ProviderInterface
try {
$response = $http->sendRequest($rocket->getRadar());
$contents = $response->getBody()->getContents();
$contents = (string) $response->getBody();
$rocket->setDestination($response->withBody(Utils::streamFor($contents)))
->setDestinationOrigin($response->withBody(Utils::streamFor($contents)));

View File

@ -21,7 +21,7 @@ class Request extends \GuzzleHttp\Psr7\Request implements JsonSerializableInterf
'url' => $this->getUri()->__toString(),
'method' => $this->getMethod(),
'headers' => $this->getHeaders(),
'body' => $this->getBody()->getContents(),
'body' => (string) $this->getBody(),
];
}
}

View File

@ -38,4 +38,16 @@ class ArrayParserTest extends TestCase
self::assertEquals(['name' => 'yansongda'], $result);
}
public function testReadContents()
{
$response = new Response(200, [], '{"name": "yansongda"}');
$response->getBody()->read(2);
$parser = new ArrayParser();
$result = $parser->parse($response);
self::assertEquals(['name' => 'yansongda'], $result);
}
}

View File

@ -70,7 +70,26 @@ class AbstractProviderTest extends TestCase
$provider = new FooProviderStub();
$result = $provider->ignite($rocket);
self::assertEquals('yansongda/pay', $result->getDestination()->getBody()->getContents());
self::assertEquals('yansongda/pay', (string) $result->getDestination()->getBody());
}
public function testIgnitePreRead()
{
$response = new Response(200, [], 'yansongda/pay');
$response->getBody()->read(1);
$rocket = new Rocket();
$rocket->setRadar(new Request('get', ''));
$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn($response);
Pay::set(HttpClientInterface::class, $http);
$provider = new FooProviderStub();
$result = $provider->ignite($rocket);
self::assertEquals('yansongda/pay', (string) $result->getDestination()->getBody());
}
public function testIgniteWrongHttpClient()
@ -117,6 +136,11 @@ class FooProviderStub extends AbstractProvider
public function success(): ResponseInterface
{
return new Response(
200,
['Content-Type' => 'application/json'],
json_encode(['code' => 'SUCCESS', 'message' => '成功']),
);
}
public function mergeCommonPlugins(array $plugins): array