/
caching-interceptor.acceptance.ts
165 lines (148 loc) · 4.81 KB
/
caching-interceptor.acceptance.ts
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
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
inject,
intercept,
Interceptor,
InvocationContext,
Provider,
ValueOrPromise,
} from '@loopback/context';
import {get, param} from '@loopback/openapi-v3';
import {
Client,
createRestAppClient,
expect,
givenHttpServerConfig,
} from '@loopback/testlab';
import {Request, RestApplication, RestBindings} from '../../..';
describe('caching interceptor', () => {
let client: Client;
let app: RestApplication;
let returnUpperCaseFromCache = false;
let returnLowerCaseFromCache = false;
before(givenAClient);
after(async () => {
await app.stop();
});
context('toUpperCase with bound caching interceptor', () => {
it('invokes the controller method if not cached', async () => {
await client.get('/toUpperCase/Hello').expect(200, 'HELLO');
expect(returnUpperCaseFromCache).to.be.false();
});
it('returns from cache without invoking the controller method', async () => {
for (let i = 0; i <= 5; i++) {
await client.get('/toUpperCase/Hello').expect(200, 'HELLO');
expect(returnUpperCaseFromCache).to.be.true();
}
});
it('invokes the controller method after cache is cleared', async () => {
CachingInterceptorProvider.clearCache();
await client.get('/toUpperCase/Hello').expect(200, 'HELLO');
expect(returnUpperCaseFromCache).to.be.false();
});
});
context('toLowerCase with cache interceptor function', () => {
it('invokes the controller method if not cached', async () => {
await client.get('/toLowerCase/Hello').expect(200, 'hello');
expect(returnLowerCaseFromCache).to.be.false();
});
it('returns from cache without invoking the controller method', async () => {
for (let i = 0; i <= 5; i++) {
await client.get('/toLowerCase/Hello').expect(200, 'hello');
expect(returnLowerCaseFromCache).to.be.true();
}
});
it('invokes the controller method after cache is cleared', async () => {
cachedResults.clear();
await client.get('/toLowerCase/Hello').expect(200, 'hello');
expect(returnLowerCaseFromCache).to.be.false();
});
});
async function givenAClient() {
app = new RestApplication({rest: givenHttpServerConfig()});
app.bind('caching-interceptor').toProvider(CachingInterceptorProvider);
app.controller(StringCaseController);
await app.start();
client = createRestAppClient(app);
}
/**
* A controller using interceptors for caching
*/
class StringCaseController {
@intercept('caching-interceptor')
@get('/toUpperCase/{text}')
toUpperCase(@param.path.string('text') text: string) {
return text.toUpperCase();
}
@intercept(cache)
@get('/toLowerCase/{text}')
toLowerCase(@param.path.string('text') text: string) {
return text.toLowerCase();
}
}
/**
* A provider class for caching interceptor that leverages dependency
* injection
*/
class CachingInterceptorProvider implements Provider<Interceptor> {
private static cache = new Map<string, unknown>();
static clearCache() {
this.cache.clear();
}
constructor(
@inject(RestBindings.Http.REQUEST, {optional: true})
private request: Request | undefined,
) {}
value() {
return async <T>(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<T>,
) => {
returnUpperCaseFromCache = false;
if (!this.request) {
// The method is not invoked by an http request, no caching
return await next();
}
const url = this.request.url;
const cachedValue = CachingInterceptorProvider.cache.get(url);
if (cachedValue) {
returnUpperCaseFromCache = true;
return cachedValue as T;
}
const result = await next();
CachingInterceptorProvider.cache.set(url, result);
return result;
};
}
}
/**
* An interceptor function that caches results. It uses `invocationContext`
* to locate the http request
*/
const cachedResults = new Map<string, unknown>();
async function cache<T>(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<T>,
) {
returnLowerCaseFromCache = false;
const req = await invocationCtx.get(RestBindings.Http.REQUEST, {
optional: true,
});
if (!req) {
// The method is not invoked by an http request, no caching
return await next();
}
const url = req.url;
const cachedValue = cachedResults.get(url);
if (cachedValue) {
returnLowerCaseFromCache = true;
return cachedValue as T;
}
const result = await next();
cachedResults.set(url, result);
return result;
}
});