|
9 | 9 | "net/http" |
10 | 10 | "net/http/httptest" |
11 | 11 | "os" |
| 12 | + "strings" |
12 | 13 | "testing" |
13 | 14 | "time" |
14 | 15 |
|
@@ -1449,3 +1450,145 @@ func TestProcessConfig_NRAutoDisable(t *testing.T) { |
1449 | 1450 | }) |
1450 | 1451 | } |
1451 | 1452 | } |
| 1453 | + |
| 1454 | +func TestInitHTTP_AdminPortSeparation(t *testing.T) { |
| 1455 | + // removed t.Parallel() — core tests mutate package-level globals |
| 1456 | + c := &cb{ |
| 1457 | + config: config.Config{ |
| 1458 | + GRPCPort: 19090, |
| 1459 | + HTTPPort: 19091, |
| 1460 | + AdminPort: 19092, |
| 1461 | + ListenHost: "127.0.0.1", |
| 1462 | + }, |
| 1463 | + svc: []CBService{&testService{}}, |
| 1464 | + } |
| 1465 | + svr, err := c.initHTTP(context.Background()) |
| 1466 | + if err != nil { |
| 1467 | + t.Fatalf("initHTTP failed: %v", err) |
| 1468 | + } |
| 1469 | + |
| 1470 | + // Gateway server should NOT serve admin endpoints. |
| 1471 | + for _, path := range []string{"/debug/pprof/", "/metrics"} { |
| 1472 | + req := httptest.NewRequest("GET", path, nil) |
| 1473 | + w := httptest.NewRecorder() |
| 1474 | + svr.Handler.ServeHTTP(w, req) |
| 1475 | + // Gateway has no routes for admin paths — must not return pprof/prometheus content. |
| 1476 | + body := w.Body.String() |
| 1477 | + if strings.Contains(body, "pprof") || strings.Contains(body, "go_goroutines") { |
| 1478 | + t.Errorf("admin endpoint %s should NOT be on gateway server when AdminPort is set (got body containing admin content)", path) |
| 1479 | + } |
| 1480 | + } |
| 1481 | + |
| 1482 | + // Admin server should serve admin endpoints. |
| 1483 | + if c.adminServer == nil { |
| 1484 | + t.Fatal("expected adminServer to be set when AdminPort > 0") |
| 1485 | + } |
| 1486 | + for _, path := range []string{"/debug/pprof/", "/metrics"} { |
| 1487 | + req := httptest.NewRequest("GET", path, nil) |
| 1488 | + w := httptest.NewRecorder() |
| 1489 | + c.adminServer.Handler.ServeHTTP(w, req) |
| 1490 | + if w.Code != http.StatusOK { |
| 1491 | + t.Fatalf("expected 200 for admin %s, got %d", path, w.Code) |
| 1492 | + } |
| 1493 | + } |
| 1494 | +} |
| 1495 | + |
| 1496 | +func TestInitHTTP_AdminPortZero_CombinedBehavior(t *testing.T) { |
| 1497 | + // removed t.Parallel() — core tests mutate package-level globals |
| 1498 | + c := &cb{ |
| 1499 | + config: config.Config{ |
| 1500 | + GRPCPort: 19090, |
| 1501 | + HTTPPort: 19091, |
| 1502 | + AdminPort: 0, |
| 1503 | + ListenHost: "127.0.0.1", |
| 1504 | + }, |
| 1505 | + svc: []CBService{&testService{}}, |
| 1506 | + } |
| 1507 | + svr, err := c.initHTTP(context.Background()) |
| 1508 | + if err != nil { |
| 1509 | + t.Fatalf("initHTTP failed: %v", err) |
| 1510 | + } |
| 1511 | + |
| 1512 | + // Admin server should NOT be created when AdminPort is 0. |
| 1513 | + if c.adminServer != nil { |
| 1514 | + t.Fatal("expected adminServer to be nil when AdminPort is 0") |
| 1515 | + } |
| 1516 | + |
| 1517 | + // Combined server should serve admin endpoints on HTTPPort. |
| 1518 | + for _, path := range []string{"/debug/pprof/", "/metrics"} { |
| 1519 | + req := httptest.NewRequest("GET", path, nil) |
| 1520 | + w := httptest.NewRecorder() |
| 1521 | + svr.Handler.ServeHTTP(w, req) |
| 1522 | + if w.Code != http.StatusOK { |
| 1523 | + t.Fatalf("expected 200 for %s on combined server, got %d", path, w.Code) |
| 1524 | + } |
| 1525 | + } |
| 1526 | +} |
| 1527 | + |
| 1528 | +func TestInitHTTP_AdminPortEqualsHTTPPort_CombinedMode(t *testing.T) { |
| 1529 | + // removed t.Parallel() — core tests mutate package-level globals |
| 1530 | + c := &cb{ |
| 1531 | + config: config.Config{ |
| 1532 | + GRPCPort: 19090, |
| 1533 | + HTTPPort: 19091, |
| 1534 | + AdminPort: 19091, // same as HTTPPort — should use combined mode |
| 1535 | + ListenHost: "127.0.0.1", |
| 1536 | + }, |
| 1537 | + svc: []CBService{&testService{}}, |
| 1538 | + } |
| 1539 | + svr, err := c.initHTTP(context.Background()) |
| 1540 | + if err != nil { |
| 1541 | + t.Fatalf("initHTTP failed: %v", err) |
| 1542 | + } |
| 1543 | + |
| 1544 | + // Should NOT create a separate admin server. |
| 1545 | + if c.adminServer != nil { |
| 1546 | + t.Fatal("expected adminServer to be nil when AdminPort == HTTPPort") |
| 1547 | + } |
| 1548 | + |
| 1549 | + // Combined server should serve admin endpoints. |
| 1550 | + for _, path := range []string{"/debug/pprof/", "/metrics"} { |
| 1551 | + req := httptest.NewRequest("GET", path, nil) |
| 1552 | + w := httptest.NewRecorder() |
| 1553 | + svr.Handler.ServeHTTP(w, req) |
| 1554 | + if w.Code != http.StatusOK { |
| 1555 | + t.Fatalf("expected 200 for %s on combined server, got %d", path, w.Code) |
| 1556 | + } |
| 1557 | + } |
| 1558 | +} |
| 1559 | + |
| 1560 | +func TestInitHTTP_AdminPortSwagger(t *testing.T) { |
| 1561 | + // removed t.Parallel() — core tests mutate package-level globals |
| 1562 | + handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 1563 | + w.Write([]byte("swagger-admin")) |
| 1564 | + }) |
| 1565 | + c := &cb{ |
| 1566 | + config: config.Config{ |
| 1567 | + GRPCPort: 19090, |
| 1568 | + HTTPPort: 19091, |
| 1569 | + AdminPort: 19092, |
| 1570 | + ListenHost: "127.0.0.1", |
| 1571 | + SwaggerURL: "/swagger/", |
| 1572 | + }, |
| 1573 | + svc: []CBService{&testService{}}, |
| 1574 | + openAPIHandler: handler, |
| 1575 | + } |
| 1576 | + _, err := c.initHTTP(context.Background()) |
| 1577 | + if err != nil { |
| 1578 | + t.Fatalf("initHTTP failed: %v", err) |
| 1579 | + } |
| 1580 | + |
| 1581 | + // Swagger should be on admin server. |
| 1582 | + if c.adminServer == nil { |
| 1583 | + t.Fatal("expected adminServer to be set when AdminPort > 0") |
| 1584 | + } |
| 1585 | + req := httptest.NewRequest("GET", "/swagger/index.html", nil) |
| 1586 | + w := httptest.NewRecorder() |
| 1587 | + c.adminServer.Handler.ServeHTTP(w, req) |
| 1588 | + if w.Code != http.StatusOK { |
| 1589 | + t.Fatalf("expected 200 for swagger on admin server, got %d", w.Code) |
| 1590 | + } |
| 1591 | + if w.Body.String() != "swagger-admin" { |
| 1592 | + t.Fatalf("expected 'swagger-admin' body, got %q", w.Body.String()) |
| 1593 | + } |
| 1594 | +} |
0 commit comments