Commit 8824b867 authored by alexpott's avatar alexpott

Issue #2759863 by klausi: Implement request header support for drupalGet() on BrowserTestBase

parent 27b2c4ff
...@@ -17,7 +17,7 @@ class BrowserTestBaseTest extends BrowserTestBase { ...@@ -17,7 +17,7 @@ class BrowserTestBaseTest extends BrowserTestBase {
* *
* @var array * @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. * Tests basic page test.
...@@ -52,6 +52,13 @@ public function testGoTo() { ...@@ -52,6 +52,13 @@ public function testGoTo() {
// Test page contains some text. // Test page contains some text.
$this->assertSession()->pageTextContains('Hello Drupal'); $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);
} }
/** /**
......
...@@ -348,4 +348,16 @@ public function getCurrentDate() { ...@@ -348,4 +348,16 @@ public function getCurrentDate() {
return $response; 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;
}
} }
...@@ -175,3 +175,10 @@ system_test.always_denied: ...@@ -175,3 +175,10 @@ system_test.always_denied:
_controller: 'chop' _controller: 'chop'
requirements: requirements:
_access: 'FALSE' _access: 'FALSE'
system_test.header:
path: '/system-test/header'
defaults:
_controller: '\Drupal\system_test\Controller\SystemTestController::getTestHeader'
requirements:
_access: 'TRUE'
...@@ -655,17 +655,29 @@ protected function buildUrl($path, array $options = array()) { ...@@ -655,17 +655,29 @@ protected function buildUrl($path, array $options = array()) {
* Drupal path or URL to load into Mink controlled browser. * Drupal path or URL to load into Mink controlled browser.
* @param array $options * @param array $options
* (optional) Options to be forwarded to the url generator. * (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 * @return string
* The retrieved HTML string, also available as $this->getRawContent() * 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; $options['absolute'] = TRUE;
$url = $this->buildUrl($path, $options); $url = $this->buildUrl($path, $options);
$session = $this->getSession(); $session = $this->getSession();
$this->prepareRequest(); $this->prepareRequest();
foreach ($headers as $header_name => $header_value) {
$session->setRequestHeader($header_name, $header_value);
}
$session->visit($url); $session->visit($url);
$out = $session->getPage()->getContent(); $out = $session->getPage()->getContent();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment