Skip to content

Commit

Permalink
Merge pull request #377 from guanguans/v3
Browse files Browse the repository at this point in the history
Feature | Added dataUrl for Response
  • Loading branch information
Sammyjo20 authored Feb 27, 2024
2 parents e74c30a + e4ad21a commit 1830bf8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ public function dom(): Crawler
return new Crawler($this->body());
}

/**
* Convert the response to a data URL
*/
public function dataUrl(): string
{
return 'data:'.$this->psrResponse->getHeaderLine('Content-Type').';base64,'.base64_encode($this->body());
}

/**
* Determine if the request was successful.
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@
expect($response->object())->toEqual((object)['foo' => 'bar']);
});

test('it can convert the response to a data url', function () {
$mockClient = new MockClient([
MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json;encoding=utf-8']),
]);

$response = connector()->send(new UserRequest, $mockClient);

expect($response->dataUrl())->toEqual('data:application/json;encoding=utf-8;base64,eyJmb28iOiJiYXIifQ==');
});

test('if a response is changed through middleware the new instance is used', function () {
$mockClient = new MockClient([
MockResponse::make(['foo' => 'bar'], 200, ['X-Custom-Header' => 'Howdy']),
Expand Down

0 comments on commit 1830bf8

Please sign in to comment.