To verify if your bearer token is still valid using @playwright/test, you can send a GET request to the authentication endpoint with the token in an Authorization header. If the token is valid, you'll get a 200 status code, otherwise, you'll get a 401 Unauthorized status code.
Here's an example test using the request
fixture:
import { test } from '@playwright/test';
test('verify bearer token validity', async ({ request }) => {
const response = await request.get('https://ray.run/auth', {
headers: { 'Authorization': `Bearer ${process.env.BEARER_TOKEN}` }
});
expect(response.status()).toBeOneOf([200, 401]);
if (response.status() === 401) {
console.log('Bearer Token Expired');
// Handle expired token, e.g., refresh it or log out the user
} else {
console.log('Bearer Token Valid');
// Continue with authenticated requests
}
});
In this test, you send a GET request to the authentication endpoint with your bearer token in an Authorization header. Then, you use an expect
assertion to check if the response status code is either 200 or 401.
If the status code is 200, the token is valid, and you can continue making authenticated requests. If the status code is 401, the token is expired, and you should handle it accordingly, such as refreshing the token or logging out the user.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].