Chris@0: drupalCreateUser(); Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: $csrf_token = $this->drupalGet($csrf_token_path); Chris@0: $url = Url::fromRoute($route_name) Chris@0: ->setAbsolute(TRUE) Chris@0: ->toString(); Chris@0: $domain = parse_url($url, PHP_URL_HOST); Chris@0: Chris@0: $session_id = $this->getSession()->getCookie($this->getSessionName()); Chris@0: /** @var \GuzzleHttp\Cookie\CookieJar $cookies */ Chris@0: $cookies = CookieJar::fromArray([$this->getSessionName() => $session_id], $domain); Chris@0: $post_options = [ Chris@0: 'headers' => ['Accept' => 'text/plain'], Chris@0: 'http_errors' => FALSE, Chris@0: ]; Chris@0: Chris@0: // Test that access is allowed for anonymous user with no token in header. Chris@0: $result = $client->post($url, $post_options); Chris@0: $this->assertEquals(200, $result->getStatusCode()); Chris@0: Chris@0: // Add cookies to POST options so that all other requests are for the Chris@0: // authenticated user. Chris@0: $post_options['cookies'] = $cookies; Chris@0: Chris@0: // Test that access is denied with no token in header. Chris@0: $result = $client->post($url, $post_options); Chris@0: $this->assertEquals(403, $result->getStatusCode()); Chris@0: Chris@0: // Test that access is allowed with correct token in header. Chris@0: $post_options['headers']['X-CSRF-Token'] = $csrf_token; Chris@0: $result = $client->post($url, $post_options); Chris@0: $this->assertEquals(200, $result->getStatusCode()); Chris@0: Chris@0: // Test that access is denied with incorrect token in header. Chris@0: $post_options['headers']['X-CSRF-Token'] = 'this-is-not-the-token-you-are-looking-for'; Chris@0: $result = $client->post($url, $post_options); Chris@0: $this->assertEquals(403, $result->getStatusCode()); Chris@0: } Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: }