国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

在單元測試中發(fā)起對項目的接口請求,應該怎么操作呢?

= - =

希望能在單元測試中直接發(fā)起對接口的請求,并斷言接口返回的數(shù)據(jù)是否正確。這個應該怎么操作呢?

2358 2 4
2個回答

菜徐坤

我是 弄了個請求的函數(shù)
/**

  • 發(fā)送http請求
  • @param $url
  • @param array|null $data
  • @param string $method
  • @param string[] $header
  • @param bool $https
  • @param int $timeout
  • @return bool|string
    */
    function curl_request(
    string $url,
    string $method = 'get',
    array $data = [],
    array $header = array("content-type: application/json"),
    bool $https = true,
    int $timeout = 5)
    {
    $method = strtoupper($method);
    $ch = curl_init();//初始化
    curl_setopt($ch, CURLOPT_URL, $url);//訪問的URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只獲取頁面內容,但不輸出
    if ($https) {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https請求 不驗證證書
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https請求 不驗證HOST
    }
    if ($method != "GET") {
    if ($method == 'POST') {
    curl_setopt($ch, CURLOPT_POST, true);//請求方式為post請求
    }
    if ($method == 'PUT' || strtoupper($method) == 'DELETE') {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //設置請求方式
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//請求數(shù)據(jù)
    }
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模擬的header頭
    //curl_setopt($ch, CURLOPT_HEADER, false);//設置不需要頭信息
    $result = curl_exec($ch);//執(zhí)行請求
    curl_close($ch);//關閉curl,釋放資源
    return $result;
    }

然后這樣使用的 不知道 符不符合你的預期
截圖

  • 暫無評論
ifui

我的是參考源碼寫了個簡單的啟用方法,可以參考一下,由于繞過了一些東西(部分源碼還沒理解透),可能會出現(xiàn)部分問題,所以放出來和大家相互討論

<?php

namespace Ifui\WebmanMarket\Testing\Traits;

use FastRoute\Dispatcher;
use support\Request;
use Webman\Config;
use Webman\Http\Response;
use Webman\Route;
use function get_class;
use function is_array;

trait MakeHttpRequest
{

    /**
     * Additional headers for the request.
     *
     * @var array
     */
    protected $defaultHeaders = [];

    /**
     * Additional cookies for the request.
     *
     * @var array
     */
    protected $defaultCookies = [];

    /**
     * Add an authorization token for the request.
     *
     * @param string $token
     * @param string $type
     * @return $this
     */
    public function withToken(string $token, string $type = 'Bearer')
    {
        return $this->withHeader('Authorization', $type . ' ' . $token);
    }

    /**
     * Add a header to be sent with the request.
     *
     * @param string $name
     * @param string $value
     * @return $this
     */
    public function withHeader(string $name, string $value)
    {
        $this->defaultHeaders[$name] = $value;

        return $this;
    }

    /**
     * Flush all the configured headers.
     *
     * @return $this
     */
    public function flushHeaders()
    {
        $this->defaultHeaders = [];

        return $this;
    }

    /**
     * Visit the given URI with a GET request.
     *
     * @param string $uri
     * @param array $parameters
     * @param array $headers
     * @return Response
     */
    public function get($uri, $parameters = [], $headers = [])
    {
        $this->withHeaders($headers);
        return $this->call('GET', $uri, $parameters);
    }

    /**
     * Define additional headers to be sent with the request.
     *
     * @param array $headers
     * @return $this
     */
    public function withHeaders(array $headers)
    {
        $this->defaultHeaders = array_merge($this->defaultHeaders, $headers);

        return $this;
    }

    /**
     * Call the given URI and return the Response.
     *
     * @param string $method
     * @param string $uri
     * @param array $parameters
     * @return Response
     */
    protected function call($method, $uri, $parameters = [])
    {
        $ret = Route::dispatch($method, $uri);

        if ($ret[0] === Dispatcher::FOUND) {
            $request = $this->request($method, $uri, $parameters);
            $callback = $ret[1]['callback'];
            $route = $ret[1]['route'];
            $route = clone $route;
            $args = !empty($ret[2]) ? $ret[2] : null;
            if ($args) {
                $route->setParams($args);
            }
            if (is_array($callback) && isset($callback[0]) && $controller = get_class($callback[0])) {
                /** @var Response $res */
                return call_user_func([new $controller, $callback[1]], $request, $args);
            }
        } else {
            return new Response(404, [], '404');
        }
    }

    /**
     * Mock send request.
     *
     * @param $method
     * @param $uri
     * @param $parameters
     * @return mixed
     */
    public function request($method, $uri, $parameters = [])
    {
        $requestClassName = Config::get('app.request_class', Request::class);
        $request = new $requestClassName('');
        $request->_data = [
            'post' => $parameters,
            'get' => $parameters,
            'headers' => $this->defaultHeaders,
            'cookie' => $this->defaultCookies,
            'files' => [], // TODO
            'method' => $method,
            'protocolVersion' => '1.1',
            'host' => 'localhost',
            'uri' => $uri,
        ];
        return $request;
    }

    /**
     * Visit the given URI with a GET request, expecting a JSON response.
     *
     * @param string $uri
     * @param array $headers
     * @return Response
     */
    public function getJson($uri, $parameters = [], $headers = [])
    {
        return $this->json('GET', $uri, $parameters, $headers);
    }

    /**
     * Call the given URI with a JSON request.
     *
     * @param string $method
     * @param string $uri
     * @param array $data
     * @param array $headers
     * @return Response
     */
    public function json($method, $uri, $data = [], $headers = [])
    {
        $content = json_encode($data);

        $this->withHeaders([
            'CONTENT_LENGTH' => mb_strlen($content, '8bit'),
            'CONTENT_TYPE' => 'application/json',
            'Accept' => 'application/json',
        ]);

        $rsp = $this->call($method, $uri, $data);
        $body = $rsp->rawBody();
        $json = json_decode($body, true) ?? $body;
        $rsp->withBody($json);
        return $rsp;
    }
}

這里是使用示例:

<?php

namespace market\apple\tests\unit;

use market\apple\tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testIndex()
    {
        $default_timezone = config('app.default_timezone');
        $this->assertIsString($default_timezone);

        $rsp = $this->get('/apple/index/index');
        $this->assertSame(200, $rsp->getStatusCode());
        $this->assertSame('hello webman market', $rsp->rawBody());

        $rsp = $this->getJson('/apple/index/json');
        $this->assertArrayHasKey('code', $rsp->rawBody());
    }
}
  • 暫無評論
年代過于久遠,無法發(fā)表回答
??