如何在Go中测试HTTPS端点?

So I'm trying to mock out a response to an https request using httptest.TLSServer but the http.Client making the request keeps telling me that the server is giving an invalid http response. I bet this is because there's no actual SSL cert. Is there any way I can get the client to ignore tls while still mocking out an https request?

The test code in question

func TestFacebookLogin(t *testing.T) {
    db := glob.db
    server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        resp := `
        {
          "id": "123",
          "email": "blah",
          "first_name": "Ben",
          "last_name": "Botwin"
        }
        `

        w.Write([]byte(resp))
    }))
    defer server.Close()

    transport := &http.Transport{
        Proxy: func(r *http.Request) (*url.URL, error) {
            return url.Parse(server.URL)
        },
    }

    svc := userService{db: db, client: &http.Client{Transport: transport}}

    _, err := svc.FacebookLogin("asdf")

    if err != nil {
        t.Errorf("Error found: %s", err)
    }

}

And the request that I'm trying to make:

url := "https://<Some facebook api endpoint>"
req, err := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
if err != nil {
    return nil, err
}
defer resp.Body.Close()

The test server uses a self signed certificate. There are two ways to avoid errors. The first is to use an HTTP client configured with the test server's certificates:

certs := x509.NewCertPool()
for _, c := range server.TLS.Certificates {
    roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
    if err != nil {
        log.Fatalf("error parsing server's root cert: %v", err)
    }
    for _, root := range roots {
        certs.AddCert(root)
    }
}
client := http.Client{
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{
            RootCAs: certs,
        },
    },
}

playground example

An easier option is to skip cert verification:

client := http.Client{
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
        },
    },
}

playground example

There's an additional issue. The client expects to connect to a proxy server, but the it's actually connecting to the test server directly. Try hooking the dial function instead of the proxy:

client := http.Client{
    Transport: &http.Transport{
        Dial: func(network, addr string) (net.Conn, error) {
            return net.Dial("tcp", server.URL[strings.LastIndex(server.URL, "/")+1:])
        },
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
        },
    },
}

playground example