gpt4 book ai didi

http:服务器在 Golang httptest 中对 HTTPS 客户端给出了 HTTP 响应

转载 作者:行者123 更新时间:2023-12-01 20:02:41 25 4
gpt4 key购买 nike

当使用下面的 httptest 服务器测试 https url 的获取请求时,我得到了 http: 服务器给 Golang httptest 中的 HTTPS 客户端的 HTTP 响应。当我使用以“http://”开头的 URL 时它工作正常

func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
s := httptest.NewServer(handler)

cli := &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, _ string) (net.Conn, error) {
return net.Dial(network, s.Listener.Addr().String())
},
},
}

return cli, s.Close
}

引用自 Code snippet

How to stub requests to remote hosts with Go

最佳答案

原来不是

s := httptest.NewServer(handler),

我应该用

s := httptest.NewTLSServer(handler)

获取 https 服务器。

关于http:服务器在 Golang httptest 中对 HTTPS 客户端给出了 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62825107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com