11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- const chai = require ( "chai" ) ;
5- const chaiAsPromised = require ( "chai-as-promised" ) ;
4+ import * as chai from "chai" ;
5+ import * as chaiAsPromised from "chai-as-promised" ;
66chai . use ( chaiAsPromised ) ;
77const expect = chai . expect ;
8- const { load } = require ( "../dist/index" ) ;
9- const { createMockedConnectionString } = require ( "./utils/testHelper" ) ;
10- const nock = require ( "nock" ) ;
8+ import { load } from "./exportedApi" ;
9+ import { createMockedConnectionString } from "./utils/testHelper" ;
10+ import * as nock from "nock" ;
1111
1212class HttpRequestCountPolicy {
13+ count : number ;
14+ name : string ;
15+
1316 constructor ( ) {
1417 this . count = 0 ;
1518 this . name = "HttpRequestCountPolicy" ;
@@ -25,12 +28,12 @@ class HttpRequestCountPolicy {
2528
2629describe ( "custom client options" , function ( ) {
2730 const fakeEndpoint = "https://azure.azconfig.io" ;
28- before ( ( ) => {
31+ beforeEach ( ( ) => {
2932 // Thus here mock it to reply 500, in which case the retry mechanism works.
3033 nock ( fakeEndpoint ) . persist ( ) . get ( ( ) => true ) . reply ( 500 ) ;
3134 } ) ;
3235
33- after ( ( ) => {
36+ afterEach ( ( ) => {
3437 nock . restore ( ) ;
3538 } )
3639
@@ -58,7 +61,7 @@ describe("custom client options", function () {
5861
5962 it ( "should override default retry options" , async ( ) => {
6063 const countPolicy = new HttpRequestCountPolicy ( ) ;
61- const loadWithMaxRetries = ( maxRetries ) => {
64+ const loadWithMaxRetries = ( maxRetries : number ) => {
6265 return load ( createMockedConnectionString ( fakeEndpoint ) , {
6366 clientOptions : {
6467 additionalPolicies : [ {
@@ -93,9 +96,26 @@ describe("custom client options", function () {
9396 expect ( countPolicy . count ) . eq ( 2 ) ;
9497 } ) ;
9598
96- // Note:
97- // core-rest-pipeline skips the retry throwing `RestError: getaddrinfo ENOTFOUND azure.azconfig.io`
98- // Probably would be fixed in upstream libs.
99- // See https://github.com/Azure/azure-sdk-for-js/issues/27037
100- it ( "should retry on DNS failure" ) ;
99+ it ( "should retry on DNS failure" , async ( ) => {
100+ nock . restore ( ) ; // stop mocking with 500 error but sending real requests which will fail with ENOTFOUND
101+ const countPolicy = new HttpRequestCountPolicy ( ) ;
102+ const loadPromise = ( ) => {
103+ return load ( createMockedConnectionString ( fakeEndpoint ) , {
104+ clientOptions : {
105+ additionalPolicies : [ {
106+ policy : countPolicy ,
107+ position : "perRetry"
108+ } ]
109+ }
110+ } )
111+ } ;
112+ let error ;
113+ try {
114+ await loadPromise ( ) ;
115+ } catch ( e ) {
116+ error = e ;
117+ }
118+ expect ( error ) . not . undefined ;
119+ expect ( countPolicy . count ) . eq ( 3 ) ;
120+ } ) ;
101121} )
0 commit comments