55using Azure . ApiManagement . PolicyToolkit . Authoring . Expressions ;
66using Azure . ApiManagement . PolicyToolkit . Testing ;
77using Azure . ApiManagement . PolicyToolkit . Testing . Document ;
8+ using Azure . ApiManagement . PolicyToolkit . Testing . Emulator . Data ;
89
910namespace Test . Emulator . Emulator . Policies ;
1011
1112[ TestClass ]
1213public class CacheStoreTests
1314{
14- class SimpleCacheStore : IDocument
15- {
16- public void Outbound ( IOutboundContext context )
17- {
18- context . CacheStore ( 10 ) ;
19- }
20- }
21-
22- class SimpleCacheStoreStoreResponse : IDocument
23- {
24- public void Outbound ( IOutboundContext context )
25- {
26- context . CacheStore ( 10 , true ) ;
27- }
28- }
29-
3015 [ TestMethod ]
3116 public void CacheStore_Callback ( )
3217 {
@@ -42,6 +27,18 @@ public void CacheStore_Callback()
4227 executedCallback . Should ( ) . BeTrue ( ) ;
4328 }
4429
30+ [ TestMethod ]
31+ public void CacheStore_NotStoreWhenLookupWasNotExecuted ( )
32+ {
33+ TestDocument test = new SimpleCacheStore ( ) . AsTestDocument ( ) ;
34+ CacheStore cache = test . SetupCacheStore ( ) ;
35+ test . SetupOutbound ( ) . CacheStore ( ) . WithCacheKey ( "key" ) ;
36+
37+ test . RunOutbound ( ) ;
38+
39+ cache . InternalCache . Should ( ) . NotContainKey ( "key" ) ;
40+ }
41+
4542 [ TestMethod ]
4643 public void CacheStore_StoreResponseInCache ( )
4744 {
@@ -62,6 +59,23 @@ public void CacheStore_StoreResponseInCache()
6259 response . Headers . Should ( ) . Equal ( contextResponse . Headers ) ;
6360 }
6461
62+ [ TestMethod ]
63+ public void CacheStore_StoreResponseInCache_WhenExternal ( )
64+ {
65+ TestDocument test = new SimpleCacheStore ( ) . AsTestDocument ( ) ;
66+ CacheStore cache = test . SetupCacheStore ( ) ;
67+ test . SetupCacheStore ( ) . WithExternalCacheSetup ( ) ;
68+ test . SetupCacheInfo ( ) . WithExecutedCacheLookup ( ) ;
69+ test . SetupOutbound ( ) . CacheStore ( ) . WithCacheKey ( "key" ) ;
70+
71+ test . RunOutbound ( ) ;
72+
73+ CacheValue ? cacheValue = cache . ExternalCache . Should ( ) . ContainKey ( "key" ) . WhoseValue ;
74+ cacheValue . Duration . Should ( ) . Be ( 10 ) ;
75+ cacheValue . Value . Should ( ) . BeAssignableTo < IResponse > ( )
76+ . And . NotBeSameAs ( test . Context . Response , "Should be a copy of response" ) ;
77+ }
78+
6579 [ TestMethod ]
6680 public void CacheStore_NotStoreIfResponseIsNot200 ( )
6781 {
@@ -98,4 +112,33 @@ public void CacheStore_StoreIfResponseIsNot200_WhenCacheResponseIsSetToTrue()
98112 response . StatusReason . Should ( ) . Be ( contextResponse . StatusReason ) ;
99113 response . Headers . Should ( ) . Equal ( contextResponse . Headers ) ;
100114 }
115+
116+ [ TestMethod ]
117+ public void CacheStore_ ( )
118+ {
119+ TestDocument test = new SimpleCacheStore ( ) . AsTestDocument ( ) ;
120+ test . SetupCacheInfo ( ) . WithExecutedCacheLookup ( new CacheLookupConfig
121+ {
122+ VaryByDeveloper = true ,
123+ VaryByDeveloperGroups = true ,
124+ CachingType = "internal" ,
125+ AllowPrivateResponseCaching =
126+ } ) ;
127+ }
128+
129+ private class SimpleCacheStore : IDocument
130+ {
131+ public void Outbound ( IOutboundContext context )
132+ {
133+ context . CacheStore ( 10 ) ;
134+ }
135+ }
136+
137+ private class SimpleCacheStoreStoreResponse : IDocument
138+ {
139+ public void Outbound ( IOutboundContext context )
140+ {
141+ context . CacheStore ( 10 , true ) ;
142+ }
143+ }
101144}
0 commit comments