Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow using Illuminate\Support\Uri on testing HTTP Requests #54038

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Cookie\CookieValuePrefix;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Uri;
use Illuminate\Testing\LoggedExceptionCollection;
use Illuminate\Testing\TestResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
Expand Down Expand Up @@ -355,7 +356,7 @@ public function withPrecognition()
/**
* Visit the given URI with a GET request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $headers
* @return \Illuminate\Testing\TestResponse
*/
Expand All @@ -370,7 +371,7 @@ public function get($uri, array $headers = [])
/**
* Visit the given URI with a GET request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $headers
* @param int $options
* @return \Illuminate\Testing\TestResponse
Expand All @@ -383,7 +384,7 @@ public function getJson($uri, array $headers = [], $options = 0)
/**
* Visit the given URI with a POST request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -399,7 +400,7 @@ public function post($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a POST request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -413,7 +414,7 @@ public function postJson($uri, array $data = [], array $headers = [], $options =
/**
* Visit the given URI with a PUT request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -429,7 +430,7 @@ public function put($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a PUT request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -443,7 +444,7 @@ public function putJson($uri, array $data = [], array $headers = [], $options =
/**
* Visit the given URI with a PATCH request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -459,7 +460,7 @@ public function patch($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a PATCH request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -473,7 +474,7 @@ public function patchJson($uri, array $data = [], array $headers = [], $options
/**
* Visit the given URI with a DELETE request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -489,7 +490,7 @@ public function delete($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with a DELETE request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -503,7 +504,7 @@ public function deleteJson($uri, array $data = [], array $headers = [], $options
/**
* Visit the given URI with an OPTIONS request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @return \Illuminate\Testing\TestResponse
Expand All @@ -520,7 +521,7 @@ public function options($uri, array $data = [], array $headers = [])
/**
* Visit the given URI with an OPTIONS request, expecting a JSON response.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand All @@ -534,7 +535,7 @@ public function optionsJson($uri, array $data = [], array $headers = [], $option
/**
* Visit the given URI with a HEAD request.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $headers
* @return \Illuminate\Testing\TestResponse
*/
Expand All @@ -551,7 +552,7 @@ public function head($uri, array $headers = [])
* Call the given URI with a JSON request.
*
* @param string $method
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $data
* @param array $headers
* @param int $options
Expand Down Expand Up @@ -584,7 +585,7 @@ public function json($method, $uri, array $data = [], array $headers = [], $opti
* Call the given URI and return the Response.
*
* @param string $method
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @param array $parameters
* @param array $cookies
* @param array $files
Expand Down Expand Up @@ -619,11 +620,13 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []
/**
* Turn the given URI into a fully qualified URL.
*
* @param string $uri
* @param \Illuminate\Support\Uri|string $uri
* @return string
*/
protected function prepareUrlForRequest($uri)
{
$uri = $uri instanceof Uri ? $uri->value() : $uri;

if (str_starts_with($uri, '/')) {
$uri = substr($uri, 1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Testing\Concerns;

use Illuminate\Http\Request;
use Illuminate\Support\Uri;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;

#[WithConfig('app.key', 'base64:IUHRqAQ99pZ0A1MPjbuv1D6ff3jxv0GIvS2qIW4JNU4=')]
class MakeHttpRequestsTest extends TestCase
{
/** {@inheritDoc} */
protected function defineWebRoutes($router)
{
$router->get('decode', fn (Request $request) => [
'url' => $request->fullUrl(),
'query' => $request->query(),
]);
}

public function test_it_can_use_uri_to_make_request()
{
$this->getJson(Uri::of('decode')->withQuery(['editing' => true, 'editMode' => 'create', 'search' => 'Laravel']))
->assertSuccessful()
->assertJson([
'url' => 'http://localhost/decode?editMode=create&editing=1&search=Laravel',
'query' => [
'editing' => '1',
'editMode' => 'create',
'search' => 'Laravel',
],
]);
}
}