-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener_test.go
139 lines (134 loc) · 3.27 KB
/
listener_test.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package webserv_test
import (
"net"
"os"
"path"
"testing"
"github.com/linkdata/webserv"
)
func withCertFiles(t *testing.T, fn func(destdir string)) {
t.Helper()
certPem := []byte(`-----BEGIN CERTIFICATE-----
MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d
7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B
5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr
BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1
NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l
Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc
6MF9+Yw1Yy0t
-----END CERTIFICATE-----`)
keyPem := []byte(`-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49
AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
-----END EC PRIVATE KEY-----`)
destdir, err := os.MkdirTemp("", "weblistener")
if err == nil {
defer func() {
if err := os.RemoveAll(destdir); err != nil {
t.Error(err)
}
}()
if err = os.WriteFile(path.Join(destdir, webserv.FullchainPem), certPem, 0640); err == nil {
if err = os.WriteFile(path.Join(destdir, webserv.PrivkeyPem), keyPem, 0640); err == nil {
fn(destdir)
}
}
}
if err != nil {
t.Error(err)
}
}
func TestRandomPort(t *testing.T) {
gotListener, gotUrl, _, err := webserv.Listener("localhost:", "", "", "", "")
if err != nil {
t.Fatal(err)
}
if gotListener == nil {
t.Fatal("no listener")
}
_, p, err := net.SplitHostPort(gotListener.Addr().String())
if err != nil {
t.Fatal(err)
}
if len(p) < 2 {
t.Error(p)
}
want := "http://localhost:" + p
if gotUrl != want {
t.Errorf("want %q got %q", want, gotUrl)
}
}
func TestNew(t *testing.T) {
withCertFiles(t, func(destdir string) {
httpPort := "8080"
httpsPort := "8443"
if os.Getuid() < 1 {
httpPort = "80"
httpsPort = "443"
}
type args struct {
wantAddress string
certDir string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "http://localhost:" + httpPort,
args: args{},
wantErr: false,
},
{
name: "http://localhost:8888",
args: args{
wantAddress: "localhost:8888",
},
},
{
name: "https://localhost:" + httpsPort,
args: args{
certDir: destdir,
},
},
{
name: "https://localhost:4443",
args: args{
wantAddress: "localhost:4443",
certDir: destdir,
},
},
{
name: "invalid port",
args: args{
wantAddress: "127.0.0.1:99999",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotListener, gotUrl, gotCertDir, err := webserv.Listener(tt.args.wantAddress, tt.args.certDir, "", "", "")
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotListener != nil {
if gotUrl != tt.name {
t.Errorf("ListenURL() = %v, want %v", gotUrl, tt.name)
}
if gotCertDir != tt.args.certDir {
t.Error(gotCertDir)
}
if err := gotListener.Close(); err != nil {
t.Error(err)
}
}
})
}
})
}