@@ -21,13 +21,68 @@ describe("throttlingRetryPolicy", function () {
21
21
sinon . restore ( ) ;
22
22
} ) ;
23
23
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 ( ) => {
25
80
const request = createPipelineRequest ( {
26
81
url : "https://bing.com" ,
27
82
} ) ;
28
83
const retryResponse : PipelineResponse = {
29
84
headers : createHttpHeaders ( {
30
- "Retry-After" : "10 " ,
85
+ "Retry-After" : "Wed, 21 Oct 2015 07:28:00 GMT " ,
31
86
} ) ,
32
87
request,
33
88
status : 429 ,
@@ -43,14 +98,18 @@ describe("throttlingRetryPolicy", function () {
43
98
next . onFirstCall ( ) . resolves ( retryResponse ) ;
44
99
next . onSecondCall ( ) . resolves ( successResponse ) ;
45
100
46
- const clock = sinon . useFakeTimers ( ) ;
101
+ const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
47
102
48
103
const promise = policy . sendRequest ( request , next ) ;
49
104
assert . isTrue ( next . calledOnce ) ;
50
105
51
106
// allow the delay to occur
52
107
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
+ ) ;
54
113
assert . isTrue ( next . calledTwice ) ;
55
114
56
115
const result = await promise ;
@@ -59,16 +118,16 @@ describe("throttlingRetryPolicy", function () {
59
118
clock . restore ( ) ;
60
119
} ) ;
61
120
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 ( ) => {
63
122
const request = createPipelineRequest ( {
64
123
url : "https://bing.com" ,
65
124
} ) ;
66
125
const retryResponse : PipelineResponse = {
67
126
headers : createHttpHeaders ( {
68
- "Retry-After" : "Wed, 21 Oct 2015 07:28:00 GMT " ,
127
+ "Retry-After" : "10 " ,
69
128
} ) ,
70
129
request,
71
- status : 429 ,
130
+ status : 503 ,
72
131
} ;
73
132
const successResponse : PipelineResponse = {
74
133
headers : createHttpHeaders ( ) ,
@@ -81,18 +140,14 @@ describe("throttlingRetryPolicy", function () {
81
140
next . onFirstCall ( ) . resolves ( retryResponse ) ;
82
141
next . onSecondCall ( ) . resolves ( successResponse ) ;
83
142
84
- const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
143
+ const clock = sinon . useFakeTimers ( ) ;
85
144
86
145
const promise = policy . sendRequest ( request , next ) ;
87
146
assert . isTrue ( next . calledOnce ) ;
88
147
89
148
// allow the delay to occur
90
149
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 ) ;
96
151
assert . isTrue ( next . calledTwice ) ;
97
152
98
153
const result = await promise ;
@@ -101,13 +156,13 @@ describe("throttlingRetryPolicy", function () {
101
156
clock . restore ( ) ;
102
157
} ) ;
103
158
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 ( ) => {
105
160
const request = createPipelineRequest ( {
106
161
url : "https://bing.com" ,
107
162
} ) ;
108
163
const retryResponse : PipelineResponse = {
109
164
headers : createHttpHeaders ( {
110
- "Retry-After" : "10 " ,
165
+ "Retry-After" : "Wed, 21 Oct 2015 07:28:00 GMT " ,
111
166
} ) ,
112
167
request,
113
168
status : 503 ,
@@ -123,14 +178,18 @@ describe("throttlingRetryPolicy", function () {
123
178
next . onFirstCall ( ) . resolves ( retryResponse ) ;
124
179
next . onSecondCall ( ) . resolves ( successResponse ) ;
125
180
126
- const clock = sinon . useFakeTimers ( ) ;
181
+ const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
127
182
128
183
const promise = policy . sendRequest ( request , next ) ;
129
184
assert . isTrue ( next . calledOnce ) ;
130
185
131
186
// allow the delay to occur
132
187
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
+ ) ;
134
193
assert . isTrue ( next . calledTwice ) ;
135
194
136
195
const result = await promise ;
@@ -139,7 +198,7 @@ describe("throttlingRetryPolicy", function () {
139
198
clock . restore ( ) ;
140
199
} ) ;
141
200
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 ( ) => {
143
202
const request = createPipelineRequest ( {
144
203
url : "https://bing.com" ,
145
204
} ) ;
@@ -161,19 +220,12 @@ describe("throttlingRetryPolicy", function () {
161
220
next . onFirstCall ( ) . resolves ( retryResponse ) ;
162
221
next . onSecondCall ( ) . resolves ( successResponse ) ;
163
222
164
- const clock = sinon . useFakeTimers ( new Date ( "Wed, 21 Oct 2015 07:20:00 GMT" ) ) ;
165
-
166
223
const promise = policy . sendRequest ( request , next ) ;
167
- assert . isTrue ( next . calledOnce ) ;
224
+ const clock = sinon . useFakeTimers ( ) ;
168
225
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" ) ;
177
229
178
230
const result = await promise ;
179
231
0 commit comments