Skip to content

test(auth): add functional test for jwtaccess cache segregation#8646

Open
westarle wants to merge 1 commit into
googleapis:mainfrom
westarle:test-fixes/node-cache-segregation
Open

test(auth): add functional test for jwtaccess cache segregation#8646
westarle wants to merge 1 commit into
googleapis:mainfrom
westarle:test-fixes/node-cache-segregation

Conversation

@westarle

Copy link
Copy Markdown

Add cache segregation test to functionally verify JWTAccess isolates token caches correctly by scopes and URLs.

Add cache segregation test to functionally verify JWTAccess isolates token caches correctly by scopes and URLs.

TAG=agy
CONV=4aafb1cc-33ea-453d-968d-7e69305c0b3a

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a new test case to verify cache isolation by scopes and URLs when retrieving request headers. However, the test contains several critical issues that will cause it to fail at runtime, including missing await keywords for asynchronous calls, incorrect usage of .get() on a plain object, and a typo in the test URLs. A code suggestion has been provided to resolve these issues.

Comment on lines +225 to +251
const testUri1 = 'http:/example.com/service1';
const testUri2 = 'http:/example.com/service2';

// 1. Different scopes for the same URL should return different tokens
const headersScope1 = client.getRequestHeaders(testUri1, undefined, 'scope1');
const tokenScope1 = headersScope1.get('authorization');
assert(tokenScope1);

const headersScope2 = client.getRequestHeaders(testUri1, undefined, 'scope2');
const tokenScope2 = headersScope2.get('authorization');
assert(tokenScope2);
assert.notStrictEqual(tokenScope1, tokenScope2);

// 2. Different URLs (without scopes) should return different tokens (different audience)
const headersUrl1 = client.getRequestHeaders(testUri1);
const tokenUrl1 = headersUrl1.get('authorization');
assert(tokenUrl1);

const headersUrl2 = client.getRequestHeaders(testUri2);
const tokenUrl2 = headersUrl2.get('authorization');
assert(tokenUrl2);
assert.notStrictEqual(tokenUrl1, tokenUrl2);

// 3. Verify cache hit works for the exact same parameters
const headersScope1Cached = client.getRequestHeaders(testUri1, undefined, 'scope1');
const tokenScope1Cached = headersScope1Cached.get('authorization');
assert.strictEqual(tokenScope1, tokenScope1Cached);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There are multiple critical issues in this test that will cause it to fail at runtime:

  1. Missing await: getRequestHeaders is an asynchronous method returning a Promise<Headers>. It must be awaited to resolve the headers.
  2. Invalid .get() call: The returned Headers is a plain object ({[key: string]: string}), not a Map or a Headers class instance. Calling .get('authorization') will throw a TypeError. Instead, access the Authorization property directly.
  3. URL Typo: The test URLs are missing a slash (http:/ instead of http://).
    const testUri1 = 'http://example.com/service1';
    const testUri2 = 'http://example.com/service2';

    // 1. Different scopes for the same URL should return different tokens
    const headersScope1 = await client.getRequestHeaders(testUri1, undefined, 'scope1');
    const tokenScope1 = headersScope1.Authorization;
    assert(tokenScope1);

    const headersScope2 = await client.getRequestHeaders(testUri1, undefined, 'scope2');
    const tokenScope2 = headersScope2.Authorization;
    assert(tokenScope2);
    assert.notStrictEqual(tokenScope1, tokenScope2);

    // 2. Different URLs (without scopes) should return different tokens (different audience)
    const headersUrl1 = await client.getRequestHeaders(testUri1);
    const tokenUrl1 = headersUrl1.Authorization;
    assert(tokenUrl1);

    const headersUrl2 = await client.getRequestHeaders(testUri2);
    const tokenUrl2 = headersUrl2.Authorization;
    assert(tokenUrl2);
    assert.notStrictEqual(tokenUrl1, tokenUrl2);

    // 3. Verify cache hit works for the exact same parameters
    const headersScope1Cached = await client.getRequestHeaders(testUri1, undefined, 'scope1');
    const tokenScope1Cached = headersScope1Cached.Authorization;
    assert.strictEqual(tokenScope1, tokenScope1Cached);

@pearigee pearigee Jun 15, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It looks like there are multiple getRequestHeaders methods that are Promises . . . but the JWTAccess one is not. The test looks correct.
Image

https://github.com/search?q=repo%3Agoogleapis%2Fgoogle-cloud-node+path%3A%2F%5Ecore%5C%2Fpackages%5C%2Fgoogle-auth-library-nodejs%5C%2F%2F+getRequestHeaders&type=code

  1. Again, I think Gemini is wrong here. The data returned is a Headers object, which does have a get method:

    const headers = new Headers({authorization: `Bearer ${signedJWT}`});

  2. This is fair, but clearly a nit.

@westarle westarle marked this pull request as ready for review June 15, 2026 15:33
@westarle westarle requested a review from a team as a code owner June 15, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants