https://github.com/madflojo/testcerts
Dynamically generate self-signed certificates and certificate authorities for Go tests
https://github.com/madflojo/testcerts
certificate-generation go golang testing
Last synced: 7 months ago
JSON representation
Dynamically generate self-signed certificates and certificate authorities for Go tests
- Host: GitHub
- URL: https://github.com/madflojo/testcerts
- Owner: madflojo
- License: mit
- Created: 2019-07-11T15:15:15.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-05-19T21:17:06.000Z (over 1 year ago)
- Last Synced: 2024-05-20T16:14:05.880Z (over 1 year ago)
- Topics: certificate-generation, go, golang, testing
- Language: Go
- Homepage:
- Size: 76.2 KB
- Stars: 45
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - testcerts - signed certificates and certificate authorities within your test functions. [![godoc][D]](https://godoc.org/github.com/madflojo/testcerts) (测试 / Testing Frameworks)
- fucking-awesome-go - testcerts - Dynamically generate self-signed certificates and certificate authorities within your test functions. (Testing / Testing Frameworks)
- awesome-go - madflojo/testcerts - signed certificates and certificate authorities for Go tests ☆`83` (Testing / Testing Frameworks)
- awesome-go-plus - testcerts - Dynamically generate self-signed certificates and certificate authorities within your test functions.  (Testing / Testing Frameworks)
- awesome-go - testcerts - Dynamically generate self-signed certificates and certificate authorities within your test functions. (Testing / Testing Frameworks)
- awesome-go-with-stars - testcerts - Dynamically generate self-signed certificates and certificate authorities within your test functions. (Testing / Testing Frameworks)
- awesome-go - testcerts - Dynamically generate self-signed certificates and certificate authorities within your test functions. (Testing / Testing Frameworks)
- awesome-go - testcerts - Dynamically generate self-signed certificates and certificate authorities within your test functions. (Testing / Testing Frameworks)
README
# testcerts

[](https://codecov.io/gh/madflojo/testcerts)
[](https://goreportcard.com/report/github.com/madflojo/testcerts)
[](https://pkg.go.dev/github.com/madflojo/testcerts)
[](https://github.com/madflojo/testcerts/LICENSE)
Stop saving test certificates in your code repos. Start generating them in your tests.
```go
func TestFunc(t *testing.T) {
// Create and write self-signed Certificate and Key to temporary files
cert, key, err := testcerts.GenerateToTempFile("/tmp/")
if err != nil {
// do something
}
defer os.Remove(key)
defer os.Remove(cert)
// Start HTTP Listener with test certificates
err = http.ListenAndServeTLS("127.0.0.1:443", cert, key, someHandler)
if err != nil {
// do something
}
}
```
For more complex tests, you can also use this package to create a Certificate Authority and a key pair signed by that Certificate Authority for any test domain you want.
```go
func TestFunc(t *testing.T) {
// Generate Certificate Authority
ca := testcerts.NewCA()
go func() {
// Create a signed Certificate and Key for "localhost"
certs, err := ca.NewKeyPair("localhost")
if err != nil {
// do something
}
// Write certificates to a file
err = certs.ToFile("/tmp/cert", "/tmp/key")
if err {
// do something
}
// Start HTTP Listener
err = http.ListenAndServeTLS("localhost:443", "/tmp/cert", "/tmp/key", someHandler)
if err != nil {
// do something
}
}()
// Create a client with the self-signed CA
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: certs.ConfigureTLSConfig(ca.GenerateTLSConfig()),
},
}
// Make an HTTPS request
r, _ := client.Get("https://localhost")
}
```
Simplify your testing, and don't hassle with certificates anymore.
## Contributing
If you find a bug or have an idea for a feature, please open an issue or a pull request.
## License
testcerts is released under the MIT License. See [LICENSE](./LICENSE) for details.