diff --git a/src/Watchers/ClientRequestWatcher.php b/src/Watchers/ClientRequestWatcher.php index 576b7843f..44de385e6 100644 --- a/src/Watchers/ClientRequestWatcher.php +++ b/src/Watchers/ClientRequestWatcher.php @@ -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 { @@ -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', diff --git a/tests/Watchers/ClientRequestWatcherTest.php b/tests/Watchers/ClientRequestWatcherTest.php index 7a1274136..f1ec50522 100644 --- a/tests/Watchers/ClientRequestWatcherTest.php +++ b/tests/Watchers/ClientRequestWatcherTest.php @@ -175,7 +175,7 @@ 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), @@ -183,9 +183,64 @@ public function test_client_request_watcher_handles_file_uploads() $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();