Files
opencloud/pkg/jmaptest/tools.go
Pascal Bleser 6c57f37b3b groupware: add Groupware integration testing
* split off the Stalwart integration test boilerplate into a new
   jmaptest package, in order to be able to use it from the groupware
   package as well

 * remove the JMAP Session patching in tests and, instead, pick random
   free local ports for HTTP and IMAPS manually, and set those
   explicitly in the Stalwart testcontainer, so that we can set the
   PUBLIC_URL environment variable that Stalwart then uses to define the
   URLs that are served as part of the JMAP Sessions (API URL, etc...)
2026-07-09 14:30:14 +02:00

53 lines
1.0 KiB
Go

package jmaptest
import (
"net"
"sync"
"github.com/opencloud-eu/opencloud/pkg/jmap"
)
var (
truep = ptr(true)
falsep = ptr(false)
)
// TODO remove and replace with calls to new() when upgrading to Go 1.26
func ptr[T any | int | uint | bool | string](t T) *T {
return &t
}
func list[T jmap.Foo, GETRESP jmap.GetResponse[T]](r GETRESP) []T { return r.GetList() }
func getid[T jmap.Idable](r T) string { return r.GetId() }
func uintPtr[T int | uint](i T) *uint {
return ptr(uint(i))
}
func firstKey[K comparable, V any](m map[K]V) (K, bool) {
for k := range m {
return k, true
}
var zero K
return zero, false
}
var freeLocalhostPortSync = sync.Mutex{}
func FreeLocalhostPort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
freeLocalhostPortSync.Lock()
defer freeLocalhostPortSync.Unlock()
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}