diff --git a/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php b/core/modules/page_cache/tests/src/Functional/PageCacheTagsIntegrationTest.php similarity index 96% rename from core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php rename to core/modules/page_cache/tests/src/Functional/PageCacheTagsIntegrationTest.php index 6ec8a29cf6..fc27a1b64c 100644 --- a/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php +++ b/core/modules/page_cache/tests/src/Functional/PageCacheTagsIntegrationTest.php @@ -1,22 +1,22 @@ setHttpResponseDebugCacheabilityHeaders(FALSE); + // Disable the cacheability headers. + $this->setContainerParameter('http.response.debug_cacheability_headers', FALSE); + $this->rebuildContainer(); + $this->resetAll(); $path = 'system-test/cache_tags_page'; $tags = ['system_test_cache_tags_page']; @@ -190,28 +196,28 @@ public function testConditionalRequests() { $etag = $this->drupalGetHeader('ETag'); $last_modified = $this->drupalGetHeader('Last-Modified'); - $this->drupalGet('', [], ['If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag]); + $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => $etag]); $this->assertResponse(304, 'Conditional request returned 304 Not Modified.'); - $this->drupalGet('', [], ['If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag]); + $this->drupalGet('', [], ['If-Modified-Since' => gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match' => $etag]); $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'); - $this->drupalGet('', [], ['If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag]); + $this->drupalGet('', [], ['If-Modified-Since' => gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match' => $etag]); $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'); - $this->drupalGet('', [], ['If-Modified-Since: ' . $last_modified]); + $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => NULL]); // Verify the page is not printed twice when the cache is warm. $this->assertNoPattern('#assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); - $this->drupalGet('', [], ['If-Modified-Since: ' . gmdate(DateTimePlus::RFC7231, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag]); + $this->drupalGet('', [], ['If-Modified-Since' => gmdate(DateTimePlus::RFC7231, strtotime($last_modified) + 1), 'If-None-Match' => $etag]); $this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $user = $this->drupalCreateUser(); $this->drupalLogin($user); - $this->drupalGet('', [], ['If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag]); + $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => $etag]); $this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.'); $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absence of Page was not cached.'); } @@ -515,24 +521,25 @@ public function testCacheableResponseResponses() { * Tests that HEAD requests are treated the same as GET requests. */ public function testHead() { + $client = $this->getSession()->getDriver()->getClient()->getClient(); // GET, then HEAD. $url_a = $this->buildUrl('system-test/set-header', ['query' => ['name' => 'Foo', 'value' => 'bar']]); - $response_body = $this->curlExec([CURLOPT_HTTPGET => TRUE, CURLOPT_URL => $url_a, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_NOBODY => FALSE]); + $response_body = $this->drupalGet($url_a); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); $this->assertEqual('The following header was set: Foo: bar', $response_body); - $response_body = $this->curlExec([CURLOPT_HTTPGET => FALSE, CURLOPT_URL => $url_a, CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => FALSE]); + $response_body = $client->request('HEAD', $url_a); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); $this->assertEqual('', $response_body); // HEAD, then GET. $url_b = $this->buildUrl('system-test/set-header', ['query' => ['name' => 'Foo', 'value' => 'baz']]); - $response_body = $this->curlExec([CURLOPT_HTTPGET => FALSE, CURLOPT_URL => $url_b, CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => FALSE]); + $response_body = $client->request('HEAD', $url_b); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'baz', 'Custom header was sent.'); $this->assertEqual('', $response_body); - $response_body = $this->curlExec([CURLOPT_HTTPGET => TRUE, CURLOPT_URL => $url_b, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_NOBODY => FALSE]); + $response_body = $this->drupalGet($url_b); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'baz', 'Custom header was sent.'); $this->assertEqual('The following header was set: Foo: baz', $response_body); diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index bd50c23b48..cb793746b0 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -1419,4 +1419,44 @@ protected function checkForMetaRefresh() { return FALSE; } + /** + * Retrieves only the headers for a Drupal path or an absolute path. + * + * @param \Drupal\Core\Url|string $path + * Drupal path or URL to load into internal browser. + * @param array $options + * Options to be forwarded to the url generator. + * @param array $headers + * An array containing additional HTTP request headers, each formatted as + * "name: value". + * + * @return array + * The retrieved headers, also available as $this->getRawContent() + */ + protected function drupalHead($path, array $options = [], array $headers = []) { + $options['absolute'] = TRUE; + $url = $this->buildUrl($path, $options); + $session = $this->getSession(); + $this->prepareRequest(); + foreach ($headers as $header_name => $header_value) { + $session->setRequestHeader($header_name, $header_value); + } + $session->visit($url); + $out = $session->getResponseHeaders(); + + // Ensure that any changes to variables in the other thread are picked up. + $this->refreshVariables(); + + // Log only for JavascriptTestBase tests because for Goutte we log with + // ::getResponseLogHandler. + if ($this->htmlOutputEnabled && !($this->getSession()->getDriver() instanceof GoutteDriver)) { + $html_output = 'GET request to: ' . $url . + '
Ending URL: ' . $this->getSession()->getCurrentUrl(); + $html_output .= '
' . $out; + $html_output .= $this->getHtmlOutputHeaders(); + $this->htmlOutput($html_output); + } + return $out; + } + }