-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHttpCacheTests.cs
More file actions
627 lines (511 loc) · 18.6 KB
/
HttpCacheTests.cs
File metadata and controls
627 lines (511 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#if DEBUG
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable AccessToDisposedClosure
[TestFixture]
public class HttpCacheTests
{
[Test]
public async Task EnsureExpiryIsCorrect()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var result = await httpCache.DownloadAsync("https://httpbin.org/json");
var path = result.File!.Value.Content;
var time = Timestamp.FromPath(path);
Null(time.Expiry);
AreEqual(FileEx.MinFileDate, File.GetLastWriteTimeUtc(path));
}
[Test]
public static async Task Construction()
{
using var cacheDirectory = new TempDirectory();
#region Construction
var httpCache = new HttpCache(
cacheDirectory,
// omit for default new HttpClient()
new HttpClient
{
Timeout = TimeSpan.FromSeconds(30)
},
// omit for the default of 1000
maxEntries: 10000);
// Dispose when finished
await httpCache.DisposeAsync();
#endregion
}
[Test]
public void DependencyInjection()
{
using var cacheDirectory = new TempDirectory();
#region DependencyInjection
var services = new ServiceCollection();
services.AddSingleton(_ => new HttpCache(cacheDirectory));
using var provider = services.BuildServiceProvider();
var httpCache = provider.GetRequiredService<HttpCache>();
NotNull(httpCache);
#endregion
}
[Test]
public void DependencyInjectionWithHttpFactory()
{
using var cacheDirectory = new TempDirectory();
#region DependencyInjectionWithHttpFactory
var services = new ServiceCollection();
services.AddHttpClient();
services.AddSingleton(_ =>
{
var clientFactory = _.GetRequiredService<IHttpClientFactory>();
return new HttpCache(cacheDirectory, clientFactory.CreateClient);
});
using var provider = services.BuildServiceProvider();
var httpCache = provider.GetRequiredService<HttpCache>();
NotNull(httpCache);
#endregion
}
[Test]
public async Task DefaultInstance()
{
HttpCache.Default.Purge();
#region DefaultInstance
var content = await HttpCache.Default.DownloadAsync("https://httpbin.org/status/200");
#endregion
await Verify(content);
}
[Test]
public async Task EmptyContent()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var content = await httpCache.DownloadAsync("https://httpbin.org/status/200");
await Verify(content);
}
[Test]
public async Task DuplicateSlowDownloads()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var task = httpCache.DownloadAsync("https://httpbin.org/delay/1");
await Task.Delay(100);
var result2 = await httpCache.DownloadAsync("https://httpbin.org/delay/1");
var result1 = await task;
True(result1.Stored || result2.Stored);
}
#if DEBUG
//TODO: debug these on mac
[Test]
public async Task PurgeOldWhenContentFileLocked()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
using var result = await httpCache.DownloadAsync("https://httpbin.org/status/200");
using (await result.AsResponseMessageAsync())
{
var filePair = result.File!.Value;
filePair.PurgeItem();
True(filePair.Exists());
}
}
[Test]
public async Task PurgeOldWhenMetaFileLocked()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var result = await httpCache.DownloadAsync("https://httpbin.org/status/200");
using (await result.AsResponseMessageAsync())
{
var filePair = result.File!.Value;
filePair.PurgeItem();
True(filePair.Exists());
}
}
#endif
[Test]
public async Task LockedContentFile()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var uri = "https://httpbin.org/etag/{etag}";
var result = await httpCache.DownloadAsync(uri);
using (await result.AsResponseMessageAsync())
{
result = await httpCache.DownloadAsync(uri);
}
using var httpResponseMessage = await result.AsResponseMessageAsync();
await Verify(httpResponseMessage);
}
[Test]
public async Task LockedMetaFile()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var uri = "https://httpbin.org/etag/{etag}";
var result = await httpCache.DownloadAsync(uri);
using (new FileStream(result.File!.Value.Content, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
result = await httpCache.DownloadAsync(uri);
}
using var httpResponseMessage = await result.AsResponseMessageAsync();
await Verify(httpResponseMessage);
}
[Test]
public async Task FullHttpResponseMessageAsync()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
#region FullHttpResponseMessage
using var response = await httpCache.ResponseAsync("https://httpbin.org/status/200");
#endregion
await Verify(response);
}
[Test]
public async Task FullHttpResponseMessage()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
using var response = httpCache.Response("https://httpbin.org/status/200");
await Verify(response);
}
[Test]
public async Task NoCache()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
using var result = await httpCache.DownloadAsync("https://httpbin.org/response-headers?Cache-Control=no-cache");
await Verify(result);
}
[Test]
public async Task NoStore()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
using var result = await httpCache.DownloadAsync("https://httpbin.org/response-headers?Cache-Control=no-store");
NotNull(result.Response);
await Verify(result);
}
[Test]
public async Task Etag()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var uri = "https://httpbin.org/etag/{etag}";
await httpCache.DownloadAsync(uri);
var content = await httpCache.DownloadAsync(uri);
await Verify(content);
}
[Test]
public async Task ExpiredShouldNotSendEtagOrIfModifiedSince()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var uri = "https://httpbin.org/etag/{etag}";
var result = await httpCache.DownloadAsync(uri);
File.SetLastWriteTimeUtc(result.File!.Value.Content, new(2020, 10, 1));
Recording.Start();
result = await httpCache.DownloadAsync(uri);
await Verify(result)
.IgnoreMembers("traceparent", "Traceparent")
.ScrubInlineDateTimes("ddd, dd MMM yyyy HH:mm:ss 'GMT'");
}
[Test]
public async Task CacheControlMaxAge()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var uri = "https://httpbin.org/cache/20";
await httpCache.DownloadAsync(uri);
var content = await httpCache.DownloadAsync(uri);
await Verify(content);
}
[Test]
public async Task WithContent()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var content = await httpCache.DownloadAsync("https://httpbin.org/json");
await Verify(content);
}
[Test]
public Task WithContentSync()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var content = httpCache.Download("https://httpbin.org/json");
return Verify(content);
}
[Test]
public async Task String()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
#region string
var content = await httpCache.StringAsync("https://httpbin.org/json");
#endregion
await Verify(content);
}
[Test]
public async Task Lines()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
#region string
var lines = new List<string>();
await foreach (var line in httpCache.LinesAsync("https://httpbin.org/json"))
{
lines.Add(line);
}
#endregion
await Verify(lines);
}
[Test]
public async Task Bytes()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
#region bytes
var bytes = await httpCache.BytesAsync("https://httpbin.org/json");
#endregion
await Verify(Encoding.UTF8.GetString(bytes));
}
[Test]
public async Task Stream()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
#region stream
using var stream = await httpCache.StreamAsync("https://httpbin.org/json");
#endregion
await Verify(stream, "txt");
}
[Test]
public async Task ToFile()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var targetFile = FileEx.GetTempFileName("txt");
try
{
#region ToFile
await httpCache.ToFileAsync("https://httpbin.org/json", targetFile);
#endregion
await VerifyFile(targetFile);
}
finally
{
File.Delete(targetFile);
}
}
[Test]
public async Task ToStream()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var targetStream = new MemoryStream();
#region ToStream
await httpCache.ToStreamAsync("https://httpbin.org/json", targetStream);
#endregion
await Verify(targetStream, "txt");
}
[Test]
public async Task ModifyRequest()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
var uri = "https://httpbin.org/json";
#region ModifyRequest
var content = await httpCache.StringAsync(
uri,
modifyRequest: message =>
{
message.Headers.Add("Key1", "Value1");
message.Headers.Add("Key2", "Value2");
});
#endregion
await Verify(content);
}
[Test]
public async Task NotFound()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
await ThrowsTask(() => httpCache.StringAsync("https://httpbin.org/status/404"));
}
[Test]
public async Task Timeout()
{
using var cacheDirectory = new TempDirectory();
var httpClient = new HttpClient
{
Timeout = TimeSpan.FromMilliseconds(10)
};
await using var cache = new HttpCache(cacheDirectory, httpClient);
var uri = "https://httpbin.org/delay/1";
await ThrowsTask(() => cache.StringAsync(uri))
.UniqueForRuntime()
.IgnoreMembers("InnerException", "CancellationToken");
}
[Test]
public async Task TimeoutUseStale()
{
using var cacheDirectory = new TempDirectory();
var httpClient = new HttpClient
{
Timeout = TimeSpan.FromMilliseconds(1)
};
var httpCache = new HttpCache(cacheDirectory, httpClient);
var uri = "https://httpbin.org/delay/1";
#region AddItem
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("the content")
};
await httpCache.AddItemAsync(uri, response);
#endregion
await Verify(httpCache.DownloadAsync(uri, true));
await httpCache.DisposeAsync();
}
[Test]
public async Task SyncDownload_WithNullExpiry_ShouldUseCachedFile()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
// Add an item without expiry headers - this will result in null Expiry
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("test content")
};
// Use a real URL that we'll cache, then verify sync uses cache without network
var uri = "https://httpbin.org/json";
await httpCache.AddItemAsync(uri, response);
// Bug: HttpCache.cs does "if (timestamp.Expiry > now)" but should be
// "if (expiry == null || expiry > now)" like the async version.
// When Expiry is null, sync version incorrectly tries to re-fetch from network
// instead of returning the cached file.
// ReSharper disable once MethodHasAsyncOverload
var result = httpCache.Download(uri);
// If the bug exists, this will have fetched new content from httpbin.org/json
// If fixed, it should return our cached "test content"
// ReSharper disable once UseAwaitUsing
using var stream = result.AsStream();
using var reader = new StreamReader(stream);
// ReSharper disable once MethodHasAsyncOverload
var content = reader.ReadToEnd();
AreEqual("test content", content);
}
[Test]
public async Task ServerError()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
await ThrowsTask(() => httpCache.StringAsync("https://httpbin.org/status/500"));
}
[Test]
public async Task ServerErrorDontUseStale()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("foo")
{
Headers =
{
Expires = DateTimeOffset.Now.AddDays(-1)
}
},
};
var uri = "https://httpbin.org/status/500";
await httpCache.AddItemAsync(uri, response);
await ThrowsTask(() => httpCache.StringAsync(uri));
}
[Test]
public async Task ServerErrorUseStale()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("content")
};
var uri = "https://httpbin.org/status/500";
await httpCache.AddItemAsync(uri, response);
#region staleIfError
var content = httpCache.StringAsync(uri, staleIfError: true);
#endregion
await Verify(content);
}
[Test]
public async Task Cache404()
{
using var cacheDirectory = new TempDirectory();
var uri = "https://httpbin.org/status/404";
#region cache404
await using var cache = new HttpCache(cacheDirectory, cache404: true);
var content = await cache.StringAsync(uri);
#endregion
await Verify(content);
}
[Test]
public async Task MinFreshness()
{
using var cacheDirectory = new TempDirectory();
#region MinFreshness
await using var cache = new HttpCache(
cacheDirectory,
minFreshness: TimeSpan.FromHours(1));
var content = await cache.StringAsync("https://httpbin.org/json");
#endregion
await Verify(content);
}
[Test]
public async Task Purge_ShouldOnlyProcessBinFiles()
{
using var cacheDirectory = new TempDirectory();
var httpCache = new HttpCache(cacheDirectory);
// Add a cached item (creates both .bin and .json files)
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("test content")
};
var uri = "https://httpbin.org/purge-test";
await httpCache.AddItemAsync(uri, response);
// Verify both files exist before purge
var files = Directory.GetFiles(cacheDirectory);
var binFiles = files.Where(_ => _.EndsWith(".bin")).ToList();
var jsonFiles = files.Where(_ => _.EndsWith(".json")).ToList();
AreEqual(1, binFiles.Count);
AreEqual(1, jsonFiles.Count);
// Bug: Purge() iterates over ALL files including .json files.
// When FilePair.FromContentFile() is called on a .json file,
// it creates a pair where Content=Meta (both .json), causing
// PurgeItem() to fail when trying to move the same file twice.
httpCache.Purge();
// After purge, all cache files should be deleted
var remainingFiles = Directory.GetFiles(cacheDirectory);
AreEqual(0, remainingFiles.Length);
}
[Test]
public void Default_ConcurrentAccess_IsThreadSafe()
{
// Lazy<T> ensures thread-safe initialization without leaking instances.
// All threads get the same instance and only one HttpCache + HttpClient is created.
const int threadCount = 10;
var barrier = new Barrier(threadCount);
var instances = new HttpCache[threadCount];
var threads = Enumerable.Range(0, threadCount)
.Select(_ =>
new Thread(() =>
{
// All threads start at the same time
barrier.SignalAndWait();
instances[_] = HttpCache.Default;
}))
.ToList();
threads.ForEach(_ => _.Start());
threads.ForEach(_ => _.Join());
// All threads should get the same instance
var firstInstance = instances[0];
True(instances.All(_ => ReferenceEquals(_, firstInstance)),
"All threads should receive the same Default instance");
}
}
#endif