You are here

protected function UserLoginHttpTest::loginRequest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserLoginHttpTest.php \Drupal\Tests\user\Functional\UserLoginHttpTest::loginRequest()
  2. 9 core/modules/user/tests/src/Functional/UserLoginHttpTest.php \Drupal\Tests\user\Functional\UserLoginHttpTest::loginRequest()

Executes a login HTTP request for a given serialization format.

Parameters

string $name: The username.

string $pass: The user password.

string $format: The format to use to make the request.

Return value

\Psr\Http\Message\ResponseInterface The HTTP response.

File

core/modules/user/tests/src/Functional/UserLoginHttpTest.php, line 76

Class

UserLoginHttpTest
Tests login and password reset via direct HTTP.

Namespace

Drupal\Tests\user\Functional

Code

protected function loginRequest($name, $pass, $format = 'json') {
  $user_login_url = Url::fromRoute('user.login.http')
    ->setRouteParameter('_format', $format)
    ->setAbsolute();
  $request_body = [];
  if (isset($name)) {
    $request_body['name'] = $name;
  }
  if (isset($pass)) {
    $request_body['pass'] = $pass;
  }
  $result = \Drupal::httpClient()
    ->post($user_login_url
    ->toString(), [
    'body' => $this->serializer
      ->encode($request_body, $format),
    'headers' => [
      'Accept' => "application/{$format}",
    ],
    'http_errors' => FALSE,
    'cookies' => $this->cookies,
  ]);
  return $result;
}