-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathstack_test.go
104 lines (92 loc) · 1.96 KB
/
stack_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
//go:build integration || !race
// +build integration !race
// SPDX-License-Identifier: Apache-2.0
package proxy
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"sync"
"testing"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
func TestProxyStack_multi(t *testing.T) {
results := map[string]int{}
m := new(sync.Mutex)
total := 100000
cfgPath := ".config.json"
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m.Lock()
results[r.URL.String()]++
m.Unlock()
w.Write([]byte("{\"foo\":42}"))
}))
defer s.Close()
{
cfgContent := `{
"version":3,
"endpoints":[{
"endpoint":"/{foo}",
"backend":[
{
"host": ["%s"],
"url_pattern": "/first/{foo}",
"group": "1"
},
{
"host": ["%s"],
"url_pattern": "/second/{foo}",
"group": "2"
},
{
"host": ["%s"],
"url_pattern": "/third/{foo}",
"group": "3"
}
]
}]
}`
if err := os.WriteFile(cfgPath, []byte(fmt.Sprintf(cfgContent, s.URL, s.URL, s.URL)), 0666); err != nil {
t.Error(err)
return
}
defer os.Remove(cfgPath)
}
cfg, err := config.NewParser().Parse(cfgPath)
if err != nil {
t.Error(err)
return
}
cfg.Normalize()
factory := NewDefaultFactory(httpProxy, logging.NoOp)
p, err := factory.New(cfg.Endpoints[0])
if err != nil {
t.Error(err)
return
}
for i := 0; i < total; i++ {
p(context.Background(), &Request{
Method: "GET",
Params: map[string]string{"Foo": "42"},
Headers: map[string][]string{},
Path: "/",
Query: url.Values{},
Body: io.NopCloser(strings.NewReader("")),
URL: new(url.URL),
})
}
for k, v := range results {
if v != total {
t.Errorf("the url %s was consumed %d times", k, v)
}
}
if len(results) != 3 {
t.Errorf("unexpected number of consumed urls. have %d, want 3", len(results))
}
}