|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "go.uber.org/zap" |
| 15 | + |
| 16 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 17 | +) |
| 18 | + |
| 19 | +type recordingImportController struct { |
| 20 | + mockImportController |
| 21 | + mu sync.Mutex |
| 22 | + added []string |
| 23 | +} |
| 24 | + |
| 25 | +func (m *recordingImportController) AddServer(_ context.Context, s *config.ServerConfig) error { |
| 26 | + m.mu.Lock() |
| 27 | + defer m.mu.Unlock() |
| 28 | + m.added = append(m.added, s.Name) |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +// RC4-IMPORT-001: a URL-only entry imported with a forced claude-desktop |
| 33 | +// format and skip_quarantine=true used to be persisted as a commandless stdio |
| 34 | +// server, which made the next core startup exit with code 4. |
| 35 | +func TestImportServersJSON_RejectsInvalidServerNotPersisted(t *testing.T) { |
| 36 | + mock := &recordingImportController{mockImportController: mockImportController{apiKey: "test-key"}} |
| 37 | + server := NewServer(mock, zap.NewNop().Sugar(), nil) |
| 38 | + |
| 39 | + body, _ := json.Marshal(ImportRequest{ |
| 40 | + Format: "claude-desktop", |
| 41 | + Content: `{"mcpServers": { |
| 42 | + "broken": {"url": "https://example.com/mcp"}, |
| 43 | + "good": {"command": "npx", "args": ["-y", "some-server"]} |
| 44 | + }}`, |
| 45 | + }) |
| 46 | + req := httptest.NewRequest("POST", "/api/v1/servers/import/json?skip_quarantine=true", bytes.NewReader(body)) |
| 47 | + req.Header.Set("Content-Type", "application/json") |
| 48 | + req.Header.Set("X-API-Key", "test-key") |
| 49 | + rr := httptest.NewRecorder() |
| 50 | + server.router.ServeHTTP(rr, req) |
| 51 | + |
| 52 | + require.Equal(t, http.StatusOK, rr.Code, rr.Body.String()) |
| 53 | + var wrapped wrappedImportResponse |
| 54 | + require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &wrapped)) |
| 55 | + |
| 56 | + require.Len(t, wrapped.Data.Imported, 1) |
| 57 | + assert.Equal(t, "good", wrapped.Data.Imported[0].Name) |
| 58 | + require.Len(t, wrapped.Data.Failed, 1) |
| 59 | + assert.Equal(t, "broken", wrapped.Data.Failed[0].Name) |
| 60 | + assert.Contains(t, wrapped.Data.Failed[0].Details, "command is required") |
| 61 | + |
| 62 | + mock.mu.Lock() |
| 63 | + defer mock.mu.Unlock() |
| 64 | + assert.Equal(t, []string{"good"}, mock.added, "the invalid server must never reach AddServer") |
| 65 | +} |
0 commit comments