sublime 接口测试 代替 postman 等工具

主要是利用sublime的自带编译系统

新建json

{
    "mehtod": "GET",
    "url": "https://wx.iguojin.com/activity/astrazeneca/saveData",
    "query":
    {
        "name": "测试",
        "hospital": "一家医院",
        "phone": "13011111111"
    }
}

为json建编译系统

{
    "cmd": [
        "php",
        "C:\\php\\bin\\phppost\\index.php",
        "$file"
    ],
    "working_dir": "$file_path",
    "file_regex": ".json$"
}

cmd:执行的命令
working_dir:工作目录感觉可以去掉
file_regex:文件,以json结尾时使用

php请求

此处使用了guzzlehttp

<?php
require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Middleware;

// 默认配置
const default_config = [
    'method' => 'GET',
    'url' => '',
    'cookies' => [],
    'query' => [],
    'json' => [],
    'body' => '',
    'formData' => '',
    'debug' => false,
    'multipart' => [],
];

// 读取文件
$sourcePath = $argv[1];
$sourceDir = dirname($sourcePath);
if (!file_exists($sourcePath)) {
    die('no this file');
}

// 获取配置
$file = file_get_contents($sourcePath);
$input = json_decode($file, true);
if (json_last_error()) {
    die(json_last_error_msg());
}

$config = array_merge(default_config, $input);
extract($config);
if (!$url) {
    die('没有请求地址');
}

// 数组body
if ($body && is_array($body)) {
    $json = array_merge($json, $body);
}

// 其他body
if ($body && !is_array($body)) {
    $req['body'] = $body;
}

// 请求配置
$req = [];
$req['query'] = $query;
if ($json) {
    $req['body'] = json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    $req['headers'] = [
        'Content-Type' => 'application/json',
    ];
}

if ($multipart) {
    foreach ($multipart as &$multi) {
        if ($multi['contents'] ?? false) {
            $multi['contents'] = fopen($multi['contents'], 'r');
        }
    }
    $req['multipart'] = $multipart;
}

// cookies
$cookiePath = $sourceDir . '/cookie.php';
if (file_exists($cookiePath)) {
    $baseCookie = include_once $cookiePath;
    $cookies = array_merge($baseCookie, $cookies);
}
if ($cookies ?? null) {
    $req['cookies'] = CookieJar::fromArray($cookies, parse_url($url, PHP_URL_HOST));
}

// formData
if ($formData) {
    $req['form_params'] = $formData;
}

// 日志
$client = new Client;
$clientHandler = $client->getConfig('handler');
$tapMiddleware = Middleware::tap(function ($request) use ($debug) {
    echo 'uri:' . PHP_EOL;
    echo (string) $request->getUri() . PHP_EOL;
    echo '---------' . PHP_EOL;
    if (!$debug) {
        return;
    }
    echo 'header:' . PHP_EOL;
    $headers = $request->getHeaders();
    foreach ($headers as $name => $value) {
        echo $name . ": " . implode(", ", $value) . PHP_EOL;
    }
    echo '---------' . PHP_EOL;

    echo 'body:' . PHP_EOL;
    $body = (string) $request->getBody();
    if ($body) {
        echo $body . PHP_EOL;
    }
    echo '---------' . PHP_EOL;
});
$req['handler'] = $tapMiddleware($clientHandler);

// 发送请求
$t1 = microtime(true);
$res = $client->request($method, $url, $req);
$t2 = microtime(true);
echo 'response:' . PHP_EOL;
echo $res->getBody() . PHP_EOL;
echo '---------' . PHP_EOL;
echo 'time:' . PHP_EOL;
echo ($t2 - $t1) . 's';
echo PHP_EOL;

关于cookie

只要在和测试的同级放一个cookie.php数组,即可加载,实现登录逻辑等

结果

按下ctrl+b即可
比postman更适合程序员,便于修改参数

接口耗时:0.14481711387634s
{"code":1,"data":"","msg":101,"wait":3,"url":""}
[Finished in 0.2s]

相关文章

此处评论已关闭