|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/github/gh-aw-mcpg/internal/config" |
| 12 | + "github.com/github/gh-aw-mcpg/internal/difc" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestReflectEndpoint_BothModes_NoAuthRequired(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + createServer func(addr string, us *UnifiedServer, apiKey, hmacSecret string) *http.Server |
| 21 | + }{ |
| 22 | + {name: "routed mode", createServer: CreateHTTPServerForRoutedMode}, |
| 23 | + {name: "gateway mode", createServer: CreateHTTPServerForMCP}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, tt := range tests { |
| 27 | + t.Run(tt.name, func(t *testing.T) { |
| 28 | + us, err := NewUnified(context.Background(), &config.Config{Servers: map[string]*config.ServerConfig{}}) |
| 29 | + require.NoError(t, err) |
| 30 | + t.Cleanup(func() { us.Close() }) |
| 31 | + |
| 32 | + us.AgentRegistry.Register("proxy", []difc.Tag{"repo:github/private-repo"}, []difc.Tag{"approved"}) |
| 33 | + us.AgentRegistry.Register("abc123def456", nil, []difc.Tag{"unapproved"}) |
| 34 | + |
| 35 | + httpServer := tt.createServer(":0", us, "test-api-key", "") |
| 36 | + req := httptest.NewRequest(http.MethodGet, "/reflect", nil) |
| 37 | + w := httptest.NewRecorder() |
| 38 | + |
| 39 | + httpServer.Handler.ServeHTTP(w, req) |
| 40 | + |
| 41 | + require.Equal(t, http.StatusOK, w.Code) |
| 42 | + require.Equal(t, "application/json", w.Header().Get("Content-Type")) |
| 43 | + |
| 44 | + var got difc.ReflectResponse |
| 45 | + require.NoError(t, json.NewDecoder(w.Body).Decode(&got)) |
| 46 | + assert.Equal(t, us.Mode.String(), got.Mode) |
| 47 | + assert.ElementsMatch(t, []string{"repo:github/private-repo"}, got.Agents["proxy"].Secrecy) |
| 48 | + assert.ElementsMatch(t, []string{"approved"}, got.Agents["proxy"].Integrity) |
| 49 | + assert.Empty(t, got.Agents["abc123def456"].Secrecy) |
| 50 | + assert.ElementsMatch(t, []string{"unapproved"}, got.Agents["abc123def456"].Integrity) |
| 51 | + _, err = time.Parse(time.RFC3339, got.Timestamp) |
| 52 | + assert.NoError(t, err) |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestReflectEndpoint_EmptyRegistry(t *testing.T) { |
| 58 | + us, err := NewUnified(context.Background(), &config.Config{Servers: map[string]*config.ServerConfig{}}) |
| 59 | + require.NoError(t, err) |
| 60 | + t.Cleanup(func() { us.Close() }) |
| 61 | + |
| 62 | + httpServer := CreateHTTPServerForMCP(":0", us, "", "") |
| 63 | + req := httptest.NewRequest(http.MethodGet, "/reflect", nil) |
| 64 | + w := httptest.NewRecorder() |
| 65 | + httpServer.Handler.ServeHTTP(w, req) |
| 66 | + |
| 67 | + require.Equal(t, http.StatusOK, w.Code) |
| 68 | + |
| 69 | + var got difc.ReflectResponse |
| 70 | + require.NoError(t, json.NewDecoder(w.Body).Decode(&got)) |
| 71 | + assert.Empty(t, got.Agents) |
| 72 | +} |
0 commit comments