问题描述
我正在使用 Laravel 5
制作 API,我正在使用 PHPUnit
对其进行测试.我需要测试遗留功能的兼容性,这是一个 XML POST.截至目前,我的第一个测试看起来像:
I am making an API with Laravel 5
and I'm testing it with PHPUnit
. I need to test legacy functionality for compatibility, which is an XML POST. As of right now, my first test looks like:
public function testPostMessages()
{
$response = $this->call('POST', 'xml');
$this->assertEquals(200, $response->getStatusCode());
}
这通过就好了.我的列表中的下一个实际上是使用 POST 发送 XML 数据.所以对于我的第二次测试,我有:
This is passing just fine. Next on my list is actually sending the XML data with the POST. So for my second test, I have:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml');
$this->assertTrue(is_valid_xml($response->getContent()));
}
此测试失败.但是,我没有发送我的 XML 数据.如何发送我的 XML?
现在,之后我需要添加功能以从请求中获取内容.我知道有一个我可以与之交互的 Request
对象,但我不知道该调用哪个方法.如何从控制器内部获取 XML?
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?
在此期间,我得到了一些结果.我目前的测试是这样的:
I was able to get some results in the meantime. My current test looks like this:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
$this->assertContains('~', $response->getContent());
}
我只有波浪号,因为我知道它不会匹配,所以我可以看到整个响应.在 XmlController.php
我有:
I only have the tilde there because I know it won't match so that I can see the whole response. In XmlController.php
I have:
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()
.
Where are my parameters and my content? I feel like I am simply just using call()
incorrectly.
我更新了我的控制器以使用 Request:all()
像这样:
I updated my controller to use Request:all()
like so:
class XmlController extends Controller
{
public function index()
{
$content = Request::all();
return $content;
}
}
这返回了我的键值对,但没有返回内容.
This returned my key-value pair, but not the content.
1) XmlTest::testPostMessagesContent
Failed asserting that '{"key":"value"}' contains "~".
键值对很好;这是进步.但是,我真正需要的是内容,因为我将在请求的内容部分接收该数据.如果我使用 Request::getContent()
我得到一个空白字符串.这是我在测试中的调用:
Key-value pairs are good; it's progress. However, what I really need is the content since I'll be receiving that data in the content portion of the request. If I use Request::getContent()
I get back a blank string. Here's my call in the test:
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
这是我的测试结果:
1) XmlTest::testPostMessagesContent
Failed asserting that '' contains "~".
更新 #3
我根本无法获取 HTTP 请求的内容正文.由于 XML 不起作用,我继续使用我的 API 的 REST 部分,它使用 JSON.这是我的一项测试:
Update #3
I am not able to get the content body of an HTTP Request at all. Since XML wasn't working, I moved forward with the REST part of my API, which uses JSON. Here's one of my tests:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'messages', ['content' => 'content']);
$this->assertEquals('saved!', $response->getContent());
}
此测试通过.如果我使用 curl,我也会成功调用:
This test passes. If I use curl, I get a successful call as well:
curl -X POST -d "content=my_new_content" "http://localhost:8000/messages"
这会返回 'saved!'
这太棒了,但是如果我尝试在独立的 PHP 脚本中使用 curl(以模拟客户端),则会返回以下内容:
This returns 'saved!'
That's awesome, but if I try to use curl in a standalone PHP script (to simulate a client), this is what is returned:
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.
这是我添加 POST 字段的 curl 命令:
This is my curl command adding the POST fields:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'content=test');
在我看来,POSTFIELDS 正被添加到请求正文中.在这种情况下,我仍然遇到同样的问题.我无法获取 HTTP 标头的正文内容.注释掉我的验证后,我得到:
It seems to me that POSTFIELDS is getting added to the body of the request. In this case, I still have the same problem. I am not able to get body content of my HTTP headers. After commenting out my validation, I get:
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!'
消息.伟大的!但是,在我的数据库中,我现在有一个空行.不是很好.它仍然看不到正文内容,只看到标题.
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.
我正在寻找这些问题的答案:
I'm looking for the answers to these questions:
- 为什么我无法获取请求的正文内容?
- 如何获取请求的正文内容?
推荐答案
内部控制器注入请求对象.因此,如果您想访问控制器方法 'foo' 中的请求正文,请执行以下操作:
Inside controller inject Request object. So if you want to access request body inside controller method 'foo' do the following:
public function foo(Request $request){
$bodyContent = $request->getContent();
}
这篇关于如何在 Laravel 中获取 HTTP 请求正文内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!