|
4 | 4 | "encoding/json" |
5 | 5 | "io" |
6 | 6 | "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/modelcontextprotocol/registry/cmd/publisher/commands" |
@@ -37,6 +39,57 @@ func TestPublishCommand_Success(t *testing.T) { |
37 | 39 | assert.NoError(t, err) |
38 | 40 | } |
39 | 41 |
|
| 42 | +func TestPublishCommand_PreservesNonASCIIDescription(t *testing.T) { |
| 43 | + server := SetupMockRegistryServer(t, |
| 44 | + func(w http.ResponseWriter, r *http.Request) { |
| 45 | + var req apiv0.ServerJSON |
| 46 | + require.NoError(t, json.NewDecoder(r.Body).Decode(&req)) |
| 47 | + assert.Equal(t, "Tools for agents — safely", req.Description) |
| 48 | + |
| 49 | + w.WriteHeader(http.StatusCreated) |
| 50 | + _ = json.NewEncoder(w).Encode(apiv0.ServerResponse{ |
| 51 | + Server: apiv0.ServerJSON{ |
| 52 | + Name: req.Name, |
| 53 | + Version: req.Version, |
| 54 | + }, |
| 55 | + }) |
| 56 | + }, |
| 57 | + nil, |
| 58 | + ) |
| 59 | + SetupTestToken(t, server.URL, "test-token") |
| 60 | + |
| 61 | + CreateTestServerJSON(t, apiv0.ServerJSON{ |
| 62 | + Schema: model.CurrentSchemaURL, |
| 63 | + Name: "com.example/test-server", |
| 64 | + Description: "Tools for agents — safely", |
| 65 | + Version: "1.0.0", |
| 66 | + }) |
| 67 | + |
| 68 | + require.NoError(t, commands.PublishCommand([]string{})) |
| 69 | +} |
| 70 | + |
| 71 | +func TestPublishCommand_RejectsInvalidUTF8ServerJSON(t *testing.T) { |
| 72 | + createRawServerJSON(t, []byte{ |
| 73 | + '{', '"', '$', 's', 'c', 'h', 'e', 'm', 'a', '"', ':', '"', '"', ',', |
| 74 | + '"', 'n', 'a', 'm', 'e', '"', ':', '"', 'c', 'o', 'm', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '/', 't', 'e', 's', 't', '"', ',', |
| 75 | + '"', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', '"', ':', '"', 'b', 'a', 'd', ' ', 0xe2, '"', ',', |
| 76 | + '"', 'v', 'e', 'r', 's', 'i', 'o', 'n', '"', ':', '"', '1', '.', '0', '.', '0', '"', '}', |
| 77 | + }) |
| 78 | + |
| 79 | + err := commands.PublishCommand([]string{}) |
| 80 | + require.Error(t, err) |
| 81 | + assert.Contains(t, err.Error(), "UTF-8") |
| 82 | +} |
| 83 | + |
| 84 | +func TestPublishCommand_RejectsUnpairedSurrogateEscape(t *testing.T) { |
| 85 | + createRawServerJSON(t, []byte(`{"$schema":"","name":"com.example/test","description":"bad \udc94","version":"1.0.0"}`)) |
| 86 | + |
| 87 | + err := commands.PublishCommand([]string{}) |
| 88 | + require.Error(t, err) |
| 89 | + assert.Contains(t, err.Error(), "unpaired UTF-16 surrogate") |
| 90 | + assert.Contains(t, err.Error(), "$.description") |
| 91 | +} |
| 92 | + |
40 | 93 | func TestPublishCommand_422ValidationFlow(t *testing.T) { |
41 | 94 | validateCallCount := 0 |
42 | 95 | publishCallCount := 0 |
@@ -102,6 +155,20 @@ func TestPublishCommand_422ValidationFlow(t *testing.T) { |
102 | 155 | assert.Equal(t, 1, validateCallCount, "validate endpoint should be called once after 422") |
103 | 156 | } |
104 | 157 |
|
| 158 | +func createRawServerJSON(t *testing.T, data []byte) { |
| 159 | + t.Helper() |
| 160 | + |
| 161 | + tempDir := t.TempDir() |
| 162 | + serverFile := filepath.Join(tempDir, "server.json") |
| 163 | + require.NoError(t, os.WriteFile(serverFile, data, 0600)) |
| 164 | + |
| 165 | + originalDir, err := os.Getwd() |
| 166 | + require.NoError(t, err) |
| 167 | + t.Cleanup(func() { _ = os.Chdir(originalDir) }) |
| 168 | + |
| 169 | + require.NoError(t, os.Chdir(tempDir)) |
| 170 | +} |
| 171 | + |
105 | 172 | func TestPublishCommand_422WithMultipleIssues(t *testing.T) { |
106 | 173 | validateCallCount := 0 |
107 | 174 |
|
|
0 commit comments