diff --git a/src/Request.php b/src/Request.php index a6a8fa7..990aecb 100644 --- a/src/Request.php +++ b/src/Request.php @@ -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; diff --git a/tests/Feature/RequestTest.php b/tests/Feature/RequestTest.php index 8107027..f0d0d98 100644 --- a/tests/Feature/RequestTest.php +++ b/tests/Feature/RequestTest.php @@ -233,7 +233,7 @@ }); it('can add query parameters', function () { - $response = TestRequest::fake()->setPath( + $request = TestRequest::fake()->setPath( path: '/comments', )->withQuery( query: [ @@ -261,7 +261,9 @@ 'email' => 'Nikita@garfield.biz', '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() @@ -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 () { @@ -370,7 +374,7 @@ }); it('can add query parameters recursively without overwriting', function () { - $query = TestRequest::fake() + $request = TestRequest::fake() ->withQuery( query: [ 'postId' => 1, @@ -387,7 +391,9 @@ 'size' => 30, ], ], - )->getQuery(); + ); + + $query = $request->getQuery(); expect( $query @@ -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 () {