XU XUv0.16.0

#net/http/httptest

导入方式: #net/http/httptest

公开 API: 3 函数, 7 方法, 2 类型, 0 常量, 0 方法集

进程内 ResponseRecorder 与测试用 Request。不做真实 listen。

struct ResponseRecorder

XuAPI
pub struct ResponseRecorder {
    pub code int
    pub hdr string
    pub body string
    pub wrote int
}

用途

承载 code、hdr、body、wrote 等属性的结构体。

公开字段

code int
码
hdr string
字段(string)
body string
正文
wrote int
字段(int)

new_recorder

XuAPI
pub fn new_recorder() ResponseRecorder

用途

构造 recorder。

参数

无需传入参数。

返回值

ResponseRecorder — 该类型的值

($&ResponseRecorder) write_header

XuAPI
pub fn (r $&ResponseRecorder) write_header(code int)

用途

$&ResponseRecorder 上的操作:写入头。

参数

r $&ResponseRecorder
本次调用使用的 ResponseRecorder 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
code int
码(int)

返回值

不返回值。

($&ResponseRecorder) header_set

XuAPI
pub fn (r $&ResponseRecorder) header_set(key string, val string)

用途

$&ResponseRecorder 上的操作:执行 header set 操作。

参数

r $&ResponseRecorder
本次调用使用的 ResponseRecorder 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
key string
键(string)
val string
值(string)

返回值

不返回值。

($&ResponseRecorder) write

XuAPI
pub fn (r $&ResponseRecorder) write(b &string) int

用途

$&ResponseRecorder 上的操作:写入数据,返回写入的字节数。

参数

r $&ResponseRecorder
本次调用使用的 ResponseRecorder 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
b &string
b(只读借用,调用方仍持有原值;类型 &string)

返回值

int — 写入的字节数

(ResponseRecorder) result_code

XuAPI
pub fn (r ResponseRecorder) result_code() int

用途

ResponseRecorder 上的操作:结果码。

参数

r ResponseRecorder
本次调用使用的 ResponseRecorder 实例;按值接收。

返回值

int — 该类型的值

(ResponseRecorder) result_body

XuAPI
pub fn (r ResponseRecorder) result_body() string

用途

ResponseRecorder 上的操作:结果正文。

参数

r ResponseRecorder
本次调用使用的 ResponseRecorder 实例;按值接收。

返回值

string — 结果字符串

(ResponseRecorder) result_header

XuAPI
pub fn (r ResponseRecorder) result_header(name string) string

用途

ResponseRecorder 上的操作:结果头。

参数

r ResponseRecorder
本次调用使用的 ResponseRecorder 实例;按值接收。
name string
名字(string)

返回值

string — 结果字符串

struct Server

XuAPI
pub struct Server {
    pub url string
    pub addr string
    pub lfd int
}

用途

承载 url、addr、lfd 等属性的结构体。

公开字段

url string
字段(string)
addr string
地址
lfd int
字段(int)

new_server

XuAPI
pub fn new_server(handle fn(http.Context)) Server

用途

构造 server。失败时抛出错误,调用方需处理或继续传播。

参数

handle fn(http.Context)
handle(函数值;类型 fn(http.Context))

返回值

Server — 该类型的值

($&Server) close

XuAPI
pub fn (s $&Server) close() int

用途

$&Server 上的操作:关闭资源。

参数

s $&Server
本次调用使用的 Server 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

new_request

XuAPI
pub fn new_request(method string, target string, body string) http.Request

用途

构造 request。

参数

method string
method(string)
target string
target(string)
body string
正文(string)

返回值

http.Request — 该类型的值