我正在使用制作API,Laravel 5
并且正在使用进行测试PHPUnit
。我需要测试旧功能的兼容性,这是XML POST。截至目前,我的第一个测试如下所示:
public function testPostMessages()
{
$response = $this->call('POST', 'xml');
$this->assertEquals(200, $response->getStatusCode());
}
这一切都很好。我列表上的下一个实际上是通过POST发送XML数据。因此,对于第二项测试,我有:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml');
$this->assertTrue(is_valid_xml($response->getContent()));
}
该测试失败。但是,我没有发送XML数据。如何发送XML?
现在,在那之后,我需要添加功能以从请求中获取内容。我知道有一个Request
可以与之交互的对象,但我不完全知道该调用哪种方法。如何从控制器内部获取XML?
同时,我能够得到一些结果。我当前的测试如下:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
$this->assertContains('~', $response->getContent());
}
我只有波浪号,因为我知道它不匹配,这样我就可以看到整个响应。在XmlController.php
我有:
class XmlController extends Controller {
public function index(Request $request)
{
return $request;
}
}
我的输出PHPUnit
如下:
1) XmlTest::testPostMessagesContent
Failed asserting that 'POST xml HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Content-Type: application/x-www-form-urlencoded
Host: localhost
User-Agent: Symfony/2.X
' contains "~".
我的参数和内容在哪里?我觉得我只是在call()
错误地使用。
我将控制器更新为Request:all()
如下所示:
class XmlController extends Controller
{
public function index()
{
$content = Request::all();
return $content;
}
}
这返回了我的键值对,但没有返回内容。
1) XmlTest::testPostMessagesContent
Failed asserting that '{"key":"value"}' contains "~".
键值对很好;这是进步。但是,我真正需要的是内容,因为我将在请求的内容部分中接收该数据。如果我使用,Request::getContent()
我会返回一个空白字符串。这是我在测试中的电话:
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
这是我的测试结果:
1) XmlTest::testPostMessagesContent
Failed asserting that '' contains "~".
我根本无法获得HTTP请求的内容主体。由于XML无法正常工作,因此我继续使用JSON的API的REST部分。这是我的测试之一:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'messages', ['content' => 'content']);
$this->assertEquals('saved!', $response->getContent());
}
该测试通过。如果使用curl,我也会接到一个成功的电话:
curl -X POST -d "content=my_new_content" "http://localhost:8000/messages"
返回'saved!'
的结果太棒了,但是,如果我尝试在独立的PHP脚本中使用curl(模拟客户端),则会返回以下内容:
Array ( [url] => http://localhost:8000/messages [content_type] => text/html; charset=UTF-8 [http_code] => 302 [header_size] => 603 [request_size] => 118 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.063977 [namelookup_time] => 0.000738 [connect_time] => 0.000866 [pretransfer_time] => 0.000943 [size_upload] => 12 [size_download] => 328 [speed_download] => 5126 [speed_upload] => 187 [download_content_length] => -1 [upload_content_length] => 12 [starttransfer_time] => 0.057606 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 8000 [local_ip] => ::1 [local_port] => 63248 [redirect_url] => http://localhost:8000 [request_header] => POST /messages HTTP/1.1 Host: localhost:8000 Accept: */* Content-type: text/xml Content-length: 12 ) Redirecting to http://localhost:8000.
这是我的curl命令,添加POST字段:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'content=test');
在我看来,POSTFIELDS已添加到请求的正文中。在这种情况下,我仍然有同样的问题。我无法获取HTTP标头的正文内容。注释掉我的验证后,我得到:
Array ( [url] => http://localhost:8000/messages [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 565 [request_size] => 118 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.070225 [namelookup_time] => 0.000867 [connect_time] => 0.00099 [pretransfer_time] => 0.001141 [size_upload] => 12 [size_download] => 6 [speed_download] => 85 [speed_upload] => 170 [download_content_length] => -1 [upload_content_length] => 12 [starttransfer_time] => 0.065204 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 8000 [local_ip] => ::1 [local_port] => 63257 [redirect_url] => [request_header] => POST /messages HTTP/1.1 Host: localhost:8000 Accept: */* Content-type: text/xml Content-length: 12 ) saved!
所以我有我的'saved!'
信息。伟大的!但是,现在我的数据库中有一个空白行。不是很好。它仍然没有看到正文内容,只是标题。
我正在寻找以下问题的答案:
内部控制器注入请求对象。因此,如果要访问控制器方法'foo'中的请求主体,请执行以下操作:
public function foo(Request $request){
$bodyContent = $request->getContent();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句