diff --git a/packages/aws-appsync-auth-link/__tests__/link/auth-link-test.ts b/packages/aws-appsync-auth-link/__tests__/link/auth-link-test.ts index 8f3f24f3..a6597b69 100644 --- a/packages/aws-appsync-auth-link/__tests__/link/auth-link-test.ts +++ b/packages/aws-appsync-auth-link/__tests__/link/auth-link-test.ts @@ -55,6 +55,41 @@ describe("Auth link", () => { execute(testLink, { query }).subscribe({ }) }); + test('Test AMAZON_COGNITO_USER_POOLS authorizer uses result of async jwtToken method', (done) => { + const query = gql`query { someQuery { aField } }` + + const initialiLink = authLink({ + auth: { + type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS, + jwtToken: 'token' + }, + region: 'us-east-1', + url: 'https://xxxxx.appsync-api.amazonaws.com/graphql' + }) + + const link = authLink({ + auth: { + type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS, + jwtToken: 'updated-token' + }, + region: 'us-east-1', + url: 'https://xxxxx.appsync-api.amazonaws.com/graphql' + }) + + + const spyLink = new ApolloLink((operation, forward) => { + const { headers: { Authorization} } = operation.getContext(); + expect(Authorization).toBe('updated-token'); + done(); + + return new Observable(() => {}); + }) + + const testLink = ApolloLink.from([initialiLink, link, spyLink]); + + execute(testLink, { query }).subscribe({ }) + }); + test('Test OPENID_CONNECT authorizer for queries', (done) => { const query = gql`query { someQuery { aField } }` diff --git a/packages/aws-appsync-auth-link/src/auth-link.ts b/packages/aws-appsync-auth-link/src/auth-link.ts index 8670080d..85c5e00a 100644 --- a/packages/aws-appsync-auth-link/src/auth-link.ts +++ b/packages/aws-appsync-auth-link/src/auth-link.ts @@ -61,8 +61,8 @@ const headerBasedAuth = async ({ header, value }: Headers = { header: '', value: const headerValue = typeof value === 'function' ? await value.call(undefined) : await value; headers = { + ...headers, ...{ [header]: headerValue }, - ...headers }; }