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

标签 http go https

当使用下面的 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/

相关文章:

c# - 如何设置 SSL 环境来测试 C# 远程处理?

unit-testing - 单元测试 tls 客户端

angular 4 - 预检响应无效(重定向)

http - 在 HTTP 中维护同时连接?

go - 如何在 Golang 中正确使用可变参数?

http - 如何将数据返回给 channel 的发送者

apache - .htaccess 基于SSL协议(protocol)的重定向

java - Web 应用程序需要为 UTF-8 下载的文件提供适当的响应 header

wordpress - 将 Wordpress 站点移动到 HTTPS

if-statement - 或内部条件