duandui5648 2018-07-24 04:03
浏览 141
已采纳

单元测试TLS客户端

I have a function that makes an https request to a TLS server. I want to write unit tests to test the client function. I came across httptest.NewTLSServer that can be used to mock the server. But, how can I incorporate the certificates that the client uses instead of the default ones used in NewTLSServer?

Following is the client function that makes the request:

func MakeDownloadRequest(url string) (*http.Response, error) {

    caCert, err := ioutil.ReadFile("client/tlscerts/cert.pem")
    if err != nil {
        return nil, err
    }
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    cert, err := tls.LoadX509KeyPair("client/tlscerts/cert.pem","client/tlscerts/key.pem")
    if err != nil {
        return nil, err
    }

    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                RootCAs:      caCertPool,
                Certificates: []tls.Certificate{cert},
            },
        },
    }

    resp, err := client.Get(url)
    if err != nil {
        return nil, err
    }
    return resp, nil
}

Server stub:

server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "Hello")
    }))

Using the server.URL to make requests gives me "bad certificate" error. How can I make both the client and server use the same certificate?

  • 写回答

1条回答 默认 最新

  • dsigh7478 2018-07-24 07:54
    关注

    Instead of using NewTLSServer() you want to:

    • Create your test server via httptest.NewUnstartedServer()
    • Create a *tls.Config much like you did in the code that sets up your http.Client in your MakeDownloadRequest()
    • Set the test server's TLS field to the *tls.Config created in the previous step
    • Start the test server using its StartTLS() method

    Then your program should be able to call into your test server using those certificates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突