function UpdateFetcherTest::testUpdateFetcherHttpFallback

Same name and namespace in other branches
  1. 9 core/modules/update/tests/src/Unit/UpdateFetcherTest.php \Drupal\Tests\update\Unit\UpdateFetcherTest::testUpdateFetcherHttpFallback()
  2. 11.x core/modules/update/tests/src/Unit/UpdateFetcherTest.php \Drupal\Tests\update\Unit\UpdateFetcherTest::testUpdateFetcherHttpFallback()

@covers ::doRequest @covers ::fetchProjectData

File

core/modules/update/tests/src/Unit/UpdateFetcherTest.php, line 208

Class

UpdateFetcherTest
Tests update functionality unrelated to the database.

Namespace

Drupal\Tests\update\Unit

Code

public function testUpdateFetcherHttpFallback() : void {
    $settings = new Settings([
        'update_fetch_with_http_fallback' => TRUE,
    ]);
    $this->mockClient(new Response(500, [], 'HTTPS failed'), new Response(200, [], 'HTTP worked'));
    $update_fetcher = new UpdateFetcher($this->mockConfigFactory, $this->mockHttpClient, $settings, $this->logger);
    $data = $update_fetcher->fetchProjectData($this->testProject, '');
    // There should be two request / response pairs.
    $this->assertCount(2, $this->history);
    // The first should have been HTTPS and should have failed.
    $first_try = $this->history[0];
    $this->assertNotEmpty($first_try);
    $this->assertEquals('https', $first_try['request']->getUri()
        ->getScheme());
    $this->assertEquals(500, $first_try['response']->getStatusCode());
    // The second should have been the HTTP fallback and should have worked.
    $second_try = $this->history[1];
    $this->assertNotEmpty($second_try);
    $this->assertEquals('http', $second_try['request']->getUri()
        ->getScheme());
    $this->assertEquals(200, $second_try['response']->getStatusCode());
    // Although this is a bogus mocked response, it's what fetchProjectData()
    // should return in this case.
    $this->assertEquals('HTTP worked', $data);
    $this->assertTrue($this->logger
        ->hasErrorThatPasses(function (array $record) {
        return $record['context']['@message'] === "Server error: `GET https://www.example.com/update_test/current` resulted in a `500 Internal Server Error` response:\nHTTPS failed\n";
    }));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.