Setting headers for CakePHP Controller unit tests

烂漫一生 提交于 2019-12-23 03:44:06

问题


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.

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

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


回答1:


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']);


来源:https://stackoverflow.com/questions/11405936/setting-headers-for-cakephp-controller-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!