-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
PassThrough
AuthClient (#1771)
* feat: `PassThrough` AuthClient * refactor: Expand Types
- Loading branch information
Showing
5 changed files
with
133 additions
and
27 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,65 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {GaxiosOptions} from 'gaxios'; | ||
import {AuthClient} from './authclient'; | ||
import {GetAccessTokenResponse, Headers} from './oauth2client'; | ||
|
||
/** | ||
* An AuthClient without any Authentication information. Useful for: | ||
* - Anonymous access | ||
* - Local Emulators | ||
* - Testing Environments | ||
* | ||
*/ | ||
export class PassThroughClient extends AuthClient { | ||
/** | ||
* Creates a request without any authentication headers or checks. | ||
* | ||
* @remarks | ||
* | ||
* In testing environments it may be useful to change the provided | ||
* {@link AuthClient.transporter} for any desired request overrides/handling. | ||
* | ||
* @param opts | ||
* @returns The response of the request. | ||
*/ | ||
async request<T>(opts: GaxiosOptions) { | ||
return this.transporter.request<T>(opts); | ||
} | ||
|
||
/** | ||
* A required method of the base class. | ||
* Always will return an empty object. | ||
* | ||
* @returns {} | ||
*/ | ||
async getAccessToken(): Promise<GetAccessTokenResponse> { | ||
return {}; | ||
} | ||
|
||
/** | ||
* A required method of the base class. | ||
* Always will return an empty object. | ||
* | ||
* @returns {} | ||
*/ | ||
async getRequestHeaders(): Promise<Headers> { | ||
return {}; | ||
} | ||
} | ||
|
||
const a = new PassThroughClient(); | ||
|
||
a.getAccessToken(); |
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
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,62 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {strict as assert} from 'assert'; | ||
|
||
import * as nock from 'nock'; | ||
|
||
import {PassThroughClient} from '../src'; | ||
|
||
describe('AuthClient', () => { | ||
before(async () => { | ||
nock.disableNetConnect(); | ||
}); | ||
|
||
afterEach(async () => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe('#getAccessToken', () => { | ||
it('should return an empty object', async () => { | ||
const client = new PassThroughClient(); | ||
const token = await client.getAccessToken(); | ||
|
||
assert.deepEqual(token, {}); | ||
}); | ||
}); | ||
|
||
describe('#getRequestHeaders', () => { | ||
it('should return an empty object', async () => { | ||
const client = new PassThroughClient(); | ||
const token = await client.getRequestHeaders(); | ||
|
||
assert.deepEqual(token, {}); | ||
}); | ||
}); | ||
|
||
describe('#request', () => { | ||
it('should return the expected response', async () => { | ||
const url = 'https://google.com'; | ||
const example = {test: 'payload'}; | ||
const scope = nock(url).get('/').reply(200, example); | ||
|
||
const client = new PassThroughClient(); | ||
const response = await client.request({url}); | ||
|
||
assert.deepEqual(response.data, example); | ||
|
||
scope.done(); | ||
}); | ||
}); | ||
}); |