1+ // Copyright (c) Microsoft Corporation.
2+ // Licensed under the MIT License.
3+
4+ using Azure . ApiManagement . PolicyToolkit . Authoring ;
5+ using Azure . ApiManagement . PolicyToolkit . Authoring . Expressions ;
6+ using Azure . ApiManagement . PolicyToolkit . Testing ;
7+ using Azure . ApiManagement . PolicyToolkit . Testing . Document ;
8+
9+ namespace Test . Emulator . Emulator . Policies ;
10+
11+ [ TestClass ]
12+ public class CacheStoreTests
13+ {
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+
30+ [ TestMethod ]
31+ public void CacheStore_Callback ( )
32+ {
33+ var test = new SimpleCacheStore ( ) . AsTestDocument ( ) ;
34+ var executedCallback = false ;
35+ test . SetupOutbound ( ) . CacheStore ( ) . WithCallback ( ( _ , _ , _ ) =>
36+ {
37+ executedCallback = true ;
38+ } ) ;
39+
40+ test . RunOutbound ( ) ;
41+
42+ executedCallback . Should ( ) . BeTrue ( ) ;
43+ }
44+
45+ [ TestMethod ]
46+ public void CacheStore_StoreResponseInCache ( )
47+ {
48+ var test = new SimpleCacheStore ( ) . AsTestDocument ( ) ;
49+ var cache = test . SetupCacheStore ( ) ;
50+ test . SetupCacheInfo ( ) . WithExecutedCacheLookup ( ) ;
51+ test . SetupOutbound ( ) . CacheStore ( ) . WithCacheKey ( "key" ) ;
52+
53+ test . RunOutbound ( ) ;
54+
55+ var cacheValue = cache . InternalCache . Should ( ) . ContainKey ( "key" ) . WhoseValue ;
56+ cacheValue . Duration . Should ( ) . Be ( 10 ) ;
57+ var response = cacheValue . Value . Should ( ) . BeAssignableTo < IResponse > ( ) . Which ;
58+ var contextResponse = test . Context . Response ;
59+ response . Should ( ) . NotBeSameAs ( contextResponse , "Should be a copy of response" ) ;
60+ response . StatusCode . Should ( ) . Be ( contextResponse . StatusCode ) ;
61+ response . StatusReason . Should ( ) . Be ( contextResponse . StatusReason ) ;
62+ response . Headers . Should ( ) . Equal ( contextResponse . Headers ) ;
63+ }
64+
65+ [ TestMethod ]
66+ public void CacheStore_NotStoreIfResponseIsNot200 ( )
67+ {
68+ var test = new SimpleCacheStore ( ) . AsTestDocument ( ) ;
69+ test . Context . Response . StatusCode = 401 ;
70+ test . Context . Response . StatusReason = "Unauthorized" ;
71+ var cache = test . SetupCacheStore ( ) ;
72+ test . SetupCacheInfo ( ) . WithExecutedCacheLookup ( ) ;
73+ test . SetupOutbound ( ) . CacheStore ( ) . WithCacheKey ( "key" ) ;
74+
75+ test . RunOutbound ( ) ;
76+
77+ cache . InternalCache . Should ( ) . NotContainKey ( "key" ) ;
78+ }
79+
80+ [ TestMethod ]
81+ public void CacheStore_StoreIfResponseIsNot200_WhenCacheResponseIsSetToTrue ( )
82+ {
83+ var test = new SimpleCacheStoreStoreResponse ( ) . AsTestDocument ( ) ;
84+ var contextResponse = test . Context . Response ;
85+ contextResponse . StatusCode = 401 ;
86+ contextResponse . StatusReason = "Unauthorized" ;
87+ var cache = test . SetupCacheStore ( ) ;
88+ test . SetupCacheInfo ( ) . WithExecutedCacheLookup ( ) ;
89+ test . SetupOutbound ( ) . CacheStore ( ) . WithCacheKey ( "key" ) ;
90+
91+ test . RunOutbound ( ) ;
92+
93+ var cacheValue = cache . InternalCache . Should ( ) . ContainKey ( "key" ) . WhoseValue ;
94+ cacheValue . Duration . Should ( ) . Be ( 10 ) ;
95+ var response = cacheValue . Value . Should ( ) . BeAssignableTo < IResponse > ( ) . Which ;
96+ response . Should ( ) . NotBeSameAs ( contextResponse , "Should be a copy of response" ) ;
97+ response . StatusCode . Should ( ) . Be ( contextResponse . StatusCode ) ;
98+ response . StatusReason . Should ( ) . Be ( contextResponse . StatusReason ) ;
99+ response . Headers . Should ( ) . Equal ( contextResponse . Headers ) ;
100+ }
101+ }
0 commit comments