TestServer helper, integrate proxy

This commit is contained in:
Ian Gulliver
2023-06-04 10:31:26 -07:00
parent 889213b77a
commit 0b9c2543e4
4 changed files with 61 additions and 21 deletions

47
lib_test.go Normal file
View File

@@ -0,0 +1,47 @@
package elect_test
import (
"net"
"net/http"
"testing"
"time"
"github.com/gopatchy/elect"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
)
type TestServer struct {
Candidate *elect.Candidate
listener *net.TCPListener
srv *http.Server
}
func NewTestServer(t *testing.T, signingKey string) *TestServer {
ts := &TestServer{
Candidate: elect.NewCandidate(1, signingKey),
listener: lo.Must(net.ListenTCP("tcp", nil)),
}
ts.srv = &http.Server{
Handler: ts.Candidate,
ReadHeaderTimeout: 30 * time.Second,
}
go func() {
err := ts.srv.Serve(ts.listener)
require.ErrorIs(t, err, http.ErrServerClosed)
}()
return ts
}
func (ts *TestServer) Stop() {
ts.srv.Close()
ts.Candidate.Stop()
}
func (ts *TestServer) Addr() *net.TCPAddr {
return ts.listener.Addr().(*net.TCPAddr)
}