Skip to content

zoido/trustme-go

Repository files navigation

Trust me. Go!

Go codecov GoDoc Go Report Card

Inspired by trustme for Python.

trustme-go is a small Go package that offers you with fake certificate autority (CA) that issues TLS certificates for Go tests for the cases when httptest.NewTLSServer is not enough.

Example

func TestExample(t *testing.T) {
	ca := trustme.New(t)

	srvCfg := ca.MustIssue(trustme.WithIP(net.ParseIP("127.0.0.1"))).AsServerConfig()
	srvCfg.ClientAuth = tls.RequireAndVerifyClientCert
	listener, _ := tls.Listen("tcp", "127.0.0.1:0", srvCfg)
	defer listener.Close()

	srv := http.Server{
		Handler: http.HandlerFunc(ExampleHandler),
	}
	defer srv.Close()
	go srv.Serve(listener)

	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: ca.MustIssue().AsClientConfig(),
		},
		Timeout: time.Second * 5,
	}

	client.Get(fmt.Sprintf("https://%s/", listener.Addr().String()))
}