Skip to content

Commit ddd00b6

Browse files
committed
Update payload structure
1 parent d311665 commit ddd00b6

File tree

3 files changed

+55
-35
lines changed

3 files changed

+55
-35
lines changed

src/Records/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525
public readonly int $requestSize,
2626
public readonly int $responseSize,
2727
public HeaderBag $headers,
28-
public ?InputBag $payload,
28+
public InputBag $payload,
2929
public FileBag $files,
3030
) {
3131
//

src/Sensors/RequestSensor.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
use Laravel\Nightwatch\State\RequestState;
1515
use Laravel\Nightwatch\Types\Str;
1616
use Symfony\Component\HttpFoundation\BinaryFileResponse;
17-
use Symfony\Component\HttpFoundation\InputBag;
1817
use Symfony\Component\HttpFoundation\Response;
1918
use Throwable;
2019

2120
use function array_map;
2221
use function array_sum;
22+
use function assert;
2323
use function hash;
2424
use function implode;
2525
use function in_array;
@@ -102,7 +102,7 @@ public function __invoke(Request $request, Response $response): array
102102
$headers->remove('php-auth-pw');
103103
$headers->remove('php-auth-digest');
104104
}),
105-
payload: rescue(static fn () => $request->getPayload(), report: false),
105+
payload: clone $request->request,
106106
files: clone $request->files,
107107
),
108108
function () use ($record) {
@@ -161,16 +161,18 @@ static function ($e) {
161161
},
162162
),
163163
'body' => $this->captureBody && $record->statusCode === 500
164-
? Str::text(json_encode([
165-
'payload' => $record->payload instanceof InputBag ? $this->redactRecursively($record->payload->all()) : null,
166-
'files' => array_map(static fn (UploadedFile $file) => [
167-
'client_name' => $file->getClientOriginalName(),
168-
'client_mime_type' => $file->getClientMimeType(),
169-
'mime_type' => $file->getMimeType(),
170-
'size' => $file->getSize(),
171-
'path' => $file->getPathname(),
172-
], $record->files->all()),
173-
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT))
164+
? Str::text(rescue(
165+
fn () => json_encode([
166+
...$this->redactRecursively($record->payload->all()),
167+
'_nightwatch_files' => $this->mapUploadedFilesRecursively($record->files->all()),
168+
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION),
169+
'{"_nightwatch_error":"Failed to serialize body"}',
170+
static function ($e) {
171+
Nightwatch::unrecoverableExceptionOccurred($e);
172+
173+
return false;
174+
}
175+
))
174176
: '',
175177
];
176178
},
@@ -217,4 +219,27 @@ private function redactRecursively(array $array): array
217219
return ! in_array($key, $this->redactKeys, true) || ! is_string($value) ? $value : '['.strlen($value).' bytes redacted]';
218220
});
219221
}
222+
223+
/**
224+
* @param array<mixed> $files
225+
* @return array<mixed>
226+
*/
227+
private function mapUploadedFilesRecursively(array $files): array
228+
{
229+
return array_map(function ($file) {
230+
if (is_array($file)) {
231+
return $this->mapUploadedFilesRecursively($file);
232+
}
233+
234+
assert($file instanceof UploadedFile);
235+
236+
return [
237+
'client_name' => $file->getClientOriginalName(),
238+
'client_mime_type' => $file->getClientMimeType(),
239+
'mime_type' => $file->getMimeType(),
240+
'size' => $file->getSize(),
241+
'path' => $file->getPathname(),
242+
];
243+
}, $files);
244+
}
220245
}

tests/Feature/Sensors/RequestSensorTest.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use Orchestra\Testbench\Attributes\WithEnv;
2828
use Tests\TestCase;
2929

30-
use function array_keys;
3130
use function fseek;
3231
use function fwrite;
3332
use function hash;
@@ -948,34 +947,31 @@ public function test_it_captures_a_form_request_body_on_unhandled_exceptions():
948947
'user' => [
949948
'username' => 'taylor',
950949
'password' => '$f4c4d3',
950+
'avatar' => UploadedFile::fake()->create('avatar.jpg', 1, 'image/jpeg'),
951951
],
952-
'avatar' => UploadedFile::fake()->create('avatar.jpg', 1, 'image/jpeg'),
953952
]);
954953

955954
$response->assertInternalServerError();
956955
$ingest->assertWrittenTimes(1);
957956
$ingest->assertLatestWrite('request:0.body', function ($body) {
958957
$body = json_decode($body, true);
959-
$this->assertNotNull($body);
960958
$this->assertSame([
961959
'user' => [
962960
'username' => 'taylor',
963961
'password' => '[7 bytes redacted]',
964962
],
965-
], $body['payload']);
966-
$this->assertCount(1, $body['files']);
967-
$this->assertEqualsCanonicalizing([
968-
'client_name',
969-
'client_mime_type',
970-
'mime_type',
971-
'size',
972-
'path',
973-
], array_keys($body['files']['avatar']));
974-
$this->assertSame('avatar.jpg', $body['files']['avatar']['client_name']);
975-
$this->assertSame('image/jpeg', $body['files']['avatar']['client_mime_type']);
976-
$this->assertSame('image/jpeg', $body['files']['avatar']['mime_type']);
977-
$this->assertSame(1024, $body['files']['avatar']['size']);
978-
$this->assertFileExists($body['files']['avatar']['path']);
963+
'_nightwatch_files' => [
964+
'user' => [
965+
'avatar' => [
966+
'client_name' => 'avatar.jpg',
967+
'client_mime_type' => 'image/jpeg',
968+
'mime_type' => 'image/jpeg',
969+
'size' => 1024,
970+
'path' => $body['_nightwatch_files']['user']['avatar']['path'],
971+
],
972+
],
973+
],
974+
], $body);
979975

980976
return true;
981977
});
@@ -1001,14 +997,13 @@ public function test_it_captures_a_json_body_on_unhandled_exceptions(): void
1001997
$ingest->assertWrittenTimes(1);
1002998
$ingest->assertLatestWrite('request:0.body', function ($body) {
1003999
$body = json_decode($body, true);
1004-
$this->assertNotNull($body);
10051000
$this->assertSame([
10061001
'user' => [
10071002
'username' => 'taylor',
10081003
'password' => '[7 bytes redacted]',
10091004
],
1010-
], $body['payload']);
1011-
$this->assertCount(0, $body['files']);
1005+
'_nightwatch_files' => [],
1006+
], $body);
10121007

10131008
return true;
10141009
});
@@ -1036,14 +1031,14 @@ public function test_the_redacted_keys_can_be_customized(): void
10361031
$ingest->assertWrittenTimes(1);
10371032
$ingest->assertLatestWrite('request:0.body', function ($body) {
10381033
$body = json_decode($body, true);
1039-
$this->assertNotNull($body);
10401034
$this->assertSame([
10411035
'user' => [
10421036
'username' => 'taylor',
10431037
'password' => '$f4c4d3',
10441038
],
10451039
'foo' => '[3 bytes redacted]',
1046-
], $body['payload']);
1040+
'_nightwatch_files' => [],
1041+
], $body);
10471042

10481043
return true;
10491044
});

0 commit comments

Comments
 (0)