当前位置: 首页 > news >正文

Symfony2博客应用程序教程:第四部分(续)-测试安全页

  • 原文出处:http://www.dobervich.com/2011/03/28/symfony2-blog-application-tutorial-part-v-2-testing-secure-pages/
  • 原文作者:Dustin Dobervich
  • 授权许可:创作共用协议
  • 翻译人员:FireHare
  • 校对人员:FireHare
  • 适用版本:Symfony 2
  • 文章状态:已校对

I just wanted to write a quick post illustrating how to use the http basic authentication mechanism to test secured pages. Since the testing framework does not support sessions at the moment, it is not possible to write tests using the form login mechanism. Because of this, we have to use http basic authentication to test our secure pages.
我只想快速写一篇文章说明如何使用HTTP基本认证机制来测试安全页面。因为测试框架目前不支持会话,因此不可以使用表单登录机制来编写测试。有鉴于此,我们不得不使用HTTP基本认证来测试我们的安全页面。

First, we must make changes to the application’s test environment. The config_test.yml file located in the app/config directory is where we put all of our test environment specific configuration. We need to override the security configuration we set up in the previous tutorial to use the http basic authentication mechanism. Open up the config_test.yml file and add the following.
首先,我们必须修改应用程序的测试环境。我们将我们测试环境的相关配置全部放入了位于app/config目录中的config_test.yml文件中。我们需要覆写在先前教程中设置的安全配置,以便使用HTTP基本认证机制。打开config_test.yml文件,并添加下列语句:


 
  1. ## Security Configuration 
  2. security: 
  3.     encoders: 
  4.         Symfony\Component\Security\Core\User\User: plaintext 
  5.  
  6.     providers: 
  7.         main: 
  8.             users: 
  9.                 john.doe: { password: admin, roles: ROLE_ADMIN } 
  10.  
  11.     firewalls: 
  12.         main: 
  13.             pattern:    /.* 
  14.             http_basic: true 
  15.             logout:     true 
  16.             security: true 
  17.             anonymous: true 

Here we have declared that we want to use http_basic authentication in the test environment firewall. We have also told symfony that we want to use a plaintext password encoder for our user. This allows us to specify the user’s password in plain text. Under the providers entry we have declared an in-memory user with a username of john.doe, a password of admin and having the role ROLE_ADMIN. We will supply these credentials in our request using server parameters.
在这里,我们在测试环境的防火墙中声明我们想使用http_basic认证。我们还告诉Symfony2我们想为我们的用户使用纯文本密码编码器。这样可以让我们用纯文本指定用户的密码。在提供器条目下,我们声明了一个用户名是john.doe的in-memory用户,密码是admin,并且拥有ROLE_ADMIN角色。我们将在我们的请求里使用服务器参数来提供这些参数。

Now open up the AdminControllerTest.php file located in the src/Company/BlogBundle/Tests/Controller folder. Here is the code for the test.
现在打开位于src/Company/BlogBundle/Tests/Controller文件夹中的AdminControllerTest.php文件,以下是测试代码。


 
  1. namespace Company\BlogBundle\Tests\Controller; 
  2.   
  3. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
  4.   
  5. class AdminControllerTest extends WebTestCase 
  6.     public function testIndex() 
  7.     { 
  8.         $client = $this->createClient(); 
  9.         $client->followRedirects(true); 
  10.   
  11.         // request the index action with invalid credentials 
  12.         $crawler = $client->request('GET''/admin/'array(), array(), 
  13.             array('PHP_AUTH_USER' => 'john.doe''PHP_AUTH_PW' => 'wrong_pass')); 
  14.   
  15.         $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
  16.   
  17.         // we should be redirected to the login page 
  18.         $this->assertTrue($crawler->filter('title:contains("Login")')->count() > 0); 
  19.   
  20.         // request the index action with valid credentials 
  21.         $crawler = $client->request('GET''/admin/'array(), array(), 
  22.             array('PHP_AUTH_USER' => 'john.doe''PHP_AUTH_PW' => 'admin')); 
  23.   
  24.         $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
  25.   
  26.         // check the title of the page matches the admin home page 
  27.         $this->assertTrue($crawler->filter('title:contains("Admin | Home")')->count() > 0); 
  28.   
  29.         // check that the logout link exists 
  30.         $this->assertTrue($crawler->filter('a:contains("Logout")')->count() > 0); 
  31.     } 

The code is fairly straightforward. You should be able to follow along with the comments and know what is going on. Two special server parameters are used to pass the user’s credentials to the application PHP_AUTH_USER and PHP_AUTH_PW.
代码非常简单。您应该能够根据注解明白是怎么回事。两个特定的服务器参数(PHP_AUTH_USERPHP_AUTH_PW)用于将用户的证书发送到应用程序。

You should now be setup to test all of your secured pages. I am still not sure what I will be posting about next. I have been out of town, so I have not had time to even think about it. I am hesitant to do a Form tutorial because of the proposed changes. I was thinking about maybe going over the container and writing a custom service. Let me know what you guys want. Until next time…
您现在应该做好测试您所有安全页面的设置。我一直不确定我下一篇文章要写什么。我不在家,所以我没有时间考虑这个。我很犹豫是改主意写一篇表单教程,还是按原计划写写容器和自定义服务?让我知道您需要什么。直到下一次...

相关文章:

  • 用vs2008打framework2.0的包
  • 网页的学习语言将仿佛使你生活更动人
  • 四级词汇(俞敏洪)-词根与词缀(二)
  • zBrow发布倒计时:对不起,让大家久等了
  • 常用公用用例设计-分页
  • JProfiler7发布,支持JDBC数据显示
  • 我在上海IT运维的日子
  • 【转】 cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
  • 老菜鸟苦战oracle asm
  • android的各种*.img 文件
  • HyperSnap 6捕获的视频图片都是一片漆黑
  • GDI+ 绘制自定义制表位位数的文本。
  • 数据结构~链表
  • ComboBox控件数据绑定
  • [转].NET 数字格式化:忽略末尾零
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • CSS盒模型深入
  • gcc介绍及安装
  • JS实现简单的MVC模式开发小游戏
  • MySQL用户中的%到底包不包括localhost?
  • Next.js之基础概念(二)
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • SQL 难点解决:记录的引用
  • vue--为什么data属性必须是一个函数
  • 程序员该如何有效的找工作?
  • 动态魔术使用DBMS_SQL
  • 分布式任务队列Celery
  • 关于for循环的简单归纳
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 思维导图—你不知道的JavaScript中卷
  • 小李飞刀:SQL题目刷起来!
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​2020 年大前端技术趋势解读
  • ​Spring Boot 分片上传文件
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • (1)SpringCloud 整合Python
  • (26)4.7 字符函数和字符串函数
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (亲测有效)解决windows11无法使用1500000波特率的问题
  • (算法)N皇后问题
  • (一)Dubbo快速入门、介绍、使用
  • ../depcomp: line 571: exec: g++: not found
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .net core使用ef 6
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET MAUI学习笔记——2.构建第一个程序_初级篇
  • .net wcf memory gates checking failed
  • .NET 中的轻量级线程安全
  • .net流程开发平台的一些难点(1)