@@ -21,13 +21,68 @@ describe("throttlingRetryPolicy", function () {
2121 sinon . restore ( ) ;
2222 } ) ;
2323
24- it ( "It should retry after a given number of seconds on a response with status code 429" , async ( ) => {
24+ const defaultDurations = [ 0 , 10 * 1000 ] ; // milliseconds
25+
26+ defaultDurations . forEach ( ( defaultDuration ) => {
27+ const headersWithDefaultDuration = [
28+ {
29+ "Retry-After" : String ( defaultDuration / 1000 ) ,
30+ } ,
31+ {
32+ "retry-after-ms" : String ( defaultDuration ) ,
33+ } ,
34+ {
35+ "x-ms-retry-after-ms" : String ( defaultDuration ) ,
36+ } ,
37+ ] as const ;
38+ headersWithDefaultDuration . forEach ( ( headers ) => {
39+ it ( `(${
40+ Object . keys ( headers ) [ 0 ]
41+ } ) - should retry after a given number of seconds/milliseconds on a response with status code 429`, async ( ) => {
42+ const request = createPipelineRequest ( {
43+ url : "https://bing.com" ,
44+ } ) ;
45+ const retryResponse : PipelineResponse = {
46+ headers : createHttpHeaders ( headers ) ,
47+ request,
48+ status : 429 ,
49+ } ;
50+ const successResponse : PipelineResponse = {
51+ headers : createHttpHeaders ( ) ,
52+ request,
53+ status : 200 ,
54+ } ;
55+
56+ const policy = throttlingRetryPolicy ( ) ;
57+ const next = sinon . stub < Parameters < SendRequest > , ReturnType < SendRequest > > ( ) ;
58+ next . onFirstCall ( ) . resolves ( retryResponse ) ;
59+ next . onSecondCall ( ) . resolves ( successResponse ) ;
60+
61+ const clock = sinon . useFakeTimers ( ) ;
62+
63+ const promise = policy . sendRequest ( request , next ) ;
64+ assert . isTrue ( next . calledOnce , "next wasn't called once" ) ;
65+
66+ // allow the delay to occur
67+ const time = await clock . nextAsync ( ) ;
68+ assert . strictEqual ( time , defaultDuration ) ;
69+ assert . isTrue ( next . calledTwice , "next wasn't called twice" ) ;
70+
71+ const result = await promise ;
72+
73+ assert . strictEqual ( result , successResponse ) ;
74+ clock . restore ( ) ;
75+ } ) ;
76+ } ) ;
77+ } ) ;
78+
79+ it ( "It should retry after a given date occurs on a response with status code 429" , async ( ) => {
2580 const request = createPipelineRequest ( {
2681 url : "https://bing.com" ,
2782 } ) ;
2883 const retryResponse : PipelineResponse = {
2984 headers : createHttpHeaders ( {
30- "Retry-After" : "10 " ,
85+ "Retry-After" : "Wed, 21 Oct 2015 07:28:00 GMT " ,
3186 } ) ,
3287 request,
3388 status : 429 ,
@@ -43,14 +98,18 @@ describe("throttlingRetryPolicy", function () {
4398 next . onFirstCall ( ) . resolves ( retryResponse ) ;
4499 next . onSecondCall ( ) . resolves ( successResponse ) ;
45100
46- const clock = sinon . useFakeTimers ( ) ;
101+ const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
47102
48103 const promise = policy . sendRequest ( request , next ) ;
49104 assert . isTrue ( next . calledOnce ) ;
50105
51106 // allow the delay to occur
52107 const time = await clock . nextAsync ( ) ;
53- assert . strictEqual ( time , 10 * 1000 ) ;
108+ assert . strictEqual (
109+ time ,
110+ new Date ( "Wed, 21 Oct 2015 07:28:00 GMT" ) . getTime ( ) ,
111+ "It should now be the time from the header."
112+ ) ;
54113 assert . isTrue ( next . calledTwice ) ;
55114
56115 const result = await promise ;
@@ -59,16 +118,16 @@ describe("throttlingRetryPolicy", function () {
59118 clock . restore ( ) ;
60119 } ) ;
61120
62- it ( "It should retry after a given date occurs on a response with status code 429 " , async ( ) => {
121+ it ( "It should retry after a given number of seconds on a response with status code 503 " , async ( ) => {
63122 const request = createPipelineRequest ( {
64123 url : "https://bing.com" ,
65124 } ) ;
66125 const retryResponse : PipelineResponse = {
67126 headers : createHttpHeaders ( {
68- "Retry-After" : "Wed, 21 Oct 2015 07:28:00 GMT " ,
127+ "Retry-After" : "10 " ,
69128 } ) ,
70129 request,
71- status : 429 ,
130+ status : 503 ,
72131 } ;
73132 const successResponse : PipelineResponse = {
74133 headers : createHttpHeaders ( ) ,
@@ -81,18 +140,14 @@ describe("throttlingRetryPolicy", function () {
81140 next . onFirstCall ( ) . resolves ( retryResponse ) ;
82141 next . onSecondCall ( ) . resolves ( successResponse ) ;
83142
84- const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
143+ const clock = sinon . useFakeTimers ( ) ;
85144
86145 const promise = policy . sendRequest ( request , next ) ;
87146 assert . isTrue ( next . calledOnce ) ;
88147
89148 // allow the delay to occur
90149 const time = await clock . nextAsync ( ) ;
91- assert . strictEqual (
92- time ,
93- new Date ( "Wed, 21 Oct 2015 07:28:00 GMT" ) . getTime ( ) ,
94- "It should now be the time from the header."
95- ) ;
150+ assert . strictEqual ( time , 10 * 1000 ) ;
96151 assert . isTrue ( next . calledTwice ) ;
97152
98153 const result = await promise ;
@@ -101,13 +156,13 @@ describe("throttlingRetryPolicy", function () {
101156 clock . restore ( ) ;
102157 } ) ;
103158
104- it ( "It should retry after a given number of seconds on a response with status code 503" , async ( ) => {
159+ it ( "It should retry after a given date occurs on a response with status code 503" , async ( ) => {
105160 const request = createPipelineRequest ( {
106161 url : "https://bing.com" ,
107162 } ) ;
108163 const retryResponse : PipelineResponse = {
109164 headers : createHttpHeaders ( {
110- "Retry-After" : "10 " ,
165+ "Retry-After" : "Wed, 21 Oct 2015 07:28:00 GMT " ,
111166 } ) ,
112167 request,
113168 status : 503 ,
@@ -123,14 +178,18 @@ describe("throttlingRetryPolicy", function () {
123178 next . onFirstCall ( ) . resolves ( retryResponse ) ;
124179 next . onSecondCall ( ) . resolves ( successResponse ) ;
125180
126- const clock = sinon . useFakeTimers ( ) ;
181+ const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
127182
128183 const promise = policy . sendRequest ( request , next ) ;
129184 assert . isTrue ( next . calledOnce ) ;
130185
131186 // allow the delay to occur
132187 const time = await clock . nextAsync ( ) ;
133- assert . strictEqual ( time , 10 * 1000 ) ;
188+ assert . strictEqual (
189+ time ,
190+ new Date ( "Wed, 21 Oct 2015 07:28:00 GMT" ) . getTime ( ) ,
191+ "It should now be the time from the header."
192+ ) ;
134193 assert . isTrue ( next . calledTwice ) ;
135194
136195 const result = await promise ;
@@ -139,7 +198,7 @@ describe("throttlingRetryPolicy", function () {
139198 clock . restore ( ) ;
140199 } ) ;
141200
142- it ( "It should retry after a given date occurs on a response with status code 503" , async ( ) => {
201+ it ( "It should retry after 0 seconds with status code 503 for a past date " , async ( ) => {
143202 const request = createPipelineRequest ( {
144203 url : "https://bing.com" ,
145204 } ) ;
@@ -161,19 +220,12 @@ describe("throttlingRetryPolicy", function () {
161220 next . onFirstCall ( ) . resolves ( retryResponse ) ;
162221 next . onSecondCall ( ) . resolves ( successResponse ) ;
163222
164- const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
165-
166223 const promise = policy . sendRequest ( request , next ) ;
167- assert . isTrue ( next . calledOnce ) ;
224+ const clock = sinon . useFakeTimers ( ) ;
168225
169- // allow the delay to occur
170- const time = await clock . nextAsync ( ) ;
171- assert . strictEqual (
172- time ,
173- new Date ( "Wed, 21 Oct 2015 07:28:00 GMT" ) . getTime ( ) ,
174- "It should now be the time from the header."
175- ) ;
176- assert . isTrue ( next . calledTwice ) ;
226+ assert . isTrue ( next . calledOnce , "next wasn't called once" ) ;
227+ await clock . nextAsync ( ) ;
228+ assert . isTrue ( next . calledTwice , "next wasn't called twice" ) ;
177229
178230 const result = await promise ;
179231
0 commit comments