Skip to content

Commit

Permalink
support query params on GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell committed Aug 9, 2024
1 parent 75aca30 commit aa33d20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,12 @@ public function send(): Response
public function getUrl(): string
{
$url = (string) Str::of($this->path())
->when(
!empty($this->query),
fn (Stringable $path): Stringable => $path->append('?', http_build_query($this->query))
);
if(Str::of($this->method)->upper()->contains('GET','HEAD')){
->when(
! empty($this->query),
fn (Stringable $path): Stringable => $path->append('?', http_build_query($this->query))
);

if (Str::of($this->method)->upper()->contains('HEAD')) {
return $this->path();
}
return $url;
Expand Down
16 changes: 12 additions & 4 deletions tests/Feature/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
});

it('can add query parameters', function () {
$response = TestRequest::fake()->setPath(
$request = TestRequest::fake()->setPath(
path: '/comments',
)->withQuery(
query: [
Expand Down Expand Up @@ -261,7 +261,9 @@
'email' => '[email protected]',
'body' => "quia molestiae reprehenderit quasi aspernatur\naut expedita occaecati aliquam eveniet laudantium\nomnis quibusdam delectus saepe quia accusamus maiores nam est\ncum et ducimus et vero voluptates excepturi deleniti ratione",
],
])->send();
]);

$response = $request->send();

expect(
$response->json()
Expand All @@ -270,6 +272,8 @@
foreach ($response->json() as $item) {
expect($item['postId'])->toBe(1);
}

expect($request->getUrl())->toBe('/comments?postId=1');
});

it('can add data to the request', function () {
Expand Down Expand Up @@ -370,7 +374,7 @@
});

it('can add query parameters recursively without overwriting', function () {
$query = TestRequest::fake()
$request = TestRequest::fake()
->withQuery(
query: [
'postId' => 1,
Expand All @@ -387,7 +391,9 @@
'size' => 30,
],
],
)->getQuery();
);

$query = $request->getQuery();

expect(
$query
Expand All @@ -404,6 +410,8 @@
expect(
$query['page']['size']
)->toBe(30);

expect($request->getUrl())->toBe('/todos?postId=1&page%5Bnumber%5D=2&page%5Bsize%5D=30');
});

it('can get the request payload', function () {
Expand Down

0 comments on commit aa33d20

Please sign in to comment.