Fixed the usage of baseURL with the APIAuthInterceptor
This commit is contained in:
parent
323d06744c
commit
031bb6593f
|
@ -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' )
|
||||
);
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue