Skip to content

Commit

Permalink
Improve file handling in client request watcher (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdebrauwer authored Jun 18, 2021
1 parent d43db08 commit 4c14cba
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Watchers/ClientRequestWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Symfony\Component\HttpFoundation\File\File;

class ClientRequestWatcher extends Watcher
{
Expand Down Expand Up @@ -156,7 +157,19 @@ protected function input(Request $request)
}

return collect($request->data())->mapWithKeys(function ($data) {
if (! empty($data['filename']) || ! empty($data['headers'])) {
if ($data['contents'] instanceof File) {
$value = [
'name' => $data['filename'] ?? $data['contents']->getClientOriginalName(),
'size' => ($data['contents']->getSize() / 1000).'KB',
'headers' => $data['headers'] ?? [],
];
} elseif (is_resource($data['contents'])) {
$value = [
'name' => $data['filename'] ?? null,
'size' => (filesize(stream_get_meta_data($data['contents'])['uri']) / 1000).'KB',
'headers' => $data['headers'] ?? [],
];
} elseif (json_encode($data['contents']) === false) {
$value = [
'name' => $data['filename'] ?? null,
'size' => (strlen($data['contents']) / 1000).'KB',
Expand Down
63 changes: 59 additions & 4 deletions tests/Watchers/ClientRequestWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,72 @@ public function test_client_request_watcher_handles_multipart_request()
$this->assertSame(['firstname' => 'Taylor', 'lastname' => 'Otwell'], $entry->content['payload']);
}

public function test_client_request_watcher_handles_file_uploads()
public function test_client_request_watcher_handles_file_contents_upload()
{
Http::fake([
'*' => Http::response(null, 204),
]);

$image = UploadedFile::fake()->image('avatar.jpg');

Http::attach(
'image', file_get_contents($image), 'photo.jpg', ['foo' => 'bar']
)->post('https://laravel.com/fake-upload-file-route');
Http::attach('image', file_get_contents($image), 'photo.jpg', ['foo' => 'bar'])->post('https://laravel.com/fake-upload-file-route');

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::CLIENT_REQUEST, $entry->type);
$this->assertSame('POST', $entry->content['method']);
$this->assertSame('photo.jpg', $entry->content['payload']['image']['name']);
$this->assertSame(($image->getSize() / 1000).'KB', $entry->content['payload']['image']['size']);
$this->assertSame(['foo' => 'bar'], $entry->content['payload']['image']['headers']);
}

public function test_client_request_watcher_handles_file_contents_upload_without_explicit_filename_or_headers()
{
Http::fake([
'*' => Http::response(null, 204),
]);

$image = UploadedFile::fake()->image('avatar.jpg');

Http::attach('image', file_get_contents($image))->post('https://laravel.com/fake-upload-file-route');

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::CLIENT_REQUEST, $entry->type);
$this->assertSame('POST', $entry->content['method']);
$this->assertNull($entry->content['payload']['image']['name']);
$this->assertSame(($image->getSize() / 1000).'KB', $entry->content['payload']['image']['size']);
$this->assertSame([], $entry->content['payload']['image']['headers']);
}

public function test_client_request_watcher_handles_resource_file_upload()
{
Http::fake([
'*' => Http::response(null, 204),
]);

$image = UploadedFile::fake()->image('avatar.jpg');

Http::attach('image', $image->tempFile)->post('https://laravel.com/fake-upload-file-route');

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::CLIENT_REQUEST, $entry->type);
$this->assertSame('POST', $entry->content['method']);
$this->assertNull($entry->content['payload']['image']['name']);
$this->assertSame(($image->getSize() / 1000).'KB', $entry->content['payload']['image']['size']);
$this->assertSame([], $entry->content['payload']['image']['headers']);
}

public function test_client_request_watcher_handles_resource_file_upload_with_filename_and_headers()
{
Http::fake([
'*' => Http::response(null, 204),
]);

$image = UploadedFile::fake()->image('avatar.jpg');

Http::attach('image', $image->tempFile, 'photo.jpg', ['foo' => 'bar'])->post('https://laravel.com/fake-upload-file-route');

$entry = $this->loadTelescopeEntries()->first();

Expand Down

0 comments on commit 4c14cba

Please sign in to comment.