簡體   English   中英

Symfony 4 使用 CSRF 令牌和 Session 的功能形式測試

[英]Symfony 4 Functional Form Test with CSRF Token and Session

我正在使用帶有表單包的 symfony 4.4 並想進行一些功能測試。

我的表單有多個步驟,我想執行一個完整的表單,直到出現成功消息。 但是使用csrf_protection:true我什至無法進入第 2 頁。如果我在測試環境中禁用它,我可以進入第 2 頁。如果我在第 2 頁轉儲我的 session,我可以看到它是空的。 這是我的測試示例:

$client = static::createClient();
$client->request('GET', '/test');
$crawler = $client->submitForm('Next', ['name' => 'Max Mustermann']); // => lands on step2
$crawler = $client->submitForm('Next', ['email' => 'asdf@ase.de']); // => lands back on step 1 with emtpy form. So session is empty

有誰知道這里出了什么問題? This page說,那必須工作,但事實並非如此。 https://symfony.com/doc/4.4/components/http_foundation/session_testing.html#functional-testing

我建議您從客戶的響應中檢索表單,然后提交包含數據的表單,例如:

// Get the crawler
$crawler = $client->request('GET', '/test');

// Get the form
$form = $crawler->filter('form')->form();

// Fill the form. NB: double check your form structure/fields name
$form['name'] = 'Max Mustermann';
$form['email'] = 'asdf@ase.de';

// Submit the form.
$crawler = $this->client->submit($form);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM