This repository was archived by the owner on Dec 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
89 lines (75 loc) · 2.3 KB
/
Copy pathserver_test.go
File metadata and controls
89 lines (75 loc) · 2.3 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package netutil_test
import (
"fmt"
"net"
"net/http"
"testing"
"time"
"github.com/fhofherr/netutil"
"github.com/stretchr/testify/assert"
)
func TestListenAndServe_StartServer(t *testing.T) {
s := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNoContent)
}),
}
defer s.Close()
addrC := make(chan string)
go netutil.ListenAndServe(s, netutil.NotifyAddr(addrC)) // nolint: errcheck
addr := netutil.GetAddr(t, addrC, 10*time.Millisecond)
res, err := http.Get(fmt.Sprintf("http://%s/", addr))
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
assert.Equal(t, http.StatusNoContent, res.StatusCode)
}
func TestListenAndServe_ReturnsListenerErrors(t *testing.T) {
s := &http.Server{}
defer s.Close()
addrC := make(chan string)
go netutil.ListenAndServe(s, netutil.NotifyAddr(addrC)) // nolint: errcheck
addr := netutil.GetAddr(t, addrC, 10*time.Millisecond)
s2 := &http.Server{}
err := netutil.ListenAndServe(s2, netutil.WithAddr(addr))
assert.Error(t, err)
}
func TestListenAndServe_IgnoresErrServerClosed(t *testing.T) {
s := &http.Server{}
addrC := make(chan string)
errC := make(chan error)
go func(errC chan<- error) {
errC <- netutil.ListenAndServe(s, netutil.NotifyAddr(addrC))
}(errC)
// We are just interested in the address being sent. This signals that
// the server is listening and ready to accept connections.
_ = netutil.GetAddr(t, addrC, 10*time.Millisecond)
if err := s.Close(); err != nil {
t.Fatal(err)
}
err := netutil.GetErr(t, errC, 10*time.Millisecond)
assert.NoError(t, err)
}
func TestListenAndServe_ReturnsServerErrors(t *testing.T) {
s := &http.Server{}
defer s.Close()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addrC := make(chan string)
errC := make(chan error)
go func(errC chan<- error) {
errC <- netutil.ListenAndServe(s, netutil.NotifyAddr(addrC), netutil.WithListener(l))
}(errC)
addr := netutil.GetAddr(t, addrC, 10*time.Millisecond)
l.Close()
if res, err := http.Get(fmt.Sprintf("http://%s/", addr)); err == nil {
// we actually don't expect this to work, since we closed l before
// making the request.
defer res.Body.Close()
t.Fatal("Received a resonse from s")
}
assert.Error(t, netutil.GetErr(t, errC, 10*time.Millisecond))
}