github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/tests/utils.go (about)

     1  package tests
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func getFreePort(t *testing.T) (port int) {
    15  	require := require.New(t)
    16  
    17  	listener, err := net.Listen("tcp", "")
    18  	require.NoError(err)
    19  	defer listener.Close()
    20  
    21  	tcpAddr, ok := listener.Addr().(*net.TCPAddr)
    22  	require.True(ok)
    23  
    24  	return tcpAddr.Port
    25  }
    26  
    27  func newRequest(t *testing.T, method Method, url string, body io.Reader) (req *http.Request, cancelCtx func()) {
    28  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    29  
    30  	req, err := http.NewRequestWithContext(ctx, string(method), url, body)
    31  	require.NoError(t, err)
    32  
    33  	return req, cancel
    34  }
    35  
    36  func ptrStr(v string) *string {
    37  	return &v
    38  }
    39  
    40  func ptrUint(v uint) *uint {
    41  	return &v
    42  }
    43  
    44  func ptrFloat(v float64) *float64 {
    45  	return &v
    46  }