https://github.com/zoido/trustme-go
Reduce the boilerplate code when testing with TLS
https://github.com/zoido/trustme-go
go golang golang-library golang-package testing tests tls x509
Last synced: 11 months ago
JSON representation
Reduce the boilerplate code when testing with TLS
- Host: GitHub
- URL: https://github.com/zoido/trustme-go
- Owner: zoido
- License: mit
- Created: 2020-02-08T11:13:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T14:23:36.000Z (over 3 years ago)
- Last Synced: 2025-03-22T17:44:17.662Z (about 1 year ago)
- Topics: go, golang, golang-library, golang-package, testing, tests, tls, x509
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Trust me. Go!
[](https://github.com/zoido/trustme-go/actions?query=workflow%3AGo)
[](https://codecov.io/gh/zoido/trustme-go)
[](https://godoc.org/github.com/zoido/trustme-go)
[](https://goreportcard.com/report/github.com/zoido/trustme-go)
Inspired by [trustme](https://github.com/python-trio/trustme)
for [Python](https://www.python.org/).
`trustme-go` is a small Go package that offers you with fake
[certificate autority](https://en.wikipedia.org/wiki/Certificate_authority)
(CA) that issues TLS certificates for Go tests for the cases when
[`httptest.NewTLSServer`](https://golang.org/pkg/net/http/httptest/#NewTLSServer)
is not enough.
## Example
```go
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()))
}
```