Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!7798
Only set User-Agent for requests to the correct host.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Only set User-Agent for requests to the correct host.
issue/drupal-3443894:3443894-testhttpclientmiddleware-should-only
into
11.x
Overview
1
Commits
10
Pipelines
8
Changes
6
1 unresolved thread
Hide all comments
Open
Dave Long
requested to merge
issue/drupal-3443894:3443894-testhttpclientmiddleware-should-only
into
11.x
1 year ago
Overview
1
Commits
10
Pipelines
8
Changes
6
1 unresolved thread
Hide all comments
Expand
Closes
#3443894
0
0
Merge request reports
Compare
11.x
version 7
5326e252
1 year ago
version 6
48727c25
1 year ago
version 5
a1ad0975
1 year ago
version 4
aa5dd5b2
1 year ago
version 3
11df624a
1 year ago
version 2
acec93d4
1 year ago
version 1
9dc78030
1 year ago
11.x (base)
and
latest version
latest version
630bf744
10 commits,
7 months ago
version 7
5326e252
9 commits,
1 year ago
version 6
48727c25
6 commits,
1 year ago
version 5
a1ad0975
4 commits,
1 year ago
version 4
aa5dd5b2
3 commits,
1 year ago
version 3
11df624a
2 commits,
1 year ago
version 2
acec93d4
1 commit,
1 year ago
version 1
9dc78030
1 commit,
1 year ago
6 files
+
52
−
3
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php
+
31
−
2
Options
@@ -7,12 +7,29 @@
use
Psr\Http\Message\ResponseInterface
;
/**
*
Overrid
es the
U
ser
-A
gent
HTTP header for outbound HTTP
requests.
*
Blocks unknown external hosts and replac
es the
u
ser
a
gent
for test
requests.
*/
class
TestHttpClientMiddleware
{
/**
* HTTP middleware that replaces the user agent for test requests.
* List of external host names that tests can make HTTP requests to.
*
* @var string[]
*/
protected
static
array
$allowedHosts
=
[
'ftp.drupal.org'
,
'oembed.com'
];
/**
* Adds a host name to the allow list for the remainder of this test run.
*
* @param string $host
* The hostname to allow.
*/
public
static
function
allowHost
(
string
$host
):
void
{
static
::
$allowedHosts
[]
=
$host
;
}
/**
* Block unknown external hosts and replace the user agent.
*/
public
function
__invoke
()
{
// If the database prefix is being used to run the tests in a copied
@@ -26,6 +43,18 @@ public function __invoke() {
if
(
$user_agent
=
drupal_generate_test_ua
(
drupal_valid_test_ua
()))
{
$request
=
$request
->
withHeader
(
'User-Agent'
,
$user_agent
);
}
// Allow specific hosts with no alterations.
if
(
in_array
(
$request
->
getUri
()
->
getHost
(),
static
::
$allowedHosts
,
TRUE
))
{
return
$handler
(
$request
,
$options
);
}
// Disallow other external hosts.
$host
=
parse_url
(
getenv
(
'SIMPLETEST_BASE_URL'
),
PHP_URL_HOST
);
if
(
$host
!==
$request
->
getUri
()
->
getHost
())
{
throw
new
\RuntimeException
(
sprintf
(
'Tests should only make requests to the SIMPLETEST_BASE_URL host of %s, but a request to %s was made.'
,
$host
,
$request
->
getUri
()
->
getHost
()));
}
return
$handler
(
$request
,
$options
)
->
then
(
function
(
ResponseInterface
$response
)
{
if
(
!
drupal_valid_test_ua
())
{
Loading