-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
122 lines (117 loc) · 4.63 KB
/
app_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
// Copyright 2022 anoweb Author. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package anoweb
import (
"github.com/stretchr/testify/require"
"io/ioutil"
"net"
"net/http"
"os"
"testing"
"time"
)
func TestAppRun(t *testing.T) {
errCh := make(chan error, 1)
okCh := make(chan bool, 1)
go func() {
New().Run()
}()
time.AfterFunc(time.Millisecond*100, func() {
http.DefaultClient.Timeout = time.Second
response, err := http.Get("http://localhost:9494")
if err != nil {
errCh <- err
} else if response.StatusCode > 0 {
okCh <- true
}
})
select {
case <-time.After(time.Second):
t.Error("test fail: timeout")
case err := <-errCh:
t.Errorf("test err: %v", err)
case <-okCh:
t.Log("test ok")
}
}
func TestAppRunWithTLS(t *testing.T) {
defer func() {
if re := recover(); re != nil {
t.Log("test ok!")
}
}()
_ = ioutil.WriteFile("ca.crt", []byte(certFile), 0700)
_ = ioutil.WriteFile("ca.key", []byte(keyFile), 0700)
defer func() {
_ = os.Remove("ca.crt")
_ = os.Remove("ca.key")
}()
go func() {
app := New()
app.Config.Server.TLS.Enable = true
app.Config.Server.TLS.CertFile = "ca.crt"
app.Config.Server.TLS.KeyFile = "ca.key"
app.Config.Server.Port = 9595
app.Run()
}()
time.Sleep(time.Second)
conn, _ := net.DialTimeout("tcp", "localhost:9595", time.Second)
require.NotNil(t, conn)
_ = conn.Close()
}
const (
certFile = `-----BEGIN CERTIFICATE-----
MIIDETCCAfkCFHyMmBP9DYIZRoqW17cQvSupfKISMA0GCSqGSIb3DQEBCwUAMEUx
CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl
cm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMjExMjE4MDEyNzQ1WhcNMzExMjE2MDEy
NzQ1WjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UE
CgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAxDXnGF0QWnNtRA4TL8uAhDE0tuorSwq6DZI2rCXYkaY4+6SN
WYcGbpLTnJVP0yRBbf+8kI04yGWGFObmfcyUniDS4N0QrKcdTE31Egvc+vb5RKbj
Ht0jcj/ofyWSb50xZUaGolqKJ9rb3A0Y4ia9TnLGnf6KSDlYAvjfu7wY1RGFkgRx
HnpxKGT/Ry2J+pSWYrVOYOHub2M2eI2RLzY6jrEHWkif+Bybtwt84qIWG6fKkgFC
d7hQwbaqiD6AuwuUtsSzBzJ5S0cOLvuCHLG0dRVcLkkHkirLJ1WMEfzC/SlsOHCW
hS92v6IH72UbPO2nHGskikbtnQqC8CnKfiq1XwIDAQABMA0GCSqGSIb3DQEBCwUA
A4IBAQAiPJ9nDjqTsCbq4pgfNaVFNY+bGgi2cGyiD1+MOGxp8BycfLUfTmHiBUPM
OmOhg5hXxiknJ5eAhRBP+c6NlemoT61oTUmsr1jmTpoiryBZmnZnKCu8TxkxhG/r
Mk9ZwXJdtmO8iJT/9vBF7i4vHCUBMimZztowsH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