简体   繁体   中英

http: server gave HTTP response to HTTPS client in Golang httptest

I got http: server gave HTTP response to HTTPS client in Golang httptest, when testing a get request of https url using httptest server below. It works fine when I use URL start with "http://"

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
}

refer from Code snippet

How to stub requests to remote hosts with Go

Turns out that instead of

s:= httptest.NewServer(handler) ,

I should use

s:= httptest.NewTLSServer(handler)

to get a https server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM