github.com/kubri/kubri@v0.5.1-0.20240317001612-bda2aaef967e/internal/emulator/server.go (about)

     1  package emulator
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  // FileServer creates a new file server and returns its URL.
    12  //
    13  // On MacOS the host is replaced with host.docker.internal to allow docker
    14  // containers to access the service.
    15  // See https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
    16  func FileServer(t *testing.T, dir string) string {
    17  	t.Helper()
    18  	s := httptest.NewServer(http.FileServer(http.Dir(dir)))
    19  	t.Cleanup(s.Close)
    20  	if runtime.GOOS != "darwin" {
    21  		return s.URL
    22  	}
    23  	return strings.Replace(s.URL, "127.0.0.1", "host.docker.internal", 1)
    24  }