Skip to content

Commit

Permalink
Merge pull request #31 from spatie/fix-body-issues
Browse files Browse the repository at this point in the history
Fix body data contents
  • Loading branch information
rubenvanassche authored Aug 1, 2024
2 parents 79c064e + 1b9de03 commit 180f8ca
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"spatie/phpunit-snapshot-assertions": "^4.0|^5.0",
"spatie/pest-plugin-snapshots": "^1.0|^2.0",
"pestphp/pest": "^1.20|^2.0"
},
"autoload": {
Expand Down
19 changes: 18 additions & 1 deletion src/Context/RequestContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use RuntimeException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Throwable;
Expand Down Expand Up @@ -138,11 +140,26 @@ public function getRequestData(): array
{
return [
'queryString' => $this->request->query->all(),
'body' => $this->request->request->all(),
'body' => $this->getInputBag()->all() + $this->request->query->all(),
'files' => $this->getFiles(),
];
}

protected function getInputBag(): InputBag|ParameterBag
{
$contentType = $this->request->headers->get('CONTENT_TYPE', 'text/html');

$isJson = str_contains($contentType, '/json') || str_contains($contentType, '+json');

if ($isJson) {
return new InputBag((array) json_decode($this->request->getContent(), true));
}

return in_array($this->request->getMethod(), ['GET', 'HEAD'])
? $this->request->query
: $this->request->request;
}

/** @return array<string, mixed> */
public function toArray(): array
{
Expand Down
6 changes: 2 additions & 4 deletions tests/Concerns/MatchesCodeSnippetSnapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
namespace Spatie\FlareClient\Tests\Concerns;

use Spatie\FlareClient\Tests\TestClasses\CodeSnippetDriver;
use Spatie\Snapshots\MatchesSnapshots;
use function Spatie\Snapshots\assertMatchesSnapshot;

trait MatchesCodeSnippetSnapshots
{
use MatchesSnapshots;

public function assertMatchesCodeSnippetSnapshot(array $codeSnippet)
{
$codeSnippet = $this->removeMicrotime($codeSnippet);
$codeSnippet = $this->removeTime($codeSnippet);

$this->assertMatchesSnapshot($codeSnippet, new CodeSnippetDriver());
assertMatchesSnapshot($codeSnippet, new CodeSnippetDriver());
}

private function removeMicrotime(array $codeSnippet): array
Expand Down
6 changes: 2 additions & 4 deletions tests/Concerns/MatchesDumpSnapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace Spatie\FlareClient\Tests\Concerns;

use Spatie\FlareClient\Tests\TestClasses\DumpDriver;
use Spatie\Snapshots\MatchesSnapshots;
use function Spatie\Snapshots\assertMatchesSnapshot;

trait MatchesDumpSnapshots
{
use MatchesSnapshots;

public function assertMatchesDumpSnapshot(array $codeSnippet)
{
$this->assertMatchesSnapshot($codeSnippet, new DumpDriver());
assertMatchesSnapshot($codeSnippet, new DumpDriver());
}
}
6 changes: 2 additions & 4 deletions tests/Concerns/MatchesReportSnapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace Spatie\FlareClient\Tests\Concerns;

use Spatie\FlareClient\Tests\TestClasses\ReportDriver;
use Spatie\Snapshots\MatchesSnapshots;
use function Spatie\Snapshots\assertMatchesSnapshot;

trait MatchesReportSnapshots
{
use MatchesSnapshots;

public function assertMatchesReportSnapshot(array $report)
{
$this->assertMatchesSnapshot($report, new ReportDriver());
assertMatchesSnapshot($report, new ReportDriver());
}
}
52 changes: 52 additions & 0 deletions tests/Context/RequestContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,55 @@

$this->assertMatchesCodeSnippetSnapshot($contextArray);
});

it('can retrieve the body contents of a json request', function () {
$content = '{"key": "value"}';

$server = [
'HTTP_CONTENT_TYPE' => 'application/json',
];

$request = new Request(server: $server, content: $content);

$context = new RequestContextProvider($request);

expect($context->toArray()['request_data']['body'])->toBe(['key' => 'value']);
});

it('will not crash when a json body is invalid', function () {
$content = 'SOME INVALID JSON';

$server = [
'HTTP_CONTENT_TYPE' => 'application/json',
];

$request = new Request(server: $server, content: $content);

$context = new RequestContextProvider($request);

expect($context->toArray()['request_data']['body'])->toBe([]);
});

it('can retrieve the body contents of a POST request', function () {
$post = ['key' => 'value'];

$server['REQUEST_METHOD'] = 'POST';

$request = new Request(request: $post, server: $server);

$context = new RequestContextProvider($request);

expect($context->toArray()['request_data']['body'])->toBe(['key' => 'value']);
});

it('can retrieve the body contents of a GET request', function () {
$query = ['key' => 'value'];

$server['REQUEST_METHOD'] = 'GET';

$request = new Request(query: $query, server: $server);

$context = new RequestContextProvider($request);

expect($context->toArray()['request_data']['body'])->toBe(['key' => 'value']);
});
2 changes: 2 additions & 0 deletions tests/FlareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
$_POST['user'] = '[email protected]';
$_POST['password'] = 'secret';

$_SERVER['REQUEST_METHOD'] = 'POST';

$this->flare->censorRequestBodyFields(['user', 'password']);

reportException();
Expand Down
1 change: 1 addition & 0 deletions tests/TestClasses/ReportDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected function removePhpunitArguments(array $data): array
protected function freezeLanguageVersion(array $data): array
{
data_set($data, 'language_version', '7.3.2', true);
data_set($data, 'context.env.php_version', '7.3.2', true);

return $data;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
notifier: 'Flare Client'
language: PHP
framework_version: null
language_version: 7.3.2
exception_class: PHPUnit\Framework\Exception
seen_at: 1546346096
message: 'This is a test'
glows: { }
solutions: { }
documentation_links: { }
stacktrace: { }
context:
arguments:
- '[phpunit arguments removed]'
env:
php_version: 7.3.2
stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ stacktrace: { }
context:
arguments:
- '[phpunit arguments removed]'
env:
php_version: 7.3.2
stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
uuid: fake-uuid
3 changes: 3 additions & 0 deletions tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ stacktrace: { }
context:
arguments:
- '[phpunit arguments removed]'
env:
php_version: 7.3.2
stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ stacktrace: { }
context:
arguments:
- '[phpunit arguments removed]'
env:
php_version: 7.3.2
stage: null
message_level: null
open_frame_index: 0
application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ stacktrace: { }
context:
arguments:
- '[phpunit arguments removed]'
env:
php_version: 7.3.2
stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
request:
url: 'http://example.com/test'
ip: 1.2.3.4
method: GET
useragent: null
request_data:
queryString:
get-key-1: get-value-1
body:
get-key-1: get-value-1
files:
file-one:
pathname: /tests/stubs/file.txt
size: 4
mimeType: text/plain
file-two:
pathname: /tests/stubs/file.txt
size: 4
mimeType: text/plain
headers:
host: example.com
cookies:
cookie-key-1: cookie-value-1
session: { }

0 comments on commit 180f8ca

Please sign in to comment.