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

Function TestProxyWithNoTargetReturnsBadGateway

middleware/proxy_test.go:461–518  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

459}
460
461func TestProxyWithNoTargetReturnsBadGateway(t *testing.T) {
462 targetURL, _ := url.Parse("http://127.0.0.1:8080")
463 target := &ProxyTarget{Name: "target", URL: targetURL}
464 emptyAfterRemove := NewRoundRobinBalancer([]*ProxyTarget{target})
465 assert.True(t, emptyAfterRemove.RemoveTarget("target"))
466
467 testCases := []struct {
468 name string
469 balancer ProxyBalancer
470 }{
471 {
472 name: "random balancer with nil targets",
473 balancer: NewRandomBalancer(nil),
474 },
475 {
476 name: "round-robin balancer with nil targets",
477 balancer: NewRoundRobinBalancer(nil),
478 },
479 {
480 name: "round-robin balancer after removing last target",
481 balancer: emptyAfterRemove,
482 },
483 {
484 name: "custom balancer with nil target",
485 balancer: &customBalancer{},
486 },
487 {
488 name: "custom balancer with nil target URL",
489 balancer: &customBalancer{target: &ProxyTarget{Name: "target"}},
490 },
491 }
492
493 for _, tc := range testCases {
494 t.Run(tc.name, func(t *testing.T) {
495 e := echo.New()
496 errorHandlerCalled := false
497 e.Use(ProxyWithConfig(ProxyConfig{
498 Balancer: tc.balancer,
499 ErrorHandler: func(c *echo.Context, err error) error {
500 errorHandlerCalled = true
501 httpErr, ok := err.(*echo.HTTPError)
502 assert.True(t, ok, "expected http error to be passed to handler")
503 assert.Equal(t, http.StatusBadGateway, httpErr.Code, "expected http bad gateway error to be passed to handler")
504 return err
505 },
506 }))
507
508 req := httptest.NewRequest(http.MethodGet, "/", nil)
509 rec := httptest.NewRecorder()
510
511 assert.NotPanics(t, func() {
512 e.ServeHTTP(rec, req)
513 })
514 assert.True(t, errorHandlerCalled)
515 assert.Equal(t, http.StatusBadGateway, rec.Code)
516 })
517 }
518}

Callers

nothing calls this directly

Calls 6

NewRoundRobinBalancerFunction · 0.85
NewRandomBalancerFunction · 0.85
ProxyWithConfigFunction · 0.85
ServeHTTPMethod · 0.80
RemoveTargetMethod · 0.65
UseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…