@@ -48,25 +48,25 @@ public async Task RetryAfterHandlerProcesses503Response()
48
48
49
49
// Create the RetryHttpHandler with retry enabled and a 5-second timeout
50
50
var retryHandler = new RetryHttpHandler ( mockHandler , true , 5 ) ;
51
-
51
+
52
52
// Create an HttpClient with our handler
53
53
var httpClient = new HttpClient ( retryHandler ) ;
54
-
54
+
55
55
// Set the mock handler to return a success response after the first retry
56
56
mockHandler . SetResponseAfterRetryCount ( 1 , new HttpResponseMessage ( HttpStatusCode . OK )
57
57
{
58
58
Content = new StringContent ( "Success" )
59
59
} ) ;
60
-
60
+
61
61
// Send a request
62
62
var response = await httpClient . GetAsync ( "http://test.com" ) ;
63
-
63
+
64
64
// Verify the response is OK
65
65
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
66
66
Assert . Equal ( "Success" , await response . Content . ReadAsStringAsync ( ) ) ;
67
67
Assert . Equal ( 2 , mockHandler . RequestCount ) ; // Initial request + 1 retry
68
68
}
69
-
69
+
70
70
/// <summary>
71
71
/// Tests that the RetryHttpHandler throws an exception when the retry timeout is exceeded.
72
72
/// </summary>
@@ -83,22 +83,22 @@ public async Task RetryAfterHandlerThrowsWhenTimeoutExceeded()
83
83
84
84
// Create the RetryHttpHandler with retry enabled and a 1-second timeout
85
85
var retryHandler = new RetryHttpHandler ( mockHandler , true , 1 ) ;
86
-
86
+
87
87
// Create an HttpClient with our handler
88
88
var httpClient = new HttpClient ( retryHandler ) ;
89
-
89
+
90
90
// Send a request and expect an AdbcException
91
- var exception = await Assert . ThrowsAsync < AdbcException > ( async ( ) =>
91
+ var exception = await Assert . ThrowsAsync < AdbcException > ( async ( ) =>
92
92
await httpClient . GetAsync ( "http://test.com" ) ) ;
93
-
93
+
94
94
// Verify the exception has the correct SQL state in the message
95
95
Assert . Contains ( "[SQLState: 08001]" , exception . Message ) ;
96
96
Assert . Equal ( AdbcStatusCode . IOError , exception . Status ) ;
97
-
97
+
98
98
// Verify we only tried once (since the Retry-After value of 2 exceeds our timeout of 1)
99
99
Assert . Equal ( 1 , mockHandler . RequestCount ) ;
100
100
}
101
-
101
+
102
102
/// <summary>
103
103
/// Tests that the RetryHttpHandler doesn't retry when retry is disabled.
104
104
/// </summary>
@@ -115,19 +115,19 @@ public async Task RetryAfterHandlerDoesNotRetryWhenDisabled()
115
115
116
116
// Create the RetryHttpHandler with retry disabled
117
117
var retryHandler = new RetryHttpHandler ( mockHandler , false , 5 ) ;
118
-
118
+
119
119
// Create an HttpClient with our handler
120
120
var httpClient = new HttpClient ( retryHandler ) ;
121
-
121
+
122
122
// Send a request
123
123
var response = await httpClient . GetAsync ( "http://test.com" ) ;
124
-
124
+
125
125
// Verify the response is 503
126
126
Assert . Equal ( HttpStatusCode . ServiceUnavailable , response . StatusCode ) ;
127
127
Assert . Equal ( "Service Unavailable" , await response . Content . ReadAsStringAsync ( ) ) ;
128
128
Assert . Equal ( 1 , mockHandler . RequestCount ) ; // Only the initial request, no retries
129
129
}
130
-
130
+
131
131
/// <summary>
132
132
/// Tests that the RetryHttpHandler handles non-503 responses correctly.
133
133
/// </summary>
@@ -143,19 +143,19 @@ public async Task RetryAfterHandlerHandlesNon503Response()
143
143
144
144
// Create the RetryHttpHandler with retry enabled
145
145
var retryHandler = new RetryHttpHandler ( mockHandler , true , 5 ) ;
146
-
146
+
147
147
// Create an HttpClient with our handler
148
148
var httpClient = new HttpClient ( retryHandler ) ;
149
-
149
+
150
150
// Send a request
151
151
var response = await httpClient . GetAsync ( "http://test.com" ) ;
152
-
152
+
153
153
// Verify the response is 404
154
154
Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
155
155
Assert . Equal ( "Not Found" , await response . Content . ReadAsStringAsync ( ) ) ;
156
156
Assert . Equal ( 1 , mockHandler . RequestCount ) ; // Only the initial request, no retries
157
157
}
158
-
158
+
159
159
/// <summary>
160
160
/// Tests that the RetryHttpHandler handles 503 responses without Retry-After headers correctly.
161
161
/// </summary>
@@ -171,19 +171,19 @@ public async Task RetryAfterHandlerHandles503WithoutRetryAfterHeader()
171
171
172
172
// Create the RetryHttpHandler with retry enabled
173
173
var retryHandler = new RetryHttpHandler ( mockHandler , true , 5 ) ;
174
-
174
+
175
175
// Create an HttpClient with our handler
176
176
var httpClient = new HttpClient ( retryHandler ) ;
177
-
177
+
178
178
// Send a request
179
179
var response = await httpClient . GetAsync ( "http://test.com" ) ;
180
-
180
+
181
181
// Verify the response is 503
182
182
Assert . Equal ( HttpStatusCode . ServiceUnavailable , response . StatusCode ) ;
183
183
Assert . Equal ( "Service Unavailable" , await response . Content . ReadAsStringAsync ( ) ) ;
184
184
Assert . Equal ( 1 , mockHandler . RequestCount ) ; // Only the initial request, no retries
185
185
}
186
-
186
+
187
187
/// <summary>
188
188
/// Tests that the RetryHttpHandler handles invalid Retry-After headers correctly.
189
189
/// </summary>
@@ -207,19 +207,19 @@ public async Task RetryAfterHandlerHandlesInvalidRetryAfterHeader()
207
207
208
208
// Create the RetryHttpHandler with retry enabled
209
209
var retryHandler = new RetryHttpHandler ( mockHandler , true , 5 ) ;
210
-
210
+
211
211
// Create an HttpClient with our handler
212
212
var httpClient = new HttpClient ( retryHandler ) ;
213
-
213
+
214
214
// Send a request
215
215
response = await httpClient . GetAsync ( "http://test.com" ) ;
216
-
216
+
217
217
// Verify the response is 503
218
218
Assert . Equal ( HttpStatusCode . ServiceUnavailable , response . StatusCode ) ;
219
219
Assert . Equal ( "Service Unavailable" , await response . Content . ReadAsStringAsync ( ) ) ;
220
220
Assert . Equal ( 1 , mockHandler . RequestCount ) ; // Only the initial request, no retries
221
221
}
222
-
222
+
223
223
/// <summary>
224
224
/// Mock HttpMessageHandler for testing the RetryHttpHandler.
225
225
/// </summary>
@@ -228,38 +228,38 @@ private class MockHttpMessageHandler : HttpMessageHandler
228
228
private readonly HttpResponseMessage _defaultResponse ;
229
229
private HttpResponseMessage ? _responseAfterRetryCount ;
230
230
private int _retryCountForResponse ;
231
-
231
+
232
232
public int RequestCount { get ; private set ; }
233
-
233
+
234
234
public MockHttpMessageHandler ( HttpResponseMessage defaultResponse )
235
235
{
236
236
_defaultResponse = defaultResponse ;
237
237
}
238
-
238
+
239
239
public void SetResponseAfterRetryCount ( int retryCount , HttpResponseMessage response )
240
240
{
241
241
_retryCountForResponse = retryCount ;
242
242
_responseAfterRetryCount = response ;
243
243
}
244
-
244
+
245
245
protected override Task < HttpResponseMessage > SendAsync (
246
246
HttpRequestMessage request ,
247
247
CancellationToken cancellationToken )
248
248
{
249
249
RequestCount ++ ;
250
-
250
+
251
251
if ( _responseAfterRetryCount != null && RequestCount > _retryCountForResponse )
252
252
{
253
253
return Task . FromResult ( _responseAfterRetryCount ) ;
254
254
}
255
-
255
+
256
256
// Create a new response instance to avoid modifying the original
257
257
var response = new HttpResponseMessage
258
258
{
259
259
StatusCode = _defaultResponse . StatusCode ,
260
260
Content = _defaultResponse . Content
261
261
} ;
262
-
262
+
263
263
// Copy headers only if they exist
264
264
if ( _defaultResponse . Headers . Contains ( "Retry-After" ) )
265
265
{
@@ -268,9 +268,9 @@ protected override Task<HttpResponseMessage> SendAsync(
268
268
response . Headers . Add ( "Retry-After" , value ) ;
269
269
}
270
270
}
271
-
271
+
272
272
return Task . FromResult ( response ) ;
273
273
}
274
274
}
275
275
}
276
- }
276
+ }
0 commit comments