♻️ Use embed certificate keys for TestServe util

This commit is contained in:
Maxim Lebedev 2021-11-15 02:12:15 +05:00
parent 3f18bf73b5
commit 4654988dd1
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 17 additions and 6 deletions

View File

@ -3,8 +3,8 @@ package util
import ( import (
"crypto/tls" "crypto/tls"
_ "embed"
"net" "net"
"path/filepath"
"testing" "testing"
"time" "time"
@ -12,23 +12,33 @@ import (
httputil "github.com/valyala/fasthttp/fasthttputil" httputil "github.com/valyala/fasthttp/fasthttputil"
) )
//nolint: exhaustivestruct //nolint: gochecknoglobals
var (
//go:embed cert.pem
certData []byte
//go:embed key.pem
keyData []byte
)
// TestServe returns the InMemory HTTP-server and the client connected to it
// with the specified handler.
func TestServe(tb testing.TB, handler http.RequestHandler) (*http.Client, *http.Server, func()) { func TestServe(tb testing.TB, handler http.RequestHandler) (*http.Client, *http.Server, func()) {
tb.Helper() tb.Helper()
//nolint: exhaustivestruct
server := &http.Server{ server := &http.Server{
CloseOnShutdown: true, CloseOnShutdown: true,
DisableKeepalive: true, DisableKeepalive: true,
Handler: http.TimeoutHandler(handler, 1*time.Minute, "handler performance is too slow"),
ReduceMemoryUsage: true, ReduceMemoryUsage: true,
Handler: http.TimeoutHandler(handler, 1*time.Second, "handler performance is too slow"),
} }
ln := httputil.NewInmemoryListener() ln := httputil.NewInmemoryListener()
//nolint: errcheck //nolint: errcheck
go server.ServeTLS(ln, filepath.Join("..", "..", "..", "util", "cert.pem"), go server.ServeTLSEmbed(ln, certData, keyData)
filepath.Join("..", "..", "..", "util", "key.pem"))
//nolint: exhaustivestruct
client := &http.Client{ client := &http.Client{
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
InsecureSkipVerify: true, //nolint: gosec InsecureSkipVerify: true, //nolint: gosec
@ -38,7 +48,8 @@ func TestServe(tb testing.TB, handler http.RequestHandler) (*http.Client, *http.
}, },
} }
//nolint: errcheck
return client, server, func() { return client, server, func() {
server.Shutdown() //nolint: errcheck server.Shutdown()
} }
} }