[英]Laravel Testing API Endpoint - POST Parameters Missing
I have a API endpoint that I wish to test, it receives a POST payload. 我有一个要测试的API端点,它接收POST负载。 The endpoint basically invokes the Omnipay Sage Pay library to handle a Server Notification.
端点基本上会调用Omnipay Sage Pay库来处理服务器通知。
I can POST to this endpoint using Postman client fine, but when using a Laravel phpunit test eg 我可以使用Postman客户端正常发布到此端点,但是使用Laravel phpunit测试时,例如
$response = $this->post($url, $data);
The Omnipay library can't see the post data? Omnipay库看不到帖子数据?
I think this is because the Laravel helper $this->post
doesn't use application/x-www-form-urlencoded POST requests? 我认为这是因为Laravel帮助程序
$this->post
不使用application / x-www-form-urlencoded POST请求吗? I took a look behind the scenes at $this->post
and it seems to invoke the controllers/methods directly which makes sense.. 我在
$this->post
幕后看了一下,似乎直接调用了控制器/方法,这很有意义。
The closest I got to this working was using Guzzle directly (See below), however this routes the request outside of the 'testing' session and back into my local application I'm assuming? 我最接近此工作的是直接使用Guzzle(请参见下文),但是这会将请求路由到“测试”会话之外并返回到我假设的本地应用程序中? This then breaks my tests as I'm setting up some testing data via factories before the POST call.
这会破坏我的测试,因为我在POST调用之前通过工厂设置了一些测试数据。
$response = $client->request('POST', $url, ['form_params' => $data]);
I'm not too sure where the issue lies, either in my tests or the Omnipay library itself? 我不太确定问题出在哪里,无论是在我的测试中还是在Omnipay库本身中?
Solved it.. Turns out Omnipay allows you to pass your own Request class/object as the third param when invoking the Omnipay object. 解决了..事实证明,Omnipay允许您在调用Omnipay对象时将自己的Request类/对象作为第三个参数传递。 The library will then have access to the POST params from the test
$this->post($url, $data);
然后,库将可以从测试
$this->post($url, $data);
访问POST参数$this->post($url, $data);
call. 呼叫。
Laravel's Illuminate\\Http\\Request
is an extension of Symfony\\Component\\HttpFoundation\\Request
, so we can pass this straight through. Laravel的
Illuminate\\Http\\Request
是Symfony\\Component\\HttpFoundation\\Request
的扩展,因此我们可以直接通过它。
<?php
use Illuminate\Http\Request;
class SagePayController extends Controller
{
protected $gateway;
protected function setupGateway(Request $request)
{
$this->gateway = OmniPay::create('SagePay\Server', null, $request);
...
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.