Laravel(5.8)php unit make raw post request

eagi6jfj  于 6个月前  发布在  PHP
关注(0)|答案(2)|浏览(60)

要进行定期测试后请求,可以使用用途:

// signature: json(string method,string url, array data)
   $response = $this->json("post","/api/oee/v1/statuses/log", ["data" =>$data])

字符串
然而,json()方法需要一个数组作为数据参数。然而我的数据需要是一个原始字符串:

{ "data": [ { "component_id": 16, "value": 265, "time": 1556520087 }, { "component_id": 16, "value": 324, "time": 1556520087 }, { "component_id": 16, "value": 65, "time": 1556520087 } ] }


有没有一种方法可以用来发送一个带有原始数据的post请求?

s3fp2yjn

s3fp2yjn1#

你可以解码你的字符串并将其作为数组传递。

$data = '{ "data": [ { "component_id": 16, "value": 265, "time": 1556520087 }, { "component_id": 16, "value": 324, "time": 1556520087 }, { "component_id": 16, "value": 65, "time": 1556520087 } ] }';

$response = $this->json("post","/api/oee/v1/statuses/log", [
    "data" => json_decode($data, true)
]);

字符串
如果这是测试套件中的常见操作,那么在基本应用程序测试用例中创建一个helper方法:

public function jsonString($method, $uri, $data, array $headers = [])
{
    return $this->json($method, $uri, json_decode($data, true), $headers);
}


或者一个特质会更好,你可以只在需要的时候使用,例如:

trait MakesRawJsonRequests
{
    public function jsonRaw($method, $uri, $data, array $headers = [])
    {
        return $this->json($method, $uri, json_decode($data, true), $headers);
    }
}


替代命名约定:jsonFromString()

bf1o4zei

bf1o4zei2#

使用call()

$this->call('POST', '/your/route',  [], [], [], [], 'your request body');

字符串

相关问题