MCPcopy
hub / github.com/labstack/echo / TestProxy

Function TestProxy

middleware/proxy_test.go:29–129  ·  view source on GitHub ↗

Assert expected with url.EscapedPath method to obtain the path.

(t *testing.T)

Source from the content-addressed store, hash-verified

27
28// Assert expected with url.EscapedPath method to obtain the path.
29func TestProxy(t *testing.T) {
30 // Setup
31 t1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32 fmt.Fprint(w, "target 1")
33 }))
34 defer t1.Close()
35 url1, _ := url.Parse(t1.URL)
36 t2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37 fmt.Fprint(w, "target 2")
38 }))
39 defer t2.Close()
40 url2, _ := url.Parse(t2.URL)
41
42 targets := []*ProxyTarget{
43 {
44 Name: "target 1",
45 URL: url1,
46 },
47 {
48 Name: "target 2",
49 URL: url2,
50 },
51 }
52 rb := NewRandomBalancer(nil)
53 // must add targets:
54 for _, target := range targets {
55 assert.True(t, rb.AddTarget(target))
56 }
57
58 // must ignore duplicates:
59 for _, target := range targets {
60 assert.False(t, rb.AddTarget(target))
61 }
62
63 // Random
64 e := echo.New()
65 e.Use(ProxyWithConfig(ProxyConfig{Balancer: rb}))
66 req := httptest.NewRequest(http.MethodGet, "/", nil)
67 rec := httptest.NewRecorder()
68 e.ServeHTTP(rec, req)
69 body := rec.Body.String()
70 expected := map[string]bool{
71 "target 1": true,
72 "target 2": true,
73 }
74 assert.Condition(t, func() bool {
75 return expected[body]
76 })
77
78 for _, target := range targets {
79 assert.True(t, rb.RemoveTarget(target.Name))
80 }
81
82 assert.False(t, rb.RemoveTarget("unknown target"))
83
84 // Round-robin
85 rrb := NewRoundRobinBalancer(targets)
86 e = echo.New()

Callers

nothing calls this directly

Calls 12

NewRandomBalancerFunction · 0.85
ProxyWithConfigFunction · 0.85
NewRoundRobinBalancerFunction · 0.85
ServeHTTPMethod · 0.80
SetMethod · 0.80
AddTargetMethod · 0.65
RemoveTargetMethod · 0.65
CloseMethod · 0.45
UseMethod · 0.45
StringMethod · 0.45
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…