TestHttpClientMiddleware should only set User-Agent for Drupal requests

Created on 27 April 2024, 9 months ago

Problem/Motivation

Discovered in 📌 Upgrade PHPUnit to 10, drop Symfony PHPUnit-bridge dependency Fixed , SecurityAdvisoryTest makes a request to updates.drupal.org at the end of the installer, and TestHttpClientMiddleware intercepts it and tries to add the Drupal test user agent. But at this point the site directory is not writeable and so writing the key fails - this triggers an PHP warning, which is converted to an exception by PHPUnit, and which (accidentally) prevents the key from being stored.

In PHPUnit 10 the warning is no longer converted and execution continues.

There are two problems here:

  1. TestHttpClientMiddleware should not be altering non-Drupal requests
  2. We should not be making requests to updates.drupal.org during tests

If we solve this correctly then the same solution should work on both PHPUnit 9 and 10.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
PHPUnit 

Last updated about 20 hours ago

Created by

🇬🇧United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @longwave
  • Status changed to Needs review 9 months ago
  • 🇬🇧United Kingdom longwave UK
  • Pipeline finished with Canceled
    9 months ago
    Total: 192s
    #158395
  • 🇬🇧United Kingdom longwave UK

    Actually, we can solve both problems here: explicitly throw an exception if we make a request to another host, and then we will never send the Drupal User-Agent to any other host anyway.

  • Pipeline finished with Failed
    9 months ago
    Total: 524s
    #158400
  • 🇫🇷France andypost

    Nice catch!

  • 🇬🇧United Kingdom longwave UK

    I imagine this is going to break contrib or custom tests that explicitly want to talk to third party services (remote search servers for example).

  • Pipeline finished with Failed
    9 months ago
    Total: 511s
    #158451
  • 🇮🇹Italy mondrake 🇮🇹
  • 🇬🇧United Kingdom catch

    This is flushing out a lot of http requests in core tests too. I'm not sure we can commit it even once those are fixed for the reasons given in #7, maybe we can add some kind of flag to enable the behaviour or not?

  • Pipeline finished with Failed
    9 months ago
    Total: 508s
    #159782
  • Pipeline finished with Failed
    9 months ago
    #159805
  • 🇬🇧United Kingdom longwave UK

    OEmbedTestTrait has what might be a better method:

      /**
       * Configures the HTTP client to always use the fixtures directory.
       *
       * All requests are carried out relative to the URL of the fixtures directory.
       * For example, after calling this method, a request for foobar.html will
       * actually request http://test-site/path/to/fixtures/foobar.html.
       */
      protected function lockHttpClientToFixtures() {
        $this->writeSettings([
          'settings' => [
            'http_client_config' => [
              'base_uri' => (object) [
                'value' => $this->getFixturesUrl() . '/',
                'required' => TRUE,
              ],
            ],
          ],
        ]);
    
  • Pipeline finished with Failed
    9 months ago
    Total: 564s
    #159834
  • Status changed to Needs work 9 months ago
  • 🇬🇧United Kingdom longwave UK

    This is acting differently on local vs GitLab CI and I have no idea why.

  • 🇮🇹Italy mondrake 🇮🇹

    Removing PHPUnit 10 tag, it's no longer strictly related to the PHPUnit upgrade.

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    If we block requests to anything other than the site under test we're going to break contrib and custom tests big time.

  • 🇬🇧United Kingdom catch

    I think it would fine if we only blocked it for core tests, but it would be good to 100% enforce it in core because it is easy to do this without realising - as we've found out.

  • First commit to issue fork.
  • Pipeline finished with Failed
    3 months ago
    Total: 328s
    #311585
Production build 0.71.5 2024