1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . IO ;
4
3
using System . IO . Compression ;
4
+ using System . Net ;
5
5
using System . Text ;
6
6
using System . Threading . Tasks ;
7
+ using FluentAssertions ;
8
+ using FluentAssertions . Extensions ;
7
9
using Gw2Sharp . Tests . Helpers ;
8
10
using Gw2Sharp . WebApi . Caching ;
11
+ using Gw2Sharp . WebApi . Http ;
9
12
using Xunit ;
10
13
11
14
#pragma warning disable S3881 // "IDisposable" should be implemented correctly
@@ -27,10 +30,10 @@ public async Task StoresRawCacheIntoArchiveTest()
27
30
{
28
31
string category = "testdata" ;
29
32
string id = "bytearray.dat" ;
30
- var expiryTime = DateTimeOffset . UtcNow . AddMinutes ( 1 ) ;
33
+ var expiresAt = 1 . Minutes ( ) . After ( DateTime . Now ) ;
31
34
32
35
byte [ ] data = new byte [ ] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
33
- await this . cacheMethod . SetAsync ( category , id , data , expiryTime ) ;
36
+ await this . cacheMethod . SetAsync ( category , id , data , expiresAt ) ;
34
37
var actualCache = await this . cacheMethod . TryGetAsync < byte [ ] > ( category , id ) ;
35
38
36
39
Assert . Equal ( data , actualCache ? . Item ) ;
@@ -47,17 +50,18 @@ public async Task StoresRawCacheIntoArchiveTest()
47
50
}
48
51
49
52
[ Fact ]
50
- public async Task StoresArrayCacheIntoArchiveTest ( )
53
+ public async Task StoresStringCacheIntoArchiveTest ( )
51
54
{
52
55
string category = "testdata" ;
53
- string id = "stringarray .dat" ;
54
- var expiryTime = DateTimeOffset . UtcNow . AddMinutes ( 1 ) ;
56
+ string id = "string .dat" ;
57
+ var expiresAt = 1 . Minutes ( ) . After ( DateTime . Now ) ;
55
58
56
- string [ ] data = new [ ] { "Hello" , "World!" } ;
57
- await this . cacheMethod . SetAsync ( category , id , data , expiryTime ) ;
58
- var actualCache = await this . cacheMethod . TryGetAsync < IEnumerable < string > > ( category , id ) ;
59
+ var cache = new WebApiResponse ( "Hello world " , HttpStatusCode . OK , null ) ;
60
+ await this . cacheMethod . SetAsync ( category , id , cache , expiresAt ) ;
61
+ var actualCache = await this . cacheMethod . TryGetAsync < IWebApiResponse > ( category , id ) ;
59
62
60
- Assert . Equal ( data , actualCache ? . Item ) ;
63
+ actualCache . Should ( ) . NotBeNull ( ) ;
64
+ actualCache . Item . Should ( ) . BeEquivalentTo ( cache ) ;
61
65
this . cacheMethod . Dispose ( ) ;
62
66
63
67
using var stream = File . OpenRead ( ARCHIVE_FILENAME ) ;
@@ -67,7 +71,7 @@ public async Task StoresArrayCacheIntoArchiveTest()
67
71
using var memoryStream = new MemoryStream ( ) ;
68
72
await entryStream . CopyToAsync ( memoryStream ) ;
69
73
70
- string expected = $ "[ \" { data [ 0 ] } \" ,\" { data [ 1 ] } \" ] ";
74
+ string expected = "{ \" content \" : \" Hello world \" ,\" responseHeaders \" :{}, \" statusCode \" :200} ";
71
75
string actual = Encoding . UTF8 . GetString ( memoryStream . ToArray ( ) ) ;
72
76
Assert . Equal ( expected , actual ) ;
73
77
}
@@ -77,10 +81,10 @@ public async Task LoadExistingArchiveTest()
77
81
{
78
82
string category = "testdata" ;
79
83
string id = "bytearray.dat" ;
80
- var expiryTime = DateTimeOffset . UtcNow . AddMinutes ( 1 ) ;
84
+ var expiresAt = 1 . Minutes ( ) . After ( DateTime . Now ) ;
81
85
82
86
byte [ ] data = new byte [ ] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
83
- await this . cacheMethod . SetAsync ( category , id , data , expiryTime ) ;
87
+ await this . cacheMethod . SetAsync ( category , id , data , expiresAt ) ;
84
88
var actualCache = await this . cacheMethod . TryGetAsync < byte [ ] > ( category , id ) ;
85
89
86
90
Assert . Equal ( data , actualCache ? . Item ) ;
@@ -89,10 +93,35 @@ public async Task LoadExistingArchiveTest()
89
93
this . cacheMethod = new ArchiveCacheMethod ( ARCHIVE_FILENAME ) ;
90
94
actualCache = await this . cacheMethod . TryGetAsync < byte [ ] > ( category , id ) ;
91
95
Assert . Equal ( data , actualCache ? . Item ) ;
92
- Assert . Equal ( expiryTime , actualCache ? . ExpiryTime ) ;
96
+ Assert . Equal ( expiresAt , actualCache ? . ExpiryTime ) ;
93
97
}
94
98
99
+ [ Fact ]
100
+ public async Task GetsUnsupportedTypeFromArchiveTest ( )
101
+ {
102
+ const string CATEGORY = "category" ;
103
+ const string ID = "id" ;
104
+ var expiresAt = 1 . Minutes ( ) . After ( DateTime . Now ) ;
105
+
106
+ // We need to store something valid first
107
+ byte [ ] data = new byte [ ] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ;
108
+ await this . cacheMethod . SetAsync ( CATEGORY , ID , data , expiresAt ) ;
95
109
110
+ await this . cacheMethod . Invoking ( x => x . TryGetAsync < object > ( CATEGORY , ID ) )
111
+ . Should ( ) . ThrowAsync < NotSupportedException > ( ) ;
112
+ }
113
+
114
+ [ Fact ]
115
+ public async Task StoresUnsupportedTypeIntoArchiveTest ( )
116
+ {
117
+ const string CATEGORY = "category" ;
118
+ const string ID = "id" ;
119
+ var expiresAt = 1 . Minutes ( ) . After ( DateTime . Now ) ;
120
+
121
+ object data = new object ( ) ;
122
+ await this . cacheMethod . Invoking ( x => x . SetAsync ( CATEGORY , ID , data , expiresAt ) )
123
+ . Should ( ) . ThrowAsync < NotSupportedException > ( ) ;
124
+ }
96
125
97
126
public void Dispose ( )
98
127
{
0 commit comments