Assert expected with url.EscapedPath method to obtain the path.
(t *testing.T)
| 27 | |
| 28 | // Assert expected with url.EscapedPath method to obtain the path. |
| 29 | func 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() |
nothing calls this directly
no test coverage detected
searching dependent graphs…