|
| 1 | +package v0_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/danielgtaylor/huma/v2" |
| 9 | + "github.com/danielgtaylor/huma/v2/adapters/humago" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
| 12 | + v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0" |
| 13 | +) |
| 14 | + |
| 15 | +func TestVersionEndpoint(t *testing.T) { |
| 16 | + // Test cases |
| 17 | + testCases := []struct { |
| 18 | + name string |
| 19 | + versionInfo *v0.VersionBody |
| 20 | + expectedBody map[string]string |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "returns version information", |
| 24 | + versionInfo: &v0.VersionBody{ |
| 25 | + Version: "v1.2.3", |
| 26 | + GitCommit: "abc123def456", |
| 27 | + BuildTime: "2025-10-14T12:00:00Z", |
| 28 | + }, |
| 29 | + expectedBody: map[string]string{ |
| 30 | + "version": "v1.2.3", |
| 31 | + "git_commit": "abc123def456", |
| 32 | + "build_time": "2025-10-14T12:00:00Z", |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "returns dev version information", |
| 37 | + versionInfo: &v0.VersionBody{ |
| 38 | + Version: "dev", |
| 39 | + GitCommit: "unknown", |
| 40 | + BuildTime: "unknown", |
| 41 | + }, |
| 42 | + expectedBody: map[string]string{ |
| 43 | + "version": "dev", |
| 44 | + "git_commit": "unknown", |
| 45 | + "build_time": "unknown", |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + for _, tc := range testCases { |
| 51 | + t.Run(tc.name, func(t *testing.T) { |
| 52 | + // Create a new test API |
| 53 | + mux := http.NewServeMux() |
| 54 | + api := humago.New(mux, huma.DefaultConfig("Test API", "1.0.0")) |
| 55 | + |
| 56 | + // Register the version endpoint |
| 57 | + v0.RegisterVersionEndpoint(api, "/v0", tc.versionInfo) |
| 58 | + |
| 59 | + // Create a test request |
| 60 | + req := httptest.NewRequest(http.MethodGet, "/v0/version", nil) |
| 61 | + w := httptest.NewRecorder() |
| 62 | + |
| 63 | + // Serve the request |
| 64 | + mux.ServeHTTP(w, req) |
| 65 | + |
| 66 | + // Check the status code |
| 67 | + assert.Equal(t, http.StatusOK, w.Code) |
| 68 | + |
| 69 | + // Check the response body |
| 70 | + body := w.Body.String() |
| 71 | + assert.Contains(t, body, `"version":"`+tc.expectedBody["version"]+`"`) |
| 72 | + assert.Contains(t, body, `"git_commit":"`+tc.expectedBody["git_commit"]+`"`) |
| 73 | + assert.Contains(t, body, `"build_time":"`+tc.expectedBody["build_time"]+`"`) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments