|
9 | 9 | use Laravel\Nightwatch\Concerns\RecordsContext; |
10 | 10 | use Laravel\Nightwatch\Concerns\RedactsHeaders; |
11 | 11 | use Laravel\Nightwatch\ExecutionStage; |
| 12 | +use Laravel\Nightwatch\Facades\Nightwatch; |
12 | 13 | use Laravel\Nightwatch\Records\Request as RequestRecord; |
13 | 14 | use Laravel\Nightwatch\State\RequestState; |
14 | 15 | use Laravel\Nightwatch\Types\Str; |
15 | 16 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
16 | | -use Symfony\Component\HttpFoundation\InputBag; |
17 | 17 | use Symfony\Component\HttpFoundation\Response; |
18 | 18 | use Throwable; |
19 | 19 |
|
20 | 20 | use function array_map; |
21 | 21 | use function array_sum; |
| 22 | +use function assert; |
22 | 23 | use function hash; |
23 | 24 | use function implode; |
24 | 25 | use function in_array; |
@@ -101,7 +102,7 @@ public function __invoke(Request $request, Response $response): array |
101 | 102 | $headers->remove('php-auth-pw'); |
102 | 103 | $headers->remove('php-auth-digest'); |
103 | 104 | }), |
104 | | - payload: rescue(static fn () => $request->getPayload(), report: false), |
| 105 | + payload: clone $request->request, |
105 | 106 | files: clone $request->files, |
106 | 107 | ), |
107 | 108 | function () use ($record) { |
@@ -152,16 +153,18 @@ function () use ($record) { |
152 | 153 | 'context' => $this->serializedContext(), |
153 | 154 | 'headers' => Str::text(json_encode((object) $this->redactHeaders($record->headers, $this->redactHeaders)->all(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION)), |
154 | 155 | 'body' => $this->captureBody && $record->statusCode === 500 |
155 | | - ? Str::text(json_encode([ |
156 | | - 'payload' => $record->payload instanceof InputBag ? $this->redactRecursively($record->payload->all()) : null, |
157 | | - 'files' => array_map(static fn (UploadedFile $file) => [ |
158 | | - 'client_name' => $file->getClientOriginalName(), |
159 | | - 'client_mime_type' => $file->getClientMimeType(), |
160 | | - 'mime_type' => $file->getMimeType(), |
161 | | - 'size' => $file->getSize(), |
162 | | - 'path' => $file->getPathname(), |
163 | | - ], $record->files->all()), |
164 | | - ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) |
| 156 | + ? Str::text(rescue( |
| 157 | + fn () => json_encode([ |
| 158 | + ...$this->redactRecursively($record->payload->all()), |
| 159 | + '_nightwatch_files' => $this->mapUploadedFilesRecursively($record->files->all()), |
| 160 | + ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION), |
| 161 | + '{"_nightwatch_error":"Failed to serialize body"}', |
| 162 | + static function ($e) { |
| 163 | + Nightwatch::unrecoverableExceptionOccurred($e); |
| 164 | + |
| 165 | + return false; |
| 166 | + } |
| 167 | + )) |
165 | 168 | : '', |
166 | 169 | ]; |
167 | 170 | }, |
@@ -208,4 +211,27 @@ private function redactRecursively(array $array): array |
208 | 211 | return ! in_array($key, $this->redactKeys, true) || ! is_string($value) ? $value : '['.strlen($value).' bytes redacted]'; |
209 | 212 | }); |
210 | 213 | } |
| 214 | + |
| 215 | + /** |
| 216 | + * @param array<mixed> $files |
| 217 | + * @return array<mixed> |
| 218 | + */ |
| 219 | + private function mapUploadedFilesRecursively(array $files): array |
| 220 | + { |
| 221 | + return array_map(function ($file) { |
| 222 | + if (is_array($file)) { |
| 223 | + return $this->mapUploadedFilesRecursively($file); |
| 224 | + } |
| 225 | + |
| 226 | + assert($file instanceof UploadedFile); |
| 227 | + |
| 228 | + return [ |
| 229 | + 'client_name' => $file->getClientOriginalName(), |
| 230 | + 'client_mime_type' => $file->getClientMimeType(), |
| 231 | + 'mime_type' => $file->getMimeType(), |
| 232 | + 'size' => $file->getSize(), |
| 233 | + 'path' => $file->getPathname(), |
| 234 | + ]; |
| 235 | + }, $files); |
| 236 | + } |
211 | 237 | } |
0 commit comments