@flink-app/test-utils 0.12.1-alpha.0 → 0.12.1-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/http.d.ts CHANGED
@@ -1,15 +1,30 @@
1
1
  import { FlinkApp, FlinkResponse } from "@flink-app/flink";
2
- export declare type HttpOpts = {
2
+ export type HttpOpts = {
3
+ /***
4
+ * Optional query string
5
+ */
3
6
  qs?: {
4
7
  [x: string]: string | string[];
5
8
  };
9
+ /**
10
+ * Optional headers
11
+ */
6
12
  headers?: {
7
13
  [x: string]: string;
8
14
  };
15
+ /**
16
+ * Optional user object to be used for authentication.
17
+ *
18
+ * An auth plugin must be set in the app for this to work. Most likely the auth plugin
19
+ * is a mock auth plugin in test scenarios.
20
+ */
21
+ user?: any & {
22
+ roles: string[];
23
+ };
9
24
  };
10
25
  /**
11
26
  * Initializes test flink app.
12
- * Should be invoked prior to using test HTTP methods.
27
+ * Must be invoked prior to using test HTTP methods.
13
28
  */
14
29
  export declare function init(_app: FlinkApp<any>, host?: string): void;
15
30
  export declare function get<Res = any>(path: string, opts?: HttpOpts): Promise<FlinkResponse<Res>>;
package/dist/http.js CHANGED
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
25
25
  function verb(n) { return function (v) { return step([n, v]); }; }
26
26
  function step(op) {
27
27
  if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
29
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
30
  if (y = 0, t) op = [op[0] & 2, t.value];
31
31
  switch (op[0]) {
@@ -62,24 +62,27 @@ var gotOpts = {
62
62
  };
63
63
  /**
64
64
  * Initializes test flink app.
65
- * Should be invoked prior to using test HTTP methods.
65
+ * Must be invoked prior to using test HTTP methods.
66
66
  */
67
67
  function init(_app, host) {
68
68
  if (host === void 0) { host = "localhost"; }
69
69
  app = _app;
70
- baseUrl = "http://" + host + ":" + app.port;
70
+ baseUrl = "http://".concat(host, ":").concat(app.port);
71
71
  }
72
72
  exports.init = init;
73
- function get(path, opts) {
74
- if (opts === void 0) { opts = {}; }
75
- return __awaiter(this, void 0, void 0, function () {
76
- var res;
73
+ function get(path_1) {
74
+ return __awaiter(this, arguments, void 0, function (path, opts) {
75
+ var headers, res;
76
+ if (opts === void 0) { opts = {}; }
77
77
  return __generator(this, function (_a) {
78
78
  switch (_a.label) {
79
79
  case 0:
80
80
  validateApp();
81
- return [4 /*yield*/, got_1.default.get(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
81
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
82
82
  case 1:
83
+ headers = _a.sent();
84
+ return [4 /*yield*/, got_1.default.get(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: headers }))];
85
+ case 2:
83
86
  res = _a.sent();
84
87
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
85
88
  }
@@ -87,33 +90,47 @@ function get(path, opts) {
87
90
  });
88
91
  }
89
92
  exports.get = get;
90
- function post(path, body, opts) {
91
- if (opts === void 0) { opts = {}; }
92
- return __awaiter(this, void 0, void 0, function () {
93
- var res;
93
+ function post(path_1, body_1) {
94
+ return __awaiter(this, arguments, void 0, function (path, body, opts) {
95
+ var headers, res, err_1;
96
+ if (opts === void 0) { opts = {}; }
94
97
  return __generator(this, function (_a) {
95
98
  switch (_a.label) {
96
99
  case 0:
97
100
  validateApp();
98
- return [4 /*yield*/, got_1.default.post(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
101
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
99
102
  case 1:
103
+ headers = _a.sent();
104
+ _a.label = 2;
105
+ case 2:
106
+ _a.trys.push([2, 4, , 5]);
107
+ return [4 /*yield*/, got_1.default.post(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: headers }))];
108
+ case 3:
100
109
  res = _a.sent();
101
110
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
111
+ case 4:
112
+ err_1 = _a.sent();
113
+ console.error(err_1);
114
+ throw err_1;
115
+ case 5: return [2 /*return*/];
102
116
  }
103
117
  });
104
118
  });
105
119
  }
106
120
  exports.post = post;
107
- function put(path, body, opts) {
108
- if (opts === void 0) { opts = {}; }
109
- return __awaiter(this, void 0, void 0, function () {
110
- var res;
121
+ function put(path_1, body_1) {
122
+ return __awaiter(this, arguments, void 0, function (path, body, opts) {
123
+ var headers, res;
124
+ if (opts === void 0) { opts = {}; }
111
125
  return __generator(this, function (_a) {
112
126
  switch (_a.label) {
113
127
  case 0:
114
128
  validateApp();
115
- return [4 /*yield*/, got_1.default.put(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: opts.headers }))];
129
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
116
130
  case 1:
131
+ headers = _a.sent();
132
+ return [4 /*yield*/, got_1.default.put(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { body: body, headers: headers }))];
133
+ case 2:
117
134
  res = _a.sent();
118
135
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
119
136
  }
@@ -121,16 +138,19 @@ function put(path, body, opts) {
121
138
  });
122
139
  }
123
140
  exports.put = put;
124
- function del(path, opts) {
125
- if (opts === void 0) { opts = {}; }
126
- return __awaiter(this, void 0, void 0, function () {
127
- var res;
141
+ function del(path_1) {
142
+ return __awaiter(this, arguments, void 0, function (path, opts) {
143
+ var headers, res;
144
+ if (opts === void 0) { opts = {}; }
128
145
  return __generator(this, function (_a) {
129
146
  switch (_a.label) {
130
147
  case 0:
131
148
  validateApp();
132
- return [4 /*yield*/, got_1.default.delete(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: opts.headers }))];
149
+ return [4 /*yield*/, setAuthHeader(opts.user, opts.headers)];
133
150
  case 1:
151
+ headers = _a.sent();
152
+ return [4 /*yield*/, got_1.default.delete(getUrl(path, opts.qs), __assign(__assign({}, gotOpts), { headers: headers }))];
153
+ case 2:
134
154
  res = _a.sent();
135
155
  return [2 /*return*/, __assign({ status: res.statusCode }, res.body)];
136
156
  }
@@ -149,3 +169,24 @@ function getUrl(path, queryString) {
149
169
  }
150
170
  return baseUrl + path;
151
171
  }
172
+ function setAuthHeader(user, headers) {
173
+ return __awaiter(this, void 0, void 0, function () {
174
+ var token;
175
+ return __generator(this, function (_a) {
176
+ switch (_a.label) {
177
+ case 0:
178
+ if (!user)
179
+ return [2 /*return*/, headers];
180
+ headers = headers || {};
181
+ if (!app.auth) {
182
+ throw new Error("Auth plugin not set, cannot use user option");
183
+ }
184
+ return [4 /*yield*/, app.auth.createToken(user, user.roles || [])];
185
+ case 1:
186
+ token = _a.sent();
187
+ headers["Authorization"] = "Bearer ".concat(token);
188
+ return [2 /*return*/, headers];
189
+ }
190
+ });
191
+ });
192
+ }
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/dist/mocks.js CHANGED
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
25
25
  function verb(n) { return function (v) { return step([n, v]); }; }
26
26
  function step(op) {
27
27
  if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
29
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
30
  if (y = 0, t) op = [op[0] & 2, t.value];
31
31
  switch (op[0]) {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@flink-app/test-utils",
3
- "version": "0.12.1-alpha.0",
3
+ "version": "0.12.1-alpha.10",
4
4
  "description": "Test utils for Flink",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\"",
7
- "prepublish": "tsc",
7
+ "prepare": "tsc",
8
8
  "build": "tsc",
9
9
  "watch": "tsc-watch"
10
10
  },
@@ -20,15 +20,15 @@
20
20
  "qs": "^6.7.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@flink-app/flink": "^0.12.1-alpha.0",
23
+ "@flink-app/flink": "^0.12.1-alpha.10",
24
24
  "@types/express": "^4.17.12",
25
25
  "@types/got": "^9.6.12",
26
- "@types/node": "^15.6.2",
26
+ "@types/node": "22.13.10",
27
27
  "jasmine-spec-reporter": "^7.0.0",
28
28
  "nodemon": "^2.0.7",
29
29
  "ts-node": "^9.1.1",
30
30
  "tsc-watch": "^4.2.9",
31
- "typescript": "^4.2.4"
31
+ "typescript": "5.4.5"
32
32
  },
33
- "gitHead": "7f6e32aa896f5cade313f3c5133c756fc5391ce8"
33
+ "gitHead": "d4019c3af70673f27db4df55d56ba3728e75745e"
34
34
  }
package/src/http.ts CHANGED
@@ -12,13 +12,26 @@ const gotOpts: GotJSONOptions = {
12
12
  };
13
13
 
14
14
  export type HttpOpts = {
15
+ /***
16
+ * Optional query string
17
+ */
15
18
  qs?: { [x: string]: string | string[] };
19
+ /**
20
+ * Optional headers
21
+ */
16
22
  headers?: { [x: string]: string };
23
+ /**
24
+ * Optional user object to be used for authentication.
25
+ *
26
+ * An auth plugin must be set in the app for this to work. Most likely the auth plugin
27
+ * is a mock auth plugin in test scenarios.
28
+ */
29
+ user?: any & { roles: string[] };
17
30
  };
18
31
 
19
32
  /**
20
33
  * Initializes test flink app.
21
- * Should be invoked prior to using test HTTP methods.
34
+ * Must be invoked prior to using test HTTP methods.
22
35
  */
23
36
  export function init(_app: FlinkApp<any>, host = "localhost") {
24
37
  app = _app;
@@ -27,9 +40,12 @@ export function init(_app: FlinkApp<any>, host = "localhost") {
27
40
 
28
41
  export async function get<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
29
42
  validateApp();
43
+
44
+ const headers = await setAuthHeader(opts.user, opts.headers);
45
+
30
46
  const res = await got.get(getUrl(path, opts.qs), {
31
47
  ...gotOpts,
32
- headers: opts.headers,
48
+ headers,
33
49
  });
34
50
  return {
35
51
  status: res.statusCode,
@@ -39,24 +55,35 @@ export async function get<Res = any>(path: string, opts: HttpOpts = {}): Promise
39
55
 
40
56
  export async function post<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
41
57
  validateApp();
42
- const res = await got.post(getUrl(path, opts.qs), {
43
- ...gotOpts,
44
- body: body as any,
45
- headers: opts.headers,
46
- });
47
58
 
48
- return {
49
- status: res.statusCode,
50
- ...res.body,
51
- };
59
+ const headers = await setAuthHeader(opts.user, opts.headers);
60
+
61
+ try {
62
+ const res = await got.post(getUrl(path, opts.qs), {
63
+ ...gotOpts,
64
+ body: body as any,
65
+ headers,
66
+ });
67
+
68
+ return {
69
+ status: res.statusCode,
70
+ ...res.body,
71
+ };
72
+ } catch (err) {
73
+ console.error(err);
74
+ throw err;
75
+ }
52
76
  }
53
77
 
54
78
  export async function put<Req = any, Res = any>(path: string, body: Req, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
55
79
  validateApp();
80
+
81
+ const headers = await setAuthHeader(opts.user, opts.headers);
82
+
56
83
  const res = await got.put(getUrl(path, opts.qs), {
57
84
  ...gotOpts,
58
85
  body: body as any,
59
- headers: opts.headers,
86
+ headers,
60
87
  });
61
88
  return {
62
89
  status: res.statusCode,
@@ -66,9 +93,12 @@ export async function put<Req = any, Res = any>(path: string, body: Req, opts: H
66
93
 
67
94
  export async function del<Res = any>(path: string, opts: HttpOpts = {}): Promise<FlinkResponse<Res>> {
68
95
  validateApp();
96
+
97
+ const headers = await setAuthHeader(opts.user, opts.headers);
98
+
69
99
  const res = await got.delete(getUrl(path, opts.qs), {
70
100
  ...gotOpts,
71
- headers: opts.headers,
101
+ headers,
72
102
  });
73
103
  return {
74
104
  status: res.statusCode,
@@ -88,3 +118,16 @@ function getUrl(path: string, queryString?: HttpOpts["qs"]) {
88
118
  }
89
119
  return baseUrl + path;
90
120
  }
121
+
122
+ async function setAuthHeader(user?: any, headers?: HttpOpts["headers"]) {
123
+ if (!user) return headers;
124
+
125
+ headers = headers || {};
126
+
127
+ if (!app.auth) {
128
+ throw new Error("Auth plugin not set, cannot use user option");
129
+ }
130
+ const token = await app.auth.createToken(user, user.roles || []);
131
+ headers["Authorization"] = `Bearer ${token}`;
132
+ return headers;
133
+ }