|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import axios from 'axios'; |
16 | 15 | import * as sinon from 'sinon'; |
17 | 16 | import { expect } from 'chai'; |
18 | 17 | import { getExecutionRegion } from '../../src/cloud_region_resolver'; |
19 | 18 |
|
20 | | -describe('getExecutionRegion', () => { |
21 | | - let axiosGetStub: sinon.SinonStub; |
| 19 | +describe('getExecutionRegion (with fetch)', () => { |
| 20 | + let fetchStub: sinon.SinonStub; |
22 | 21 |
|
23 | 22 | beforeEach(() => { |
24 | | - axiosGetStub = sinon.stub(axios, 'get'); |
| 23 | + fetchStub = sinon.stub(global, 'fetch'); |
25 | 24 | }); |
26 | 25 |
|
27 | 26 | afterEach(() => { |
28 | | - axiosGetStub.restore(); |
| 27 | + fetchStub.restore(); |
29 | 28 | }); |
30 | 29 |
|
31 | 30 | it('should retrieve the region from the metadata server', async () => { |
32 | | - axiosGetStub.resolves({ data: 'projects/123456789/regions/us-east1' }); |
| 31 | + const mockResponse = { |
| 32 | + ok: true, |
| 33 | + text: () => Promise.resolve('projects/123456789/regions/us-east1'), |
| 34 | + }; |
| 35 | + fetchStub.resolves(mockResponse as any); |
33 | 36 |
|
34 | 37 | const region = await getExecutionRegion(); |
35 | 38 | expect(region).to.equal('us-east1'); |
36 | | - expect(axiosGetStub.calledOnce).to.be.true; |
| 39 | + expect(fetchStub.calledOnce).to.be.true; |
37 | 40 | }); |
38 | 41 |
|
39 | 42 | it('should handle errors from the metadata server', async () => { |
40 | | - axiosGetStub.rejects(new Error('Metadata server error')); |
| 43 | + fetchStub.rejects(new Error('Network error')); |
41 | 44 |
|
42 | 45 | const region = await getExecutionRegion(); |
| 46 | + |
43 | 47 | expect(region).to.be.null; |
44 | 48 | }); |
45 | 49 | }); |
0 commit comments