|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/docker/docker-agent/pkg/config" |
| 16 | +) |
| 17 | + |
| 18 | +// httpDoStatus is a slim variant of httpDo that exposes the response |
| 19 | +// status code. The standard helper assumes 2xx and only returns the body; |
| 20 | +// these tests assert on 4xx, so they need direct access to the status. |
| 21 | +func httpDoStatus(t *testing.T, ctx context.Context, method, socketPath, path string) int { |
| 22 | + t.Helper() |
| 23 | + req, err := http.NewRequestWithContext(ctx, method, "http://_"+path, http.NoBody) |
| 24 | + require.NoError(t, err) |
| 25 | + client := &http.Client{ |
| 26 | + Transport: &http.Transport{ |
| 27 | + DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| 28 | + var d net.Dialer |
| 29 | + return d.DialContext(ctx, "unix", strings.TrimPrefix(socketPath, "unix://")) |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + resp, err := client.Do(req) |
| 34 | + require.NoError(t, err) |
| 35 | + defer resp.Body.Close() |
| 36 | + _, err = io.Copy(io.Discard, resp.Body) |
| 37 | + require.NoError(t, err) |
| 38 | + return resp.StatusCode |
| 39 | +} |
| 40 | + |
| 41 | +func startServerBare(t *testing.T, ctx context.Context) string { |
| 42 | + t.Helper() |
| 43 | + var store mockStore |
| 44 | + runConfig := config.RuntimeConfig{} |
| 45 | + sources, err := config.ResolveSources(t.TempDir(), nil) |
| 46 | + require.NoError(t, err) |
| 47 | + srv, err := New(ctx, store, &runConfig, 0, sources, "") |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + socketPath := "unix://" + filepath.Join(t.TempDir(), "sock") |
| 51 | + ln, err := Listen(ctx, socketPath) |
| 52 | + require.NoError(t, err) |
| 53 | + go func() { <-ctx.Done(); _ = ln.Close() }() |
| 54 | + go func() { _ = srv.Serve(ctx, ln) }() |
| 55 | + return socketPath |
| 56 | +} |
| 57 | + |
| 58 | +// The happy path (waiter registered, callback delivered) is covered end |
| 59 | +// to end in TestUnmanagedOAuthFlow_DriveFlow_AcceptsDirectCallback in |
| 60 | +// pkg/tools/mcp. The server-side tests here focus on the input |
| 61 | +// validation and the 404 response shape so the embedder's HTTP client |
| 62 | +// can rely on it. |
| 63 | + |
| 64 | +// Short test names because the macOS unix-socket path limit (104 bytes) |
| 65 | +// includes t.TempDir() which embeds the test name. |
| 66 | + |
| 67 | +func TestMcpOAuthCb_Unknown(t *testing.T) { |
| 68 | + ctx := t.Context() |
| 69 | + lnPath := startServerBare(t, ctx) |
| 70 | + |
| 71 | + status := httpDoStatus(t, ctx, http.MethodPost, lnPath, |
| 72 | + "/api/mcp-oauth/callback?state=unknown-state&code=abc") |
| 73 | + assert.Equal(t, http.StatusNotFound, status) |
| 74 | +} |
| 75 | + |
| 76 | +func TestMcpOAuthCb_NoState(t *testing.T) { |
| 77 | + ctx := t.Context() |
| 78 | + lnPath := startServerBare(t, ctx) |
| 79 | + |
| 80 | + status := httpDoStatus(t, ctx, http.MethodPost, lnPath, |
| 81 | + "/api/mcp-oauth/callback?code=abc") |
| 82 | + assert.Equal(t, http.StatusBadRequest, status) |
| 83 | +} |
| 84 | + |
| 85 | +func TestMcpOAuthCb_NoCode(t *testing.T) { |
| 86 | + ctx := t.Context() |
| 87 | + lnPath := startServerBare(t, ctx) |
| 88 | + |
| 89 | + status := httpDoStatus(t, ctx, http.MethodPost, lnPath, |
| 90 | + "/api/mcp-oauth/callback?state=some-state") |
| 91 | + assert.Equal(t, http.StatusBadRequest, status) |
| 92 | +} |
0 commit comments