-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomponent_test.go
172 lines (150 loc) · 4.33 KB
/
component_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package prom2click
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/gotomicro/ego/core/constant"
"github.com/gotomicro/ego/core/elog"
"github.com/stretchr/testify/assert"
)
func TestContextClientIP(t *testing.T) {
router := DefaultContainer().Build(WithTrustedPlatform("X-Forwarded-For"))
router.GET("/", func(c *gin.Context) {
assert.Equal(t, "10.10.10.11", c.ClientIP())
})
performRequest(router, "GET", "/", header{
Key: "X-Forwarded-For",
Value: "10.10.10.11",
})
router3 := DefaultContainer().Build(WithTrustedPlatform("X-Forwarded-For"))
router3.GET("/", func(c *gin.Context) {
assert.NotEqual(t, "10.10.10.12", c.ClientIP())
})
performRequest(router3, "GET", "/", header{
Key: "X-Forwarded-For",
Value: "10.10.10.11,10.10.10.12",
})
router2 := DefaultContainer().Build(WithTrustedPlatform("X-CUSTOM-CDN-IP"))
router2.GET("/", func(c *gin.Context) {
assert.Equal(t, "10.10.10.12", c.ClientIP())
})
performRequest(router2, "GET", "/", header{
Key: "X-CUSTOM-CDN-IP",
Value: "10.10.10.12",
})
}
func TestNewComponent(t *testing.T) {
cfg := config{
Host: "0.0.0.0",
Port: 9006,
Network: "tcp",
}
cmp := newComponent("test-cmp", &cfg, elog.DefaultLogger)
assert.Equal(t, "test-cmp", cmp.Name())
assert.Equal(t, "server.prom2click", cmp.PackageName())
assert.Equal(t, "0.0.0.0:9006", cmp.config.Address())
assert.NoError(t, cmp.Init())
info := cmp.Info()
assert.NotEmpty(t, info.Name)
assert.Equal(t, "http", info.Scheme)
assert.Equal(t, "[::]:9006", info.Address)
assert.Equal(t, constant.ServiceProvider, info.Kind)
// err = cmp.Start()
go func() {
assert.NoError(t, cmp.Start())
}()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
<-ctx.Done()
assert.NoError(t, cmp.Stop())
}
func loadConfig(t *testing.T, loadClientCert bool) *tls.Config {
pool := x509.NewCertPool()
ca, err := os.ReadFile("./testdata/egoServer/ca.pem")
assert.Nil(t, err)
assert.True(t, pool.AppendCertsFromPEM(ca))
cf := &tls.Config{}
cf.RootCAs = pool
if loadClientCert {
serverCert, err := tls.LoadX509KeyPair("./testdata/egoClient/anyClient.pem", "./testdata/egoClient/anyClient-key.pem")
assert.Nil(t, err)
cf.Certificates = []tls.Certificate{serverCert}
}
return cf
}
func TestServerTimeouts(t *testing.T) {
timeout := 2 * time.Second
err := testServerTimeouts(timeout)
if err == nil {
return
}
t.Logf("failed at %v: %v", timeout, err)
}
func testServerTimeouts(timeout time.Duration) error {
reqNum := 0
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
reqNum++
fmt.Fprintf(res, "req=%d", reqNum)
}))
ts.Config.ReadTimeout = timeout
ts.Config.WriteTimeout = timeout
ts.Start()
defer ts.Close()
// Hit the HTTP server successfully.
c := ts.Client()
t0 := time.Now()
r, err := c.Get(ts.URL)
if err != nil {
return fmt.Errorf("http Get #1: %v", err)
}
got, err := io.ReadAll(r.Body)
latency := time.Since(t0)
fmt.Printf("got--------------->"+"%+v\n", got)
fmt.Printf("latency--------------->"+"%+v\n", latency)
expected := "req=1"
if string(got) != expected || err != nil {
return fmt.Errorf("Unexpected response for request #1; got %q ,%v; expected %q, nil",
string(got), err, expected)
}
// Slow client that should timeout.
t1 := time.Now()
conn, err := net.Dial("tcp", ts.Listener.Addr().String())
if err != nil {
return fmt.Errorf("Dial: %v", err)
}
buf := make([]byte, 1)
n, err := conn.Read(buf)
conn.Close()
latency = time.Since(t1)
fmt.Printf("latency--------------->"+"%+v\n", latency)
if n != 0 || err != io.EOF {
return fmt.Errorf("Read = %v, %v, wanted %v, %v", n, err, 0, io.EOF)
}
minLatency := timeout / 5 * 4
if latency < minLatency {
return fmt.Errorf("got EOF after %s, want >= %s", latency, minLatency)
}
// Hit the HTTP server successfully again, verifying that the
// previous slow connection didn't run our handler. (that we
// get "req=2", not "req=3")
r, err = c.Get(ts.URL)
if err != nil {
return fmt.Errorf("http Get #2: %v", err)
}
got, err = io.ReadAll(r.Body)
r.Body.Close()
expected = "req=2"
if string(got) != expected || err != nil {
return fmt.Errorf("Get #2 got %q, %v, want %q, nil", string(got), err, expected)
}
return nil
}