package server import ( "errors" "net/http" "net/http/httptest" "reflect" "testing" "github.com/proxy-pool/proxy-pool/internal/domain/routing" ) func TestRulesRouterReturnsMatchedUpstreams(t *testing.T) { t.Parallel() rules, err := routing.Compile([]routing.Rule{{ Name: "api", Match: routing.Match{HostRegex: `^example\.test$`, Methods: []string{http.MethodGet}, PathRegex: `^/v1/`}, Upstreams: []string{"provider-a", "provider-b"}, Action: routing.ActionProxy, }}) if err != nil { t.Fatalf("routing.Compile() error = %v", err) } router := NewRulesRouter(rules) request := httptest.NewRequest(http.MethodGet, "http://example.test/v1/items", nil) result, err := router.Route(request) if err != nil { t.Fatalf("Route() error = %v", err) } if !reflect.DeepEqual(result.Upstreams, []string{"provider-a", "provider-b"}) { t.Fatalf("upstreams = %v", result.Upstreams) } } func TestRulesRouterRejectsExplicitRejectAndMissingRoute(t *testing.T) { t.Parallel() rules, err := routing.Compile([]routing.Rule{{ Name: "blocked", Match: routing.Match{HostRegex: `^blocked\.test$`}, Action: routing.ActionReject, }}) if err != nil { t.Fatalf("routing.Compile() error = %v", err) } router := NewRulesRouter(rules) if _, err := router.Route(httptest.NewRequest(http.MethodGet, "http://blocked.test/", nil)); !errors.Is(err, ErrRouteRejected) { t.Fatalf("blocked Route() error = %v", err) } if _, err := router.Route(httptest.NewRequest(http.MethodGet, "http://missing.test/", nil)); !errors.Is(err, ErrRouteNotFound) { t.Fatalf("missing Route() error = %v", err) } }