-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/plugins/operation-headers/test/operation-headers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |