Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/oauthex/client_test.go

raw symbols SCIP
1 // Copyright 2026 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package oauthex
6
7 import (
8 "reflect"
9 "strings"
10 "testing"
11 )
12
13 func TestClientCredentials_Validate(t *testing.T) {
14 tests := []struct {
15 name string
16 creds ClientCredentials
17 wantErr string
18 }{
19 {
20 name: "valid public client",
21 creds: ClientCredentials{ClientID: "my-client"},
22 },
23 {
24 name: "valid confidential client",
25 creds: ClientCredentials{
26 ClientID: "my-client",
27 ClientSecretAuth: &ClientSecretAuth{
28 ClientSecret: "my-secret",
29 },
30 },
31 },
32 {
33 name: "empty client ID",
34 creds: ClientCredentials{},
35 wantErr: "ClientID is required",
36 },
37 {
38 name: "empty secret in ClientSecretAuth",
39 creds: ClientCredentials{
40 ClientID: "my-client",
41 ClientSecretAuth: &ClientSecretAuth{},
42 },
43 wantErr: "ClientSecret is required",
44 },
45 }
46 for _, tt := range tests {
47 t.Run(tt.name, func(t *testing.T) {
48 err := tt.creds.Validate()
49 if tt.wantErr == "" {
50 if err != nil {
51 t.Fatalf("Validate() unexpected error: %v", err)
52 }
53 return
54 }
55 if err == nil {
56 t.Fatalf("Validate() expected error containing %q, got nil", tt.wantErr)
57 }
58 if !strings.Contains(err.Error(), tt.wantErr) {
59 t.Fatalf("Validate() error = %v, want error containing %q", err, tt.wantErr)
60 }
61 })
62 }
63 }
64
65 // TestClientCredentials_ValidateCoversAllAuthFields uses reflection to detect
66 // when new authentication method fields are added to ClientCredentials without
67 // updating Validate. If this test fails, update Validate() to handle the new
68 // field and increment knownAuthMethods.
69 func TestClientCredentials_ValidateCoversAllAuthFields(t *testing.T) {
70 const knownAuthMethods = 1 // ClientSecretAuth
71
72 typ := reflect.TypeOf(ClientCredentials{})
73 var pointerFields int
74 for i := range typ.NumField() {
75 f := typ.Field(i)
76 if f.Name == "ClientID" {
77 continue
78 }
79 if f.Type.Kind() != reflect.Ptr {
80 t.Errorf("field %q is %v, expected a pointer to an auth method struct", f.Name, f.Type.Kind())
81 }
82 pointerFields++
83 }
84
85 if pointerFields != knownAuthMethods {
86 t.Fatalf("ClientCredentials has %d auth method fields but Validate only knows about %d -- update Validate() and this test", pointerFields, knownAuthMethods)
87 }
88 }