Iโm writing tests for a service that uses either Bearer or Basic authorization but requires slightly different request URLs for the two authorization types. I want to be able to specify authorization at the collection level (except as noted in the next paragraph) and have the requests adjust themselves to match the requirements of the selected authorization type.
The collection includes a folder of requests that override the collection-level authorization in order to make sure both types of authorization are working, regardless of which type is selected for the other tests in the collection.
Iโve attempted to write a collection-level pre-request script that checks the requestโs authorization type in order to adjust the URL as necessary, but the Authorization header is not set when the script runs, and I havenโt been able to find any other way to check the requestโs authorization type.
For example, the following pre-request script always sets an auth-header variable to โ(none)โ if no Authorization header is manually defined, even if authorization is configured. The auth-type value is set to โ(no auth)โ.
pm.environment.set('auth-header', pm.request.getHeaders()['Authorization'] || '(none)');
if (pm.request.auth) {
pm.environment.set('auth-type', pm.request.auth.type || '(auth, no type)');
}
else {
pm.environment.set('auth-type', '(no auth)');
}
Am I missing something, or is this just not possible? I suppose I could set the authorization in pre-request scripts based on the value of an environment variable, but I think that would be harder to understand and maintain.