-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(GraphQL): expose request cookies in "cookies" argument (#1853)
- Loading branch information
1 parent
e628278
commit 463b74f
Showing
2 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
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
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,90 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { graphql, HttpResponse } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const server = setupServer( | ||
graphql.query('GetUser', ({ cookies }) => { | ||
const { session } = cookies | ||
|
||
if (!session) { | ||
return HttpResponse.json({ | ||
errors: [ | ||
{ | ||
name: 'Unauthorized', | ||
message: 'Must be authorized to query "GetUser"', | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
return HttpResponse.json({ | ||
data: { | ||
user: { | ||
name: 'John', | ||
}, | ||
}, | ||
}) | ||
}), | ||
) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
it('responds to a GraphQL query when the request has the cookies', async () => { | ||
const response = await fetch('http://localhost/graphql', { | ||
method: 'POST', | ||
headers: { | ||
Cookie: 'session=superSecret', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: ` | ||
query GetUser { | ||
user { | ||
name | ||
} | ||
} | ||
`, | ||
}), | ||
}) | ||
const result = await response.json() | ||
|
||
expect(result.data).toEqual({ | ||
user: { name: 'John' }, | ||
}) | ||
expect(result.errors).toBeUndefined() | ||
}) | ||
|
||
it('errors on a GraphQL query when the request is missig the cookies', async () => { | ||
const response = await fetch('http://localhost/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: ` | ||
query GetUser { | ||
user { | ||
name | ||
} | ||
} | ||
`, | ||
}), | ||
}) | ||
const result = await response.json() | ||
|
||
expect(result.errors).toEqual([ | ||
{ | ||
name: 'Unauthorized', | ||
message: 'Must be authorized to query "GetUser"', | ||
}, | ||
]) | ||
expect(result.data).toBeUndefined() | ||
}) |