@@ -19,6 +19,7 @@ describe("Middleware | Utils | cache", function () {
19
19
} ;
20
20
21
21
const response = {
22
+ statusCode : 200 ,
22
23
send : sinon . spy ( ) ,
23
24
} ;
24
25
@@ -39,14 +40,16 @@ describe("Middleware | Utils | cache", function () {
39
40
expect ( response . send . callCount ) . to . equal ( 2 ) ;
40
41
} ) ;
41
42
42
- it ( "should invalidate stale the response" , function ( ) {
43
+ it ( "should invalidate stale response" , function ( ) {
43
44
const cacheTestKey = "__cache__2" ;
44
45
45
46
const request = {
46
47
method : "GET" ,
47
48
originalUrl : "/test2" ,
48
49
} ;
49
50
const response = {
51
+ on : sinon . spy ( ) ,
52
+ statusCode : 200 ,
50
53
send : sinon . spy ( ) ,
51
54
} ;
52
55
@@ -66,6 +69,9 @@ describe("Middleware | Utils | cache", function () {
66
69
cacheMiddlewareForInvalidation ( request , response , nextSpy ) ;
67
70
response . send ( responseBody ) ;
68
71
72
+ response . on . withArgs ( "finish" ) . yield ( ) ;
73
+
74
+ expect ( response . on . callCount ) . to . equal ( 1 ) ;
69
75
expect ( nextSpy . callCount ) . to . equal ( 2 ) ;
70
76
expect ( response . send . callCount ) . to . equal ( 2 ) ;
71
77
@@ -75,4 +81,79 @@ describe("Middleware | Utils | cache", function () {
75
81
expect ( nextSpy . callCount ) . to . equal ( 3 ) ;
76
82
expect ( response . send . callCount ) . to . equal ( 3 ) ;
77
83
} ) ;
84
+
85
+ it ( "should not cache the response" , function ( ) {
86
+ const cacheTestKey = "__cache__3" ;
87
+ const request = {
88
+ method : "GET" ,
89
+ originalUrl : "/test3" ,
90
+ } ;
91
+
92
+ const response = {
93
+ statusCode : 400 ,
94
+ send : sinon . spy ( ) ,
95
+ } ;
96
+
97
+ const nextSpy = sinon . spy ( ) ;
98
+
99
+ const cacheMiddleware = cacheResponse ( { invalidationKey : cacheTestKey } ) ;
100
+
101
+ cacheMiddleware ( request , response , nextSpy ) ;
102
+
103
+ response . send ( responseBody ) ;
104
+
105
+ expect ( nextSpy . callCount ) . to . equal ( 1 ) ;
106
+ expect ( response . send . callCount ) . to . equal ( 1 ) ;
107
+
108
+ cacheMiddleware ( request , response , nextSpy ) ;
109
+ response . send ( responseBody ) ;
110
+
111
+ expect ( nextSpy . callCount ) . to . equal ( 2 ) ;
112
+
113
+ expect ( response . send . callCount ) . to . equal ( 2 ) ;
114
+ } ) ;
115
+
116
+ it ( "should not invalidate stale the response if theres an error" , function ( ) {
117
+ const cacheTestKey = "__cache__4" ;
118
+
119
+ const request = {
120
+ method : "GET" ,
121
+ originalUrl : "/test4" ,
122
+ } ;
123
+ const response = {
124
+ on : sinon . spy ( ) ,
125
+ statusCode : 200 ,
126
+ send : sinon . spy ( ) ,
127
+ } ;
128
+
129
+ const nextSpy = sinon . spy ( ) ;
130
+
131
+ const cacheMiddlewareForCache = cacheResponse ( { invalidationKey : cacheTestKey } ) ;
132
+
133
+ cacheMiddlewareForCache ( request , response , nextSpy ) ;
134
+
135
+ response . send ( responseBody ) ;
136
+
137
+ expect ( nextSpy . callCount ) . to . equal ( 1 ) ;
138
+ expect ( response . send . callCount ) . to . equal ( 1 ) ;
139
+
140
+ response . statusCode = 400 ;
141
+
142
+ const cacheMiddlewareForInvalidation = invalidateCache ( { invalidationKeys : [ cacheTestKey ] } ) ;
143
+
144
+ cacheMiddlewareForInvalidation ( request , response , nextSpy ) ;
145
+ response . send ( responseBody ) ;
146
+
147
+ response . on . withArgs ( "finish" ) . yield ( ) ;
148
+
149
+ expect ( response . on . callCount ) . to . equal ( 1 ) ;
150
+ expect ( nextSpy . callCount ) . to . equal ( 2 ) ;
151
+ expect ( response . send . callCount ) . to . equal ( 2 ) ;
152
+
153
+ response . statusCode = 200 ;
154
+ cacheMiddlewareForCache ( request , response , nextSpy ) ;
155
+
156
+ expect ( nextSpy . callCount ) . to . equal ( 2 ) ;
157
+ expect ( response . send . callCount ) . to . equal ( 3 ) ;
158
+ } ) ;
78
159
} ) ;
0 commit comments