MCPcopy
hub / github.com/dosco/graphjin / TestTokenExchange

Function TestTokenExchange

core/openapi/auth_test.go:177–221  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

175}
176
177func TestTokenExchange(t *testing.T) {
178 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
179 var body map[string]any
180 if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
181 t.Errorf("decode body: %v", err)
182 }
183 if body["apiKeyId"] != "kid" || body["apiKeySecret"] != "ksec" {
184 t.Errorf("body = %v", body)
185 }
186 // Mimic Salesforce MC Personalization shape — non-OAuth2 field names.
187 w.Header().Set("Content-Type", "application/json")
188 _ = json.NewEncoder(w).Encode(map[string]any{
189 "access_token": "is-token-xyz",
190 "expires_in": "3500", // string form, on purpose
191 })
192 }))
193 defer srv.Close()
194
195 p, err := NewAuthProvider(AuthConfig{
196 Scheme: "token_exchange",
197 TokenURL: srv.URL,
198 Request: &TokenExchangeRequest{
199 BodyFormat: "json",
200 Body: map[string]any{
201 "apiKeyId": "kid",
202 "apiKeySecret": "ksec",
203 },
204 },
205 Response: &TokenExchangeResponse{
206 TokenField: "access_token",
207 ExpiresField: "expires_in",
208 },
209 }, srv.Client())
210 if err != nil {
211 t.Fatal(err)
212 }
213
214 req := newTestRequest(t, "https://api.example.com/users")
215 if err := p.Apply(context.Background(), req, nil); err != nil {
216 t.Fatal(err)
217 }
218 if got := req.Header.Get("Authorization"); got != "Bearer is-token-xyz" {
219 t.Errorf("Authorization = %q", got)
220 }
221}
222
223func TestCachedTokenExpiry(t *testing.T) {
224 c := &cachedToken{}

Callers

nothing calls this directly

Calls 7

ApplyMethod · 0.95
NewAuthProviderFunction · 0.85
newTestRequestFunction · 0.85
ClientMethod · 0.80
SetMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected