Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
8824b867
Commit
8824b867
authored
Aug 11, 2016
by
alexpott
Browse files
Issue
#2759863
by klausi: Implement request header support for drupalGet() on BrowserTestBase
parent
27b2c4ff
Changes
4
Hide whitespace changes
Inline
Side-by-side
core/modules/simpletest/tests/src/Functional/BrowserTestBaseTest.php
View file @
8824b867
...
...
@@ -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
);
}
/**
...
...
core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
View file @
8824b867
...
...
@@ -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
;
}
}
core/modules/system/tests/modules/system_test/system_test.routing.yml
View file @
8824b867
...
...
@@ -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'
core/tests/Drupal/Tests/BrowserTestBase.php
View file @
8824b867
...
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment