From 8824b86716d412d542648e1f6e17906e545a5748 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 11 Aug 2016 14:28:52 +0100
Subject: [PATCH] Issue #2759863 by klausi: Implement request header support
 for drupalGet() on BrowserTestBase

---
 .../tests/src/Functional/BrowserTestBaseTest.php   |  9 ++++++++-
 .../src/Controller/SystemTestController.php        | 12 ++++++++++++
 .../modules/system_test/system_test.routing.yml    |  7 +++++++
 core/tests/Drupal/Tests/BrowserTestBase.php        | 14 +++++++++++++-
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php b/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
index 4cd379abc2fc..5c1e3f546c33 100644
--- a/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
+++ b/core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
@@ -17,7 +17,7 @@ class BrowserTestBaseTest extends BrowserTestBase {
    *
    * @var array
    */
-  public static $modules = array('test_page_test', 'form_test');
+  public static $modules = array('test_page_test', 'form_test', 'system_test');
 
   /**
    * Tests basic page test.
@@ -52,6 +52,13 @@ public function testGoTo() {
 
     // Test page contains some text.
     $this->assertSession()->pageTextContains('Hello Drupal');
+
+    // Test that setting headers with drupalGet() works.
+    $this->drupalGet('system-test/header', array(), array(
+      'Test-Header' => 'header value',
+    ));
+    $returned_header = $this->getSession()->getResponseHeader('Test-Header');
+    $this->assertSame('header value', $returned_header);
   }
 
   /**
diff --git a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
index 9bf4fae13428..f2e138621e29 100644
--- a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
+++ b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
@@ -348,4 +348,16 @@ public function getCurrentDate() {
     return $response;
   }
 
+  /**
+   * Returns a response with a test header set from the request.
+   *
+   * @return \Symfony\Component\HttpFoundation\Response $response
+   *   A Response object containing the test header.
+   */
+  public function getTestHeader(Request $request) {
+    $response = new Response();
+    $response->headers->set('Test-Header', $request->headers->get('Test-Header'));
+    return $response;
+  }
+
 }
diff --git a/core/modules/system/tests/modules/system_test/system_test.routing.yml b/core/modules/system/tests/modules/system_test/system_test.routing.yml
index a91e83b1e3e4..f35f5462b314 100644
--- a/core/modules/system/tests/modules/system_test/system_test.routing.yml
+++ b/core/modules/system/tests/modules/system_test/system_test.routing.yml
@@ -175,3 +175,10 @@ system_test.always_denied:
     _controller: 'chop'
   requirements:
     _access: 'FALSE'
+
+system_test.header:
+  path: '/system-test/header'
+  defaults:
+    _controller: '\Drupal\system_test\Controller\SystemTestController::getTestHeader'
+  requirements:
+    _access: 'TRUE'
diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php
index 3767ad1b5b28..c8df47c7261e 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -655,17 +655,29 @@ protected function buildUrl($path, array $options = array()) {
    *   Drupal path or URL to load into Mink controlled browser.
    * @param array $options
    *   (optional) Options to be forwarded to the url generator.
+   * @param string[] $headers
+   *   An array containing additional HTTP request headers, the array keys are
+   *   the header names and the array values the header values. This is useful
+   *   to set for example the "Accept-Language" header for requesting the page
+   *   in a different language. Note that not all headers are supported, for
+   *   example the "Accept" header is always overridden by the browser. For
+   *   testing REST APIs it is recommended to directly use an HTTP client such
+   *   as Guzzle instead.
    *
    * @return string
    *   The retrieved HTML string, also available as $this->getRawContent()
    */
-  protected function drupalGet($path, array $options = array()) {
+  protected function drupalGet($path, array $options = array(), array $headers = array()) {
     $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->getPage()->getContent();
 
-- 
GitLab