Skip to content

Commit

Permalink
Updated HttpDataSource for amphp/http-client 5/Beta 11 parity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jun 16, 2023
1 parent 20d08fe commit aa0a4d5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prefer-stable": true,
"require": {
"php": "^8.1",
"amphp/http-client": "^5.0.0-beta.7",
"amphp/http-client": "^5.0.0-beta.11",
"amphp/http-client-cookies": "^2.0.0-beta.1",
"scriptfusion/porter": "^7"
},
Expand Down
13 changes: 7 additions & 6 deletions src/HttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

namespace ScriptFUSION\Porter\Net\Http;

use Amp\Http\Client\RequestBody;
use Amp\Http\Client\BufferedContent;
use Amp\Http\Client\HttpContent;
use ScriptFUSION\Porter\Connector\DataSource;

final class HttpDataSource implements DataSource
{
private string $method = 'GET';
private ?RequestBody $body = null;
private ?HttpContent $body = null;
private array $headers = [];

public function __construct(private readonly string $url)
Expand All @@ -18,7 +19,7 @@ public function __construct(private readonly string $url)

public function computeHash(): string
{
$body = $this->body?->createBodyStream()->read();
$body = $this->body?->getContent()->read();

return \md5("{$this->flattenHeaders()}$this->method$this->url$body", true);
}
Expand All @@ -40,14 +41,14 @@ public function setMethod(string $method): self
return $this;
}

public function getBody(): ?RequestBody
public function getBody(): ?HttpContent
{
return $this->body;
}

public function setBody(?RequestBody $body): self
public function setBody(HttpContent|string $body): self
{
$this->body = $body;
$this->body = is_string($body) ? BufferedContent::fromString($body) : $body;

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions test/Functional/HttpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testConnectionToLocalWebserver(): void
self::buildDataSource()
->setMethod('POST')
->addHeader($headerName = 'Foo', $headerValue = 'Bar')
->setBody(new StringBody($body = 'Baz'))
->setBody($body = 'Baz')
);
} finally {
$this->stopServer($server);
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testAsyncConnectionToLocalWebserver(): void
self::buildDataSource()
->setMethod('POST')
->addHeader($headerName = 'Foo', $headerValue = 'Bar')
->setBody(new StringBody($body = 'Baz'))
->setBody($body = 'Baz')
);
} finally {
$this->stopServer($server);
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/HttpDataSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testComputeHash(): void
self::assertSame($hash, $this->source->computeHash());

self::assertNotSame($hash, $hash = $this->source->setMethod('Alfa')->computeHash());
self::assertNotSame($hash, $hash = $this->source->setBody(new StringBody('Bravo'))->computeHash());
self::assertNotSame($hash, $hash = $this->source->setBody('Bravo')->computeHash());
self::assertNotSame($hash, $hash = $this->source->addHeader('Charlie', 'Delta')->computeHash());

self::assertSame($hash, $this->source->computeHash());
Expand Down

0 comments on commit aa0a4d5

Please sign in to comment.