proxy_posix_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //go:build !windows
  2. package proxy
  3. import (
  4. "net"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. "github.com/cloudflare/cloudflared/config"
  11. )
  12. func TestUnixSocketOrigin(t *testing.T) {
  13. file, err := os.CreateTemp("", "unix.sock")
  14. require.NoError(t, err)
  15. os.Remove(file.Name()) // remove the file since binding the socket expects to create it
  16. l, err := net.Listen("unix", file.Name())
  17. require.NoError(t, err)
  18. defer l.Close()
  19. defer os.Remove(file.Name())
  20. api := &httptest.Server{
  21. Listener: l,
  22. Config: &http.Server{Handler: mockAPI{}},
  23. }
  24. api.Start()
  25. defer api.Close()
  26. unvalidatedIngress := []config.UnvalidatedIngressRule{
  27. {
  28. Hostname: "unix.example.com",
  29. Service: "unix:" + file.Name(),
  30. },
  31. {
  32. Hostname: "*",
  33. Service: "http_status:404",
  34. },
  35. }
  36. tests := []MultipleIngressTest{
  37. {
  38. url: "http://unix.example.com",
  39. expectedStatus: http.StatusCreated,
  40. expectedBody: []byte("Created"),
  41. },
  42. }
  43. runIngressTestScenarios(t, unvalidatedIngress, tests)
  44. }