登录
首页 >  Golang >  Go问答

Golang httptest 使用HTTP服务器向HTTPS客户端提供响应

来源:stackoverflow

时间:2024-03-28 23:06:30 352浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《Golang httptest 使用HTTP服务器向HTTPS客户端提供响应》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

当使用下面的 httptest 服务器测试 https url 的 get 请求时,我得到了 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
}

参考自 代码片段

如何使用 go 存根对远程主机的请求


解决方案


事实证明,而不是

s := httptest.NewServer(handler),

我应该使用

s := httptest.NewTLSServer(handler)

获取 https 服务器。

以上就是《Golang httptest 使用HTTP服务器向HTTPS客户端提供响应》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>