Skip to content

Commit 125836d

Browse files
committed
Update test to match ticket version
1 parent 05914f2 commit 125836d

5 files changed

Lines changed: 87 additions & 49 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function createCached(fn) {
2+
return async function (...args) {
3+
return fn(...args);
4+
};
5+
}
6+
7+
module.exports = { createCached };
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const endpoints = [];
2+
3+
function apiEndpoint(method, path, handler) {
4+
endpoints.push({ method, path, handler });
5+
}
6+
7+
function createRouteConfig(method, path, handler) {
8+
const hapiHandler = async function (request, h) {
9+
return handler(request.params, request.query, request.payload);
10+
};
11+
12+
return {
13+
method,
14+
path,
15+
handler: hapiHandler,
16+
};
17+
}
18+
19+
function registerEndpoints(server, serviceInstance) {
20+
for (const definition of endpoints) {
21+
const wrapped = async (params, query, payload) => {
22+
return definition.handler.call(serviceInstance, params, query, payload);
23+
};
24+
25+
const config = createRouteConfig(definition.method, definition.path, wrapped);
26+
server.route(config);
27+
}
28+
}
29+
30+
module.exports = { apiEndpoint, registerEndpoints };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { createCached } = require("./wrapped-route-cache");
2+
3+
class ReportService {
4+
async runQuery(filter) {
5+
sink(filter);
6+
}
7+
}
8+
9+
const service = new ReportService();
10+
11+
const getReportCached = createCached(async function (filter) {
12+
return service.runQuery(filter);
13+
});
14+
15+
module.exports = { getReportCached };
Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
1-
const Hapi = require("hapi");
2-
3-
const endpoints = [];
4-
5-
function endpoint(handler) {
6-
endpoints.push({ handler });
7-
}
8-
9-
function routeConfig(handler) {
10-
return {
11-
handler: async function (request, h) {
12-
return handler(request.query);
13-
},
14-
};
15-
}
16-
17-
function createCached(fn) {
18-
return async function (...args) {
19-
return fn(...args);
20-
};
21-
}
22-
23-
const cached = createCached(function (filter) {
24-
sink(filter);
25-
});
26-
27-
class Routes {
28-
get(query) {
29-
return cached(query.filter);
1+
const Hapi = require("@hapi/hapi");
2+
const {
3+
apiEndpoint,
4+
registerEndpoints,
5+
} = require("./wrapped-route-configurator");
6+
const { getReportCached } = require("./wrapped-route-service");
7+
8+
class ReportRoutes {
9+
async getReport(params, query) {
10+
return getReportCached(query.filter);
3011
}
3112
}
3213

33-
endpoint(Routes.prototype.get);
14+
apiEndpoint("GET", "/reports", ReportRoutes.prototype.getReport);
3415

35-
function register(server, instance) {
36-
for (const definition of endpoints) {
37-
const wrapped = async (query) => definition.handler.call(instance, query);
38-
server.route(routeConfig(wrapped));
39-
}
16+
async function main() {
17+
const server = Hapi.server();
18+
registerEndpoints(server, new ReportRoutes());
19+
return server;
4020
}
4121

42-
register(new Hapi.Server(), new Routes());
22+
main();

javascript/ql/test/library-tests/frameworks/hapi/tests.expected

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test_RouteSetup
1414
| src/hapihapi.js:17:1:18:2 | server2 ... dler\\n}) |
1515
| src/hapihapi.js:29:1:29:20 | server2.route(route) |
1616
| src/hapihapi.js:36:1:36:38 | server2 ... ler()}) |
17-
| src/wrapped-route.js:38:5:38:38 | server. ... apped)) |
17+
| src/wrapped-route-configurator.js:26:5:26:24 | server.route(config) |
1818
test_RequestExpr
1919
| src/hapi.js:13:32:13:38 | request | src/hapi.js:13:14:15:5 | functio ... n\\n } |
2020
| src/hapi.js:13:32:13:38 | request | src/hapi.js:13:14:15:5 | functio ... n\\n } |
@@ -65,9 +65,11 @@ test_RequestExpr
6565
| src/hapihapi.js:25:3:25:9 | request | src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} |
6666
| src/hapihapi.js:26:3:26:9 | request | src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} |
6767
| src/hapihapi.js:34:22:34:24 | req | src/hapihapi.js:34:12:34:30 | function (req, h){} |
68-
| src/wrapped-route.js:11:30:11:36 | request | src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } |
69-
| src/wrapped-route.js:11:30:11:36 | request | src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } |
70-
| src/wrapped-route.js:12:22:12:28 | request | src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } |
68+
| src/wrapped-route-configurator.js:8:39:8:45 | request | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
69+
| src/wrapped-route-configurator.js:8:39:8:45 | request | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
70+
| src/wrapped-route-configurator.js:9:20:9:26 | request | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
71+
| src/wrapped-route-configurator.js:9:36:9:42 | request | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
72+
| src/wrapped-route-configurator.js:9:51:9:57 | request | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
7173
test_HeaderAccess
7274
| src/hapi.js:25:3:25:21 | request.headers.baz | baz |
7375
| src/hapiglue.js:27:3:27:21 | request.headers.baz | baz |
@@ -92,7 +94,7 @@ test_RouteHandler
9294
| src/hapihapi.js:17:30:18:1 | functio ... ndler\\n} | src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
9395
| src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} | src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
9496
| src/hapihapi.js:34:12:34:30 | function (req, h){} | src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
95-
| src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } | src/wrapped-route.js:42:10:42:26 | new Hapi.Server() |
97+
| src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } | src/wrapped-route.js:17:18:17:30 | Hapi.server() |
9698
test_HeaderDefinition
9799
| src/hapi.js:14:9:14:46 | request ... 1', '') | src/hapi.js:13:14:15:5 | functio ... n\\n } |
98100
| src/hapiglue.js:14:9:14:46 | request ... 1', '') | src/hapiglue.js:13:14:15:5 | functio ... n\\n } |
@@ -106,7 +108,7 @@ test_ServerDefinition
106108
| src/hapiglue.js:44:45:44:51 | server_ |
107109
| src/hapihapi.js:1:15:1:50 | new (re ... erver() |
108110
| src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
109-
| src/wrapped-route.js:42:10:42:26 | new Hapi.Server() |
111+
| src/wrapped-route.js:17:18:17:30 | Hapi.server() |
110112
test_RequestInputAccess
111113
| src/hapi.js:21:3:21:20 | request.rawPayload | body | src/hapi.js:20:1:27:1 | functio ... oken;\\n} |
112114
| src/hapi.js:22:3:22:33 | getRequ ... oad.foo | body | src/hapi.js:20:1:27:1 | functio ... oken;\\n} |
@@ -129,7 +131,9 @@ test_RequestInputAccess
129131
| src/hapihapi.js:24:3:24:18 | request.url.path | url | src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} |
130132
| src/hapihapi.js:25:3:25:21 | request.headers.baz | header | src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} |
131133
| src/hapihapi.js:26:3:26:21 | request.state.token | cookie | src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} |
132-
| src/wrapped-route.js:12:22:12:34 | request.query | parameter | src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } |
134+
| src/wrapped-route-configurator.js:9:20:9:33 | request.params | parameter | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
135+
| src/wrapped-route-configurator.js:9:36:9:48 | request.query | parameter | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
136+
| src/wrapped-route-configurator.js:9:51:9:65 | request.payload | body | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
133137
test_RouteSetup_getServer
134138
| src/hapi.js:7:1:9:2 | server2 ... ler1\\n}) | src/hapi.js:4:15:4:31 | new Hapi.Server() |
135139
| src/hapi.js:12:1:15:7 | server2 ... }}) | src/hapi.js:4:15:4:31 | new Hapi.Server() |
@@ -146,7 +150,7 @@ test_RouteSetup_getServer
146150
| src/hapihapi.js:17:1:18:2 | server2 ... dler\\n}) | src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
147151
| src/hapihapi.js:29:1:29:20 | server2.route(route) | src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
148152
| src/hapihapi.js:36:1:36:38 | server2 ... ler()}) | src/hapihapi.js:4:15:4:31 | new Hapi.Server() |
149-
| src/wrapped-route.js:38:5:38:38 | server. ... apped)) | src/wrapped-route.js:42:10:42:26 | new Hapi.Server() |
153+
| src/wrapped-route-configurator.js:26:5:26:24 | server.route(config) | src/wrapped-route.js:17:18:17:30 | Hapi.server() |
150154
test_HeaderDefinition_defines
151155
| src/hapi.js:14:9:14:46 | request ... 1', '') | header1 | |
152156
| src/hapiglue.js:14:9:14:46 | request ... 1', '') | header1 | |
@@ -173,7 +177,7 @@ test_RouteSetup_getARouteHandler
173177
| src/hapihapi.js:36:1:36:38 | server2 ... ler()}) | src/hapihapi.js:33:1:35:1 | return of function getHandler |
174178
| src/hapihapi.js:36:1:36:38 | server2 ... ler()}) | src/hapihapi.js:34:12:34:30 | function (req, h){} |
175179
| src/hapihapi.js:36:1:36:38 | server2 ... ler()}) | src/hapihapi.js:36:25:36:36 | getHandler() |
176-
| src/wrapped-route.js:38:5:38:38 | server. ... apped)) | src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } |
180+
| src/wrapped-route-configurator.js:26:5:26:24 | server.route(config) | src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } |
177181
test_RouteHandler_getARequestExpr
178182
| src/hapi.js:13:14:15:5 | functio ... n\\n } | src/hapi.js:13:32:13:38 | request |
179183
| src/hapi.js:13:14:15:5 | functio ... n\\n } | src/hapi.js:13:32:13:38 | request |
@@ -224,9 +228,11 @@ test_RouteHandler_getARequestExpr
224228
| src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} | src/hapihapi.js:25:3:25:9 | request |
225229
| src/hapihapi.js:20:1:27:1 | functio ... oken;\\n} | src/hapihapi.js:26:3:26:9 | request |
226230
| src/hapihapi.js:34:12:34:30 | function (req, h){} | src/hapihapi.js:34:22:34:24 | req |
227-
| src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } | src/wrapped-route.js:11:30:11:36 | request |
228-
| src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } | src/wrapped-route.js:11:30:11:36 | request |
229-
| src/wrapped-route.js:11:14:13:5 | async f ... ;\\n } | src/wrapped-route.js:12:22:12:28 | request |
231+
| src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } | src/wrapped-route-configurator.js:8:39:8:45 | request |
232+
| src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } | src/wrapped-route-configurator.js:8:39:8:45 | request |
233+
| src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } | src/wrapped-route-configurator.js:9:20:9:26 | request |
234+
| src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } | src/wrapped-route-configurator.js:9:36:9:42 | request |
235+
| src/wrapped-route-configurator.js:8:23:10:3 | async f ... d);\\n } | src/wrapped-route-configurator.js:9:51:9:57 | request |
230236
test_HeaderDefinition_getAHeaderName
231237
| src/hapi.js:14:9:14:46 | request ... 1', '') | header1 |
232238
| src/hapiglue.js:14:9:14:46 | request ... 1', '') | header1 |
@@ -236,4 +242,4 @@ test_RouteHandler_getAResponseHeader
236242
| src/hapiglue.js:13:14:15:5 | functio ... n\\n } | header1 | src/hapiglue.js:14:9:14:46 | request ... 1', '') |
237243
| src/hapihapi.js:13:14:15:5 | functio ... n\\n } | header1 | src/hapihapi.js:14:9:14:46 | request ... 1', '') |
238244
test_WrappedRouteFlow
239-
| src/wrapped-route.js:12:22:12:34 | request.query | src/wrapped-route.js:24:8:24:13 | filter |
245+
| src/wrapped-route-configurator.js:9:36:9:48 | request.query | src/wrapped-route-service.js:5:10:5:15 | filter |

0 commit comments

Comments
 (0)