Skip to content

Commit 1830bf8

Browse files
authored
Merge pull request #377 from guanguans/v3
Feature | Added dataUrl for Response
2 parents e74c30a + e4ad21a commit 1830bf8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Http/Response.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ public function dom(): Crawler
333333
return new Crawler($this->body());
334334
}
335335

336+
/**
337+
* Convert the response to a data URL
338+
*/
339+
public function dataUrl(): string
340+
{
341+
return 'data:'.$this->psrResponse->getHeaderLine('Content-Type').';base64,'.base64_encode($this->body());
342+
}
343+
336344
/**
337345
* Determine if the request was successful.
338346
*/

tests/Unit/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@
305305
expect($response->object())->toEqual((object)['foo' => 'bar']);
306306
});
307307

308+
test('it can convert the response to a data url', function () {
309+
$mockClient = new MockClient([
310+
MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json;encoding=utf-8']),
311+
]);
312+
313+
$response = connector()->send(new UserRequest, $mockClient);
314+
315+
expect($response->dataUrl())->toEqual('data:application/json;encoding=utf-8;base64,eyJmb28iOiJiYXIifQ==');
316+
});
317+
308318
test('if a response is changed through middleware the new instance is used', function () {
309319
$mockClient = new MockClient([
310320
MockResponse::make(['foo' => 'bar'], 200, ['X-Custom-Header' => 'Howdy']),

0 commit comments

Comments
 (0)