forked from cloudflarearchive/odoh-server-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
70 lines (51 loc) · 1.36 KB
/
main_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
package main
import (
"crypto/tls"
"io"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
kp, err := keyPair()
if err != nil {
panic(err)
}
// Disable TLS strict certificate checking for local self signed cert
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
proxy, target := serverPair(*kp)
setupHandlers(proxy, target)
os.Exit(m.Run())
}
func TestGenKeyPair(t *testing.T) {
_, err := keyPair()
assert.Nil(t, err)
}
func TestServeHTTP(t *testing.T) {
go serve("127.0.0.1:8080", false, "", "")
// Wait for server startup
time.Sleep(50 * time.Millisecond)
httpClient := http.Client{}
req, err := http.NewRequest("GET", "http://127.0.0.1:8080/health", nil)
assert.Nil(t, err)
resp, err := httpClient.Do(req)
assert.Nil(t, err)
body, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, "ok", string(body))
}
func TestServeHTTPS(t *testing.T) {
go serve("127.0.0.1:8443", true, "cert.pem", "key.pem")
// Wait for server startup
time.Sleep(50 * time.Millisecond)
httpClient := http.Client{}
req, err := http.NewRequest("GET", "https://127.0.0.1:8443/health", nil)
assert.Nil(t, err)
resp, err := httpClient.Do(req)
assert.Nil(t, err)
body, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, "ok", string(body))
}