我正在用Laravel 5
制作API,我正在用PHPUnit
测试它.我需要测试遗留功能的兼容性,这是一个XML POST.到目前为止,我的第一个测试看起来是这样的:
public function testPostMessages()
{
$response = $this->call('POST', 'xml');
$this->assertEquals(200, $response->getStatusCode());
}
这很好.我列表中的下一步实际上是随帖子一起发送XML数据.所以在我的第二次测试中,我有:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml');
$this->assertTrue(is_valid_xml($response->getContent()));
}
This test fails. However, I am not sending my XML data. How do I send my XML?
Now, after that I need to add in the functionality to get the content from the request. I know there is a Request
object that I can interact with but I don't exactly know which method to call on it. How do I get my XML from inside my controller?
更新#1
同时我也得到了一些结果.我目前的测试是这样的:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
$this->assertContains('~', $response->getContent());
}
我只有瓷砖,因为我知道它不匹配,所以我可以看到整个react .在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()
.
更新#2
我将我的控制器更新为使用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 "~".
更新#3
我根本无法获取HTTP请求的内容体.由于XML不起作用,我继续使用API的其余部分,它使用JSON.以下是我的一个测试:
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!'
,这太棒了,但是如果我try 在独立的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头的正文内容.在 comments 了我的验证之后,我得到:
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!
So I have my 'saved!'
message. Great! But, in my database now I have a blank row. Not great. It is still not seeing the body content, just the headers.
回答标准
我在寻找这些问题的答案:
- 为什么我不能得到我请求的正文内容?
- 如何获取请求的正文内容?