diff --git a/CHANGELOG.md b/CHANGELOG.md index f1125d8..f2414e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### fixed -- fix: 提前读取响应数据造成验签错误的问题(#633) +- fix: 提前读取响应数据造成数据错误的问题(#633, #634) ## v3.1.7 diff --git a/src/Parser/ArrayParser.php b/src/Parser/ArrayParser.php index 7c1f5ea..687fee5 100644 --- a/src/Parser/ArrayParser.php +++ b/src/Parser/ArrayParser.php @@ -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); diff --git a/src/Plugin/Wechat/CallbackPlugin.php b/src/Plugin/Wechat/CallbackPlugin.php index ecc52da..bd25e5a 100644 --- a/src/Plugin/Wechat/CallbackPlugin.php +++ b/src/Plugin/Wechat/CallbackPlugin.php @@ -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))) diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 39e51d5..ad5a51c 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -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))); diff --git a/src/Request.php b/src/Request.php index 825db03..6628591 100644 --- a/src/Request.php +++ b/src/Request.php @@ -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(), ]; } } diff --git a/tests/Parser/ArrayParserTest.php b/tests/Parser/ArrayParserTest.php index f850e1d..069dd5c 100644 --- a/tests/Parser/ArrayParserTest.php +++ b/tests/Parser/ArrayParserTest.php @@ -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); + } } diff --git a/tests/Provider/AbstractProviderTest.php b/tests/Provider/AbstractProviderTest.php index d9d5c09..4da801e 100644 --- a/tests/Provider/AbstractProviderTest.php +++ b/tests/Provider/AbstractProviderTest.php @@ -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