bez*_*nez 45 php xml phpunit laravel
我正在制作一个API,Laravel 5我正在测试它PHPUnit.我需要测试遗留功能的兼容性,这是一个XML POST.截至目前,我的第一个测试看起来像:
public function testPostMessages()
{
$response = $this->call('POST', 'xml');
$this->assertEquals(200, $response->getStatusCode());
}
Run Code Online (Sandbox Code Playgroud)
这传递得很好.我的列表中的下一步实际上是通过POST发送XML数据.所以对于我的第二次测试,我有:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml');
$this->assertTrue(is_valid_xml($response->getContent()));
}
Run Code Online (Sandbox Code Playgroud)
此测试失败.但是,我没有发送我的XML数据.我如何发送XML?
现在,之后我需要添加功能以从请求中获取内容.我知道有一个Request对象我可以与之交互,但我不确切知道要调用哪种方法.如何从控制器中获取XML?
在此期间我能够得到一些结果.我目前的测试看起来像这样:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
$this->assertContains('~', $response->getContent());
}
Run Code Online (Sandbox Code Playgroud)
我只有波浪号,因为我知道它不匹配,所以我可以看到整个响应.在XmlController.php我有:
class XmlController extends Controller {
public function index(Request $request)
{
return $request;
}
}
Run Code Online (Sandbox Code Playgroud)
我的输出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 "~".
Run Code Online (Sandbox Code Playgroud)
我的参数和内容在哪里?我觉得我只是call()错误地使用.
我更新了我的控制器使用Request:all()如下:
class XmlController extends Controller
{
public function index()
{
$content = Request::all();
return $content;
}
}
Run Code Online (Sandbox Code Playgroud)
这返回了我的键值对,但没有返回内容.
1) XmlTest::testPostMessagesContent
Failed asserting that '{"key":"value"}' contains "~".
Run Code Online (Sandbox Code Playgroud)
键值对很好; 它的进步.但是,我真正需要的是内容,因为我将在请求的内容部分中接收该数据.如果我使用,Request::getContent()我会收回一个空白字符串.这是我在测试中的电话:
$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
Run Code Online (Sandbox Code Playgroud)
这是我的测试结果:
1) XmlTest::testPostMessagesContent
Failed asserting that '' contains "~".
Run Code Online (Sandbox Code Playgroud)
我根本无法获得HTTP请求的内容正文.由于XML不起作用,我继续使用我的API的REST部分,它使用JSON.这是我的一个测试:
public function testPostMessagesContent()
{
$response = $this->call('POST', 'messages', ['content' => 'content']);
$this->assertEquals('saved!', $response->getContent());
}
Run Code Online (Sandbox Code Playgroud)
这个测试通过.如果我使用curl,我也会成功通话:
curl -X POST -d "content=my_new_content" "http://localhost:8000/messages"
Run Code Online (Sandbox Code Playgroud)
返回'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.
Run Code Online (Sandbox Code Playgroud)
这是我添加POST字段的curl命令:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'content=test');
Run Code Online (Sandbox Code Playgroud)
在我看来,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!
Run Code Online (Sandbox Code Playgroud)
所以我有我的'saved!'信息.大!但是,在我的数据库中,我有一个空行.不是很好.它仍然没有看到身体内容,只看到标题.
我正在寻找这些问题的答案:
gan*_*404 93
内部控制器注入Request对象.因此,如果您想在控制器方法'foo'中访问请求体,请执行以下操作:
public function foo(Request $request){
$bodyContent = $request->getContent();
}
Run Code Online (Sandbox Code Playgroud)
对于那些仍然收到空白回复的人$request->getContent(),您可以使用:
$request->all()
例如:
public function foo(Request $request){
$bodyContent = $request->all();
}
Run Code Online (Sandbox Code Playgroud)
我认为您不需要Request中的数据,我认为您需要Response中的数据。两者不同。此外,您还应该在控制器中正确构建响应。
看看编辑 #2 中的类,我会让它看起来像这样:
class XmlController extends Controller
{
public function index()
{
$content = Request::all();
return Response::json($content);
}
}
Run Code Online (Sandbox Code Playgroud)
一旦您完成了这一步,您应该检查测试用例中的响应内容(如果需要,请使用 print_r),您应该会看到里面的数据。
有关 Laravel 响应的更多信息,请参见此处:
http://laravel.com/docs/5.0/responses
| 归档时间: |
|
| 查看次数: |
65180 次 |
| 最近记录: |