使用 httptest 可以在不启动真实服务器的情况下测试 go 的 http 服务,通过构造请求、捕获响应、断言结果三步完成测试;2. httptest.newrequest 创建模拟请求,httptest.newrecorder 记录响应,直接调用处理器函数进行测试;3. 可测试不同场景如参数缺失或不同 http 方法;4. 若使用 http.servemux,可通过 mux.servehttp 测试路由匹配;5. 运行 go test -v 执行测试并验证结果,确保处理器行为正确。

在 Go 中测试 HTTP 服务时,
net/http/httptest
下面是如何使用
httptest
假设我们有一个处理
/hello
立即学习“go语言免费学习笔记(深入)”;
// main.go
package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Query().Get("name"))
}我们注册这个处理器:
func main() {
http.HandleFunc("/hello", helloHandler)
http.ListenAndServe(":8080", nil)
}httptest
我们为
helloHandler
// main_test.go
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloHandler(t *testing.T) {
// 创建一个请求
req := httptest.NewRequest("GET", "/hello?name=Gopher", nil)
// 创建一个响应记录器
w := httptest.NewRecorder()
// 调用处理器
helloHandler(w, req)
// 获取响应
resp := w.Result()
defer resp.Body.Close()
// 检查状态码
if resp.StatusCode != http.StatusOK {
t.Errorf("期望状态码 %d,实际得到 %d", http.StatusOK, resp.StatusCode)
}
// 检查响应体
expected := "Hello, Gopher!"
if body := w.Body.String(); body != expected {
t.Errorf("期望响应体 %q,实际得到 %q", expected, body)
}
}httptest.NewRequest(method, url, body)
创建一个模拟的 HTTP 请求。第三个参数是请求体(如 POST 数据),如果是 GET 可以传
nil
httptest.NewRecorder()
返回一个
*httptest.ResponseRecorder
http.ResponseWriter
直接调用 handler
因为 Go 的处理器是函数,可以直接传入
w
req
你可以轻松测试各种情况,比如参数缺失、不同 HTTP 方法等。
func TestHelloHandler_NoName(t *testing.T) {
req := httptest.NewRequest("GET", "/hello", nil)
w := httptest.NewRecorder()
helloHandler(w, req)
expected := "Hello, !"
if body := w.Body.String(); body != expected {
t.Errorf("期望 %q,实际 %q", expected, body)
}
}http.ServeMux
如果你使用了
ServeMux
func TestRouter(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/hello", helloHandler)
req := httptest.NewRequest("GET", "/hello?name=World", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("期望状态码 %d,实际 %d", http.StatusOK, w.Code)
}
expected := "Hello, World!"
if w.Body.String() != expected {
t.Errorf("期望 %q,实际 %q", expected, w.Body.String())
}
}在项目目录下运行:
go test -v
你应该看到类似输出:
=== RUN TestHelloHandler --- PASS: TestHelloHandler (0.00s) === RUN TestHelloHandler_NoName --- PASS: TestHelloHandler_NoName (0.00s) PASS ok your-module-name 0.001s
基本上就这些。使用
httptest
以上就是怎样测试Golang的HTTP服务 使用httptest包模拟请求的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号