Fixed the usage of baseURL with the APIAuthInterceptor

This commit is contained in:
Christopher Allford 2020-06-23 11:53:00 -07:00
parent 323d06744c
commit 031bb6593f
2 changed files with 22 additions and 5 deletions

View File

@ -23,7 +23,7 @@ describe( 'APIAuthInterceptor', () => {
} );
it( 'should not run unless started', async () => {
moxios.stubRequest( 'https://api.test', { status: 200 } );
moxios.stubOnce( 'GET', 'https://api.test', { status: 200 } );
apiAuthInterceptor.stop();
await axiosInstance.get( 'https://api.test' );
@ -39,7 +39,7 @@ describe( 'APIAuthInterceptor', () => {
} );
it( 'should use basic auth for HTTPS', async () => {
moxios.stubRequest( 'https://api.test', { status: 200 } );
moxios.stubOnce( 'GET', 'https://api.test', { status: 200 } );
await axiosInstance.get( 'https://api.test' );
const request = moxios.requests.mostRecent();
@ -51,7 +51,7 @@ describe( 'APIAuthInterceptor', () => {
} );
it( 'should use OAuth 1.0a for HTTP', async () => {
moxios.stubRequest( 'http://api.test', { status: 200 } );
moxios.stubOnce( 'GET', 'http://api.test', { status: 200 } );
await axiosInstance.get( 'http://api.test' );
const request = moxios.requests.mostRecent();
@ -63,4 +63,20 @@ describe( 'APIAuthInterceptor', () => {
/^OAuth oauth_consumer_key="consumer_key".*oauth_signature_method="HMAC-SHA256".*oauth_version="1.0"/
);
} );
it( 'should work with base URL', async () => {
moxios.stubOnce( 'GET', '/test', { status: 200 } );
await axiosInstance.request( {
method: 'GET',
baseURL: 'https://api.test/',
url: '/test',
} );
const request = moxios.requests.mostRecent();
expect( request.headers ).toHaveProperty( 'Authorization' );
expect( request.headers.Authorization ).toEqual(
'Basic ' + btoa( 'consumer_key:consumer_secret' )
);
} );
} );

View File

@ -59,7 +59,8 @@ export class APIAuthInterceptor {
* @return {AxiosRequestConfig} The request with the additional authorization headers.
*/
private handleRequest( request: AxiosRequestConfig ): AxiosRequestConfig {
if ( request.url!.startsWith( 'https' ) ) {
const url = request.baseURL || '' + request.url || '';
if ( url.startsWith( 'https' ) ) {
request.auth = {
username: this.oauth.consumer.key,
password: this.oauth.consumer.secret,
@ -67,7 +68,7 @@ export class APIAuthInterceptor {
} else {
request.headers.Authorization = this.oauth.toHeader(
this.oauth.authorize( {
url: request.url!,
url,
method: request.method!,
} )
).Authorization;