erest
Version:
Easy to build api server depend on @leizm/web and express.
69 lines (68 loc) • 2.16 kB
TypeScript
/**
* @file API Agent
* @author Yourtion Guo <yourtion@gmail.com>
*/
import type { IDebugger } from "debug";
import type { Test } from "supertest";
import type ERest from ".";
import { type SUPPORT_METHODS } from "./api";
import type { SourceResult } from "./utils";
export interface ITestAgentOption {
erest: ERest<unknown>;
sourceFile: SourceResult;
method: SUPPORT_METHODS;
path: string;
agent?: Test;
takeExample: boolean;
agentTestName?: string;
headers?: Record<string, string>;
input?: Record<string, unknown>;
output?: Record<string, unknown>;
agentHeader?: Record<string, unknown>;
agentInput: Record<string, unknown>;
agentOutput?: Record<string, unknown>;
}
/**
* 测试代理类
*/
export declare class TestAgent {
options: ITestAgentOption;
key: string;
debug: IDebugger;
/**
* 构造函数
*
* @param method HTTP请求方法
* @param path 请求路径
* @param key 键名:`method path`
* @param sourceFile 源文件路径描述对象
* @param erestIns hojs实例
*/
constructor(method: SUPPORT_METHODS, path: string, key: string, sourceFile: SourceResult, erestIns: ERest<unknown>);
/** 设置`supertest.Agent`实例 */
setAgent(agent: Test): void;
/** 初始化`supertest.Agent`实例 */
initAgent(app: unknown): void;
/** 获取测试代理 */
agent(): TestAgent;
/** 对测试结果加入文档 */
takeExample(name: string): this;
/** 设置请求header */
headers(data: Record<string, string>): this;
/** 添加 query 参数 */
query(data: Record<string, unknown>): this;
/** 添加输入参数 */
input(data: Record<string, unknown>): this;
/** 添加 POST 参数 */
attach(data: Record<string, unknown>): this;
/** 保存输出结果到 Example */
private saveExample;
/** 获取输出结果 */
private output;
/** 期望输出成功结果 */
success(): Promise<unknown> | undefined;
/** 期望输出失败结果 */
error(): Promise<any> | undefined;
/** 获取原始输出 */
raw(): Promise<unknown> | undefined;
}