|
| 1 | +package registries |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestFindServerByID_BeyondSearchLimit reproduces Codex RV #1: a server that |
| 16 | +// sits past the first page / UI limit of a registry listing must still be |
| 17 | +// addable via FindServerByID, not merely searchable. The official listing here |
| 18 | +// returns 60 entries on a single page with the target at index 55 — past the |
| 19 | +// old 50-entry add cap that SearchServers applied before matching. |
| 20 | +func TestFindServerByID_BeyondSearchLimit(t *testing.T) { |
| 21 | + const target = "io.example/target-server" |
| 22 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 23 | + items := make([]map[string]interface{}, 0, 60) |
| 24 | + for i := 0; i < 60; i++ { |
| 25 | + name := fmt.Sprintf("io.example/filler-%02d", i) |
| 26 | + if i == 55 { |
| 27 | + name = target |
| 28 | + } |
| 29 | + items = append(items, map[string]interface{}{ |
| 30 | + "server": map[string]interface{}{ |
| 31 | + "name": name, |
| 32 | + "description": "test server", |
| 33 | + "remotes": []interface{}{ |
| 34 | + map[string]interface{}{"type": "streamable-http", "url": "https://example.com/" + name}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }) |
| 38 | + } |
| 39 | + w.Header().Set("Content-Type", "application/json") |
| 40 | + _ = json.NewEncoder(w).Encode(map[string]interface{}{ |
| 41 | + "servers": items, |
| 42 | + "metadata": map[string]interface{}{}, // no nextCursor => single page |
| 43 | + }) |
| 44 | + })) |
| 45 | + defer srv.Close() |
| 46 | + |
| 47 | + registryList = []RegistryEntry{ |
| 48 | + {ID: "official", Name: "Official", ServersURL: srv.URL, Protocol: protocolOfficial}, |
| 49 | + } |
| 50 | + |
| 51 | + got, err := FindServerByID(context.Background(), "official", target, nil) |
| 52 | + require.NoError(t, err) |
| 53 | + require.NotNil(t, got) |
| 54 | + assert.Equal(t, target, got.ID, "server beyond the UI/search limit must still be addable") |
| 55 | +} |
| 56 | + |
| 57 | +// TestFetchServers_SendsConfiguredAPIKey covers Codex RV #2 for the generic |
| 58 | +// (non-official) request builder used by Pulse: a configured key must reach the |
| 59 | +// wire as a Bearer token, not be read-but-ignored. |
| 60 | +func TestFetchServers_SendsConfiguredAPIKey(t *testing.T) { |
| 61 | + const key = "pulse-secret-123" |
| 62 | + t.Setenv(RegistryKeyEnvVar("pulse"), key) |
| 63 | + |
| 64 | + var gotAuth string |
| 65 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 66 | + gotAuth = r.Header.Get("Authorization") |
| 67 | + w.Header().Set("Content-Type", "application/json") |
| 68 | + _, _ = w.Write([]byte(`{"servers":[]}`)) |
| 69 | + })) |
| 70 | + defer srv.Close() |
| 71 | + |
| 72 | + reg := &RegistryEntry{ID: "pulse", Protocol: "custom/pulse", ServersURL: srv.URL, RequiresKey: true} |
| 73 | + _, err := fetchServers(context.Background(), reg, nil, "") |
| 74 | + require.NoError(t, err) |
| 75 | + assert.Equal(t, "Bearer "+key, gotAuth, "configured registry key must be sent as a Bearer token") |
| 76 | +} |
| 77 | + |
| 78 | +// TestFetchOfficialServers_SendsConfiguredAPIKey covers Codex RV #2 for the |
| 79 | +// official-protocol request builder used by Smithery. |
| 80 | +func TestFetchOfficialServers_SendsConfiguredAPIKey(t *testing.T) { |
| 81 | + const key = "smithery-secret-456" |
| 82 | + t.Setenv(RegistryKeyEnvVar("smithery"), key) |
| 83 | + |
| 84 | + var gotAuth string |
| 85 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 86 | + gotAuth = r.Header.Get("Authorization") |
| 87 | + w.Header().Set("Content-Type", "application/json") |
| 88 | + _, _ = w.Write([]byte(`{"servers":[],"metadata":{}}`)) |
| 89 | + })) |
| 90 | + defer srv.Close() |
| 91 | + |
| 92 | + reg := &RegistryEntry{ID: "smithery", Protocol: protocolOfficial, ServersURL: srv.URL, RequiresKey: true} |
| 93 | + _, err := fetchOfficialServers(context.Background(), reg, nil, "") |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Equal(t, "Bearer "+key, gotAuth, "configured registry key must be sent as a Bearer token") |
| 96 | +} |
| 97 | + |
| 98 | +// TestFetchServers_NoKeyNoAuthHeader ensures no Authorization header is attached |
| 99 | +// when no key is configured (never send a bare "Bearer "). |
| 100 | +func TestFetchServers_NoKeyNoAuthHeader(t *testing.T) { |
| 101 | + t.Setenv(RegistryKeyEnvVar("pulse"), "") |
| 102 | + |
| 103 | + var gotAuth string |
| 104 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 105 | + gotAuth = r.Header.Get("Authorization") |
| 106 | + w.Header().Set("Content-Type", "application/json") |
| 107 | + _, _ = w.Write([]byte(`{"servers":[]}`)) |
| 108 | + })) |
| 109 | + defer srv.Close() |
| 110 | + |
| 111 | + reg := &RegistryEntry{ID: "pulse", Protocol: "custom/pulse", ServersURL: srv.URL} |
| 112 | + _, err := fetchServers(context.Background(), reg, nil, "") |
| 113 | + require.NoError(t, err) |
| 114 | + assert.Empty(t, gotAuth, "no Authorization header should be sent without a configured key") |
| 115 | +} |
0 commit comments