auth/internal/util/util.go

43 lines
827 B
Go
Raw Normal View History

2021-09-20 15:45:54 +00:00
package util
import (
"net"
"testing"
2021-09-20 15:45:54 +00:00
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
2021-09-20 15:45:54 +00:00
http "github.com/valyala/fasthttp"
httputil "github.com/valyala/fasthttp/fasthttputil"
)
//nolint: exhaustivestruct
func TestServe(tb testing.TB, handler http.RequestHandler) (*http.Client, *http.Server, func()) {
tb.Helper()
2021-09-20 15:45:54 +00:00
ln := httputil.NewInmemoryListener()
server := &http.Server{
Handler: handler,
DisableKeepalive: true,
CloseOnShutdown: true,
}
2021-09-20 15:45:54 +00:00
go func() {
assert.NoError(tb, server.Serve(ln))
2021-09-20 15:45:54 +00:00
}()
client := &http.Client{
Dial: func(_ string) (net.Conn, error) {
conn, err := ln.Dial()
if err != nil {
return nil, errors.Wrap(err, "cannot dial to address")
}
return conn, nil
2021-09-20 15:45:54 +00:00
},
}
return client, server, func() {
assert.NoError(tb, server.Shutdown())
}
2021-09-20 15:45:54 +00:00
}