设置CakePHP控制器单元测试的标头 [英] Setting headers for CakePHP Controller unit tests

查看:111
本文介绍了设置CakePHP控制器单元测试的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为控制器编写一些单元测试,其中响应行为略有不同,取决于 $ this-> request->是('ajax')。我一直在查看测试控制器的文档 ,但我似乎没有找到一种方式来模拟通过AJAX的请求。

I'm writing some unit tests for a Controller in which the response behavior differs slightly, depending on whether $this->request->is('ajax'). I've been looking over the documentation for testing controllers, but I can't seem to find a way to simulate a request made via AJAX.

有一种方法发送头 testAction ),因此我可以设置X-Requested-With标头?

Is there a way to send headers to testAction() so I can set the X-Requested-With header?

编辑:您可以解决这个问题编辑superglobals。

You can work around this by editing the superglobals. Anyone have a less hacky solution?

推荐答案

一个解决方案是在测试期间手动声明必要的环境变量: / p>

One solution is to manually declare the necessary environment variable for the duration of your test:

$_ENV['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
// Run your AJAX test...
unset($_ENV['HTTP_X_REQUESTED_WITH']);

这篇关于设置CakePHP控制器单元测试的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆