MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestStreamableClientOAuth_401

Function TestStreamableClientOAuth_401

mcp/streamable_client_test.go:982–1018  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

980}
981
982func TestStreamableClientOAuth_401(t *testing.T) {
983 ctx := context.Background()
984 oauthHandler := &mockOAuthHandler{token: nil}
985
986 fake := &fakeStreamableServer{
987 t: t,
988 responses: fakeResponses{
989 {"POST", "", methodInitialize, ""}: {
990 header: header{
991 "Content-Type": "application/json",
992 sessionIDHeader: "123",
993 },
994 body: jsonBody(t, initResp),
995 },
996 },
997 }
998 verifier := func(ctx context.Context, token string, req *http.Request) (*auth.TokenInfo, error) {
999 // Accept any token.
1000 return &auth.TokenInfo{Expiration: time.Now().Add(time.Hour)}, nil
1001 }
1002 httpServer := httptest.NewServer(auth.RequireBearerToken(verifier, nil)(fake))
1003 t.Cleanup(httpServer.Close)
1004
1005 transport := &StreamableClientTransport{
1006 Endpoint: httpServer.URL,
1007 OAuthHandler: oauthHandler,
1008 }
1009 client := NewClient(testImpl, nil)
1010 _, err := client.Connect(ctx, transport, nil)
1011 if err == nil || !strings.Contains(err.Error(), "Unauthorized") {
1012 t.Fatalf("client.Connect() error does not contain 'Unauthorized': %v", err)
1013 }
1014
1015 if !oauthHandler.authorizeCalled {
1016 t.Errorf("expected Authorize to be called")
1017 }
1018}
1019
1020// blockingCountingOAuthHandler is an OAuthHandler that blocks inside
1021// Authorize until the caller's context is cancelled, then returns a custom

Callers

nothing calls this directly

Calls 5

ConnectMethod · 0.95
RequireBearerTokenFunction · 0.92
jsonBodyFunction · 0.85
NewClientFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…