|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + ghcontext "github.com/github/github-mcp-server/pkg/context" |
| 9 | + "github.com/github/github-mcp-server/pkg/http/headers" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func captureFeatureFeatures(t *testing.T, r *http.Request) []string { |
| 15 | + t.Helper() |
| 16 | + |
| 17 | + var captured []string |
| 18 | + handler := WithRequestConfig(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 19 | + captured = ghcontext.GetHeaderFeatures(req.Context()) |
| 20 | + w.WriteHeader(http.StatusOK) |
| 21 | + })) |
| 22 | + |
| 23 | + rec := httptest.NewRecorder() |
| 24 | + handler.ServeHTTP(rec, r) |
| 25 | + require.Equal(t, http.StatusOK, rec.Code) |
| 26 | + return captured |
| 27 | +} |
| 28 | + |
| 29 | +func TestWithRequestConfigFeatureFlags(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + path string |
| 33 | + header string |
| 34 | + wantFeatures []string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "no flags anywhere", |
| 38 | + path: "/", |
| 39 | + wantFeatures: nil, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "header only", |
| 43 | + path: "/", |
| 44 | + header: "flag_a,flag_b", |
| 45 | + wantFeatures: []string{"flag_a", "flag_b"}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "query parameter only", |
| 49 | + path: "/?features=flag_a", |
| 50 | + wantFeatures: []string{"flag_a"}, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "query parameter takes precedence over header", |
| 54 | + path: "/?features=from_url", |
| 55 | + header: "from_header", |
| 56 | + wantFeatures: []string{"from_url"}, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "empty query parameter falls back to header", |
| 60 | + path: "/?features=", |
| 61 | + header: "from_header", |
| 62 | + wantFeatures: []string{"from_header"}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "query parameter coexists with other params", |
| 66 | + path: "/?foo=bar&features=flag_a&baz=qux", |
| 67 | + wantFeatures: []string{"flag_a"}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + r := httptest.NewRequest(http.MethodPost, tt.path, http.NoBody) |
| 74 | + if tt.header != "" { |
| 75 | + r.Header.Set(headers.MCPFeaturesHeader, tt.header) |
| 76 | + } |
| 77 | + got := captureFeatureFeatures(t, r) |
| 78 | + assert.ElementsMatch(t, tt.wantFeatures, got) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments