aboutsummaryrefslogtreecommitdiff
path: root/pipeln_test.go
blob: 9673fcc50a5f80796062d483656b34d89f47eb8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: CC0-1.0

package pipeln

import (
	"context"
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"golang.org/x/sys/unix"
)

func Test(t *testing.T) {
	ln := New("test:80")

	mux := http.NewServeMux()
	mux.HandleFunc("/endpoint", func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusOK)
	})
	srv := http.Server{Handler: mux}
	go srv.Serve(ln)

	client := http.Client{Transport: &http.Transport{Dial: ln.Dial}}

	t.Run("OK", func(t *testing.T) {
		resp, err := client.Get("http://test/endpoint")
		require.NoError(t, err)
		assert.Equal(t, http.StatusOK, resp.StatusCode)
	})

	t.Run("Address Mismatch", func(t *testing.T) {
		_, err := client.Get("http://other-test/endpoint")
		assert.ErrorIs(t, err, unix.EINVAL)
	})

	srv.Shutdown(context.Background())

	t.Run("Remote Connection Closed", func(t *testing.T) {
		_, err := client.Get("http://test/endpoint")
		assert.ErrorIs(t, err, unix.ECONNREFUSED)
	})

	t.Run("Already-closed Listener", func(t *testing.T) {
		srv = http.Server{Handler: mux}
		assert.ErrorIs(t, srv.Serve(ln), unix.EINVAL)
	})
}