Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Feb 28, 2024
1 parent 5338e8f commit c31b7f4
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/plugins/operation-headers/test/operation-headers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useOperationHeaders } from '@graphql-mesh/plugin-operation-headers';
import { wrapFetchWithPlugins } from '@graphql-mesh/runtime';

describe('Operation Headers', () => {
it('works', async () => {
const wrappedFetch = wrapFetchWithPlugins([
{
onFetch({ setFetchFn }) {
setFetchFn(async (url, opts) => {
const req = new Request(url, opts);
if (req.headers.get('Authorization') !== 'Bearer test') {
return new Response('Unauthorized', { status: 401 });
}
return Response.json({ data: 'test' });
});
},
},
useOperationHeaders(({ context }) => ({
Authorization: `Bearer ${context.headers['x-auth-token']}`,
})),
]);
const authorizedRes = await wrappedFetch(
'http://localhost:3000',
{},
{
headers: {
'x-auth-token': 'test',
},
},
);
expect(authorizedRes.status).toBe(200);
const authorizedResJson = await authorizedRes.json();
expect(authorizedResJson).toEqual({ data: 'test' });

const unauthorizedRes = await wrappedFetch(
'http://localhost:3000',
{},
{
headers: {
'x-auth-token': 'wrong-token',
},
},
);
expect(unauthorizedRes.status).toBe(401);
const unauthorizedResText = await unauthorizedRes.text();
expect(unauthorizedResText).toBe('Unauthorized');
});
});

0 comments on commit c31b7f4

Please sign in to comment.