aboutsummaryrefslogtreecommitdiff
path: root/pipeln_test.go
blob: 3b822b234573bb38f10bef9deea4d87b0f5fe20f (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"
	"syscall"
	"testing"

	"go.awhk.org/core"
)

func Test(s *testing.T) {
	t := core.T{T: s}

	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 *core.T) {
		resp, err := client.Get("http://test/endpoint")
		t.AssertErrorIs(nil, err)
		t.AssertEqual(http.StatusOK, resp.StatusCode)
	})

	t.Run("Address Mismatch", func(t *core.T) {
		_, err := client.Get("http://other-test/endpoint")
		t.AssertErrorIs(syscall.EINVAL, err)
	})

	srv.Shutdown(context.Background())

	t.Run("Remote Connection Closed", func(t *core.T) {
		_, err := client.Get("http://test/endpoint")
		t.AssertErrorIs(syscall.ECONNREFUSED, err)
	})

	t.Run("Already-closed Listener", func(t *core.T) {
		srv = http.Server{Handler: mux}
		t.AssertErrorIs(syscall.EINVAL, srv.Serve(ln))
	})
}