@webex/test-helper-server 2.59.2 → 2.59.3-next.1

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/src/index.js CHANGED
@@ -1,283 +1,283 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- /* eslint max-nested-callbacks: [2, 3] */
6
- /* eslint no-console: [0] */
7
-
8
- const btoa = require(`btoa`);
9
- const bodyParser = require(`body-parser`);
10
- const browserify = require(`browserify-middleware`);
11
- const compression = require(`compression`);
12
- const cors = require(`cors`);
13
- const express = require(`express`);
14
- const fs = require(`fs`);
15
- const glob = require(`glob`);
16
- const http = require(`http`);
17
- const morgan = require(`morgan`);
18
- const path = require(`path`);
19
- const querystring = require(`querystring`);
20
- const request = require(`request`);
21
- const url = require(`url`);
22
- const base64 = require(`urlsafe-base64`);
23
-
24
- const app = express();
25
-
26
- // Configure Logging
27
- // -----------------
28
-
29
- if (process.env.DEBUG) {
30
- app.use(
31
- morgan(`short`, {
32
- immediate: true,
33
- })
34
- );
35
- }
36
-
37
- // Configure CORS
38
- // --------------
39
-
40
- app.use(
41
- cors({
42
- credentials: true,
43
- origin(o, callback) {
44
- callback(null, true);
45
- },
46
- })
47
- );
48
-
49
- // Configure body processing
50
- // -------------------------
51
-
52
- app.use(bodyParser.raw({type: `image/*`}));
53
-
54
- // Enable gzip/deflate
55
- // -------------------
56
-
57
- app.use(compression());
58
-
59
- // Close all connections
60
- // ---------------------
61
-
62
- // This *should* help tests run faster in IE, which has a very low number of
63
- // allowed connections to the same origin.
64
- app.use((req, res, next) => {
65
- res.set(`connection`, `close`);
66
- next();
67
- });
68
-
69
- // Configure Browserify
70
- // --------------------
71
-
72
- const appPattern = `packages/{*,*/*}/test/automation/fixtures/app.js`;
73
-
74
- glob.sync(appPattern).forEach((appjs) => {
75
- const packageName = appjs
76
- .replace(`packages/`, ``)
77
- .replace(`/test/automation/fixtures/app.js`, ``);
78
-
79
- // eslint-disable-next-line no-sync
80
- fs.statSync(appjs);
81
- app.use(
82
- `/${packageName}/app.js`,
83
- browserify(appjs, {
84
- debug: true,
85
- transform: [`babelify`, `envify`],
86
- })
87
- );
88
- });
89
-
90
- // Enable active routes
91
- // --------------------
92
-
93
- app.get('/', (req, res) => {
94
- res.send(`<!DOCTYPE html>
95
- <html>
96
- <head>
97
- <title>Redirect Dispatcher</title>
98
- <script>
99
- try {
100
- var state = /state=(.+?)(&.*)?$/.exec(window.location)[1]
101
- console.info('state', state);
102
- var name = JSON.parse(atob(state)).name;
103
- console.info('name', name);
104
- window.location.pathname = name;
105
- }
106
- catch(err) {
107
- console.warn(err);
108
- }
109
- </script>
110
- </head>
111
- <body>
112
-
113
- </body>
114
- </html>`);
115
- });
116
-
117
- app.use(`/cookies`, require(`./cookies`));
118
- app.use(`/json`, require(`./json`));
119
- app.use(`/form`, require(`./form`));
120
- app.use(`/files`, require(`./files`));
121
- app.use(`/jwt`, require(`@webex/test-helper-appid`).router);
122
-
123
- app.get(`/requires-basic-auth`, (req, res) => {
124
- if (req.headers.authorization === `Basic ${btoa(`basicuser:basicpass`)}`) {
125
- res.status(200).send().end();
126
- } else {
127
- res.status(403).send().end();
128
- }
129
- });
130
-
131
- app.get(`/requires-bearer-auth`, (req, res) => {
132
- if (req.headers.authorization === `Bearer bearertoken`) {
133
- res.status(200).send().end();
134
- } else {
135
- res.status(403).send().end();
136
- }
137
- });
138
-
139
- app.get(`/return-qs-as-object`, (req, res) => {
140
- res.status(200).json(req.query).end();
141
- });
142
-
143
- app.get(`/embargoed`, (req, res) => {
144
- res.status(451).end();
145
- });
146
-
147
- // Enable static routes
148
- // --------------------
149
-
150
- const fixturePattern = `packages/{*,*/*}/test/automation/fixtures`;
151
-
152
- glob.sync(fixturePattern).forEach((fixturePath) => {
153
- const packageName = fixturePath.replace(`packages/`, ``).replace(`/test/automation/fixtures`, ``);
154
-
155
- app.get(`/${packageName}`, (req, res, next) => {
156
- if (!req.query.code) {
157
- next();
158
-
159
- return;
160
- }
161
-
162
- const state = JSON.parse(base64.decode(req.query.state));
163
-
164
- if (state.exchange === false) {
165
- next();
166
-
167
- return;
168
- }
169
-
170
- request(
171
- {
172
- /* eslint-disable camelcase */
173
- method: `POST`,
174
- uri: `${
175
- process.env.IDBROKER_BASE_URL || `https://idbroker.webex.com`
176
- }/idb/oauth2/v1/access_token`,
177
- form: {
178
- grant_type: `authorization_code`,
179
- redirect_uri: process.env.WEBEX_REDIRECT_URI,
180
- code: req.query.code,
181
- self_contained_token: true,
182
- },
183
- auth: {
184
- user: process.env.WEBEX_CLIENT_ID,
185
- pass: process.env.WEBEX_CLIENT_SECRET,
186
- sendImmediately: true,
187
- },
188
- /* eslint-enable camelcase */
189
- },
190
- (err, response) => {
191
- if (err) {
192
- console.warn(`Request to CI failed with non-HTTP error`);
193
- next(err);
194
-
195
- return;
196
- }
197
- if (response.statusCode >= 400) {
198
- console.warn(`Got unexpected response from CI`);
199
- next(new Error(response.body));
200
-
201
- return;
202
- }
203
- let redirect = url.parse(req.url, true);
204
- const qs = querystring.stringify({state: req.query.state, ...JSON.parse(response.body)});
205
-
206
- redirect = `${redirect.pathname}#${qs}`;
207
-
208
- console.info(`redirecting to ${redirect}`);
209
- res.redirect(redirect);
210
- }
211
- );
212
- });
213
- app.use(`/${packageName}`, express.static(fixturePath));
214
- });
215
-
216
- app.post(`/refresh`, bodyParser.json(), (req, res, next) => {
217
- if (!req.body.refresh_token) {
218
- next(new Error(`\`refresh_token\` is required`));
219
-
220
- return;
221
- }
222
- console.info(`Refreshing access token`);
223
- request(
224
- {
225
- /* eslint-disable camelcase */
226
- method: `POST`,
227
- uri: `${
228
- process.env.IDBROKER_BASE_URL || `https://idbroker.webex.com`
229
- }/idb/oauth2/v1/access_token`,
230
- form: {
231
- grant_type: `refresh_token`,
232
- redirect_uri: process.env.WEBEX_REDIRECT_URI,
233
- refresh_token: req.body.refresh_token,
234
- },
235
- auth: {
236
- user: process.env.WEBEX_CLIENT_ID,
237
- pass: process.env.WEBEX_CLIENT_SECRET,
238
- sendImmediately: true,
239
- },
240
- /* eslint-enable camelcase */
241
- },
242
- (err, response) => {
243
- if (err) {
244
- console.warn(`Request to CI failed with non-HTTP error`);
245
- next(err);
246
-
247
- return;
248
- }
249
- if (response.statusCode >= 400) {
250
- console.warn(`Got unexpected response from CI`);
251
- next(new Error(response.body));
252
-
253
- return;
254
- }
255
-
256
- console.info(`Returning new access token`);
257
- res.status(200).json(JSON.parse(response.body)).end();
258
- }
259
- );
260
- });
261
-
262
- app.use(express.static(path.resolve(__dirname, '..', `static`)));
263
-
264
- // Start the server
265
- // ----------------
266
-
267
- const port = parseInt(process.env.SERVER_PORT, 10) || 8000;
268
-
269
- http.createServer(app).listen(port, () => {
270
- console.log(`Express server listening on port ${port}`);
271
- });
272
-
273
- const fixtureport = parseInt(process.env.FIXTURE_PORT, 10) || 3000;
274
-
275
- http.createServer(app).listen(fixtureport, () => {
276
- console.log(`Express server listening on port ${fixtureport}`);
277
- });
278
-
279
- const corsport = parseInt(process.env.CORS_PORT, 10) || 3002;
280
-
281
- http.createServer(app).listen(corsport, () => {
282
- console.log(`Express server listening on port ${corsport}`);
283
- });
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ /* eslint max-nested-callbacks: [2, 3] */
6
+ /* eslint no-console: [0] */
7
+
8
+ const btoa = require(`btoa`);
9
+ const bodyParser = require(`body-parser`);
10
+ const browserify = require(`browserify-middleware`);
11
+ const compression = require(`compression`);
12
+ const cors = require(`cors`);
13
+ const express = require(`express`);
14
+ const fs = require(`fs`);
15
+ const glob = require(`glob`);
16
+ const http = require(`http`);
17
+ const morgan = require(`morgan`);
18
+ const path = require(`path`);
19
+ const querystring = require(`querystring`);
20
+ const request = require(`request`);
21
+ const url = require(`url`);
22
+ const base64 = require(`urlsafe-base64`);
23
+
24
+ const app = express();
25
+
26
+ // Configure Logging
27
+ // -----------------
28
+
29
+ if (process.env.DEBUG) {
30
+ app.use(
31
+ morgan(`short`, {
32
+ immediate: true,
33
+ })
34
+ );
35
+ }
36
+
37
+ // Configure CORS
38
+ // --------------
39
+
40
+ app.use(
41
+ cors({
42
+ credentials: true,
43
+ origin(o, callback) {
44
+ callback(null, true);
45
+ },
46
+ })
47
+ );
48
+
49
+ // Configure body processing
50
+ // -------------------------
51
+
52
+ app.use(bodyParser.raw({type: `image/*`}));
53
+
54
+ // Enable gzip/deflate
55
+ // -------------------
56
+
57
+ app.use(compression());
58
+
59
+ // Close all connections
60
+ // ---------------------
61
+
62
+ // This *should* help tests run faster in IE, which has a very low number of
63
+ // allowed connections to the same origin.
64
+ app.use((req, res, next) => {
65
+ res.set(`connection`, `close`);
66
+ next();
67
+ });
68
+
69
+ // Configure Browserify
70
+ // --------------------
71
+
72
+ const appPattern = `packages/{*,*/*}/test/automation/fixtures/app.js`;
73
+
74
+ glob.sync(appPattern).forEach((appjs) => {
75
+ const packageName = appjs
76
+ .replace(`packages/`, ``)
77
+ .replace(`/test/automation/fixtures/app.js`, ``);
78
+
79
+ // eslint-disable-next-line no-sync
80
+ fs.statSync(appjs);
81
+ app.use(
82
+ `/${packageName}/app.js`,
83
+ browserify(appjs, {
84
+ debug: true,
85
+ transform: [`babelify`, `envify`],
86
+ })
87
+ );
88
+ });
89
+
90
+ // Enable active routes
91
+ // --------------------
92
+
93
+ app.get('/', (req, res) => {
94
+ res.send(`<!DOCTYPE html>
95
+ <html>
96
+ <head>
97
+ <title>Redirect Dispatcher</title>
98
+ <script>
99
+ try {
100
+ var state = /state=(.+?)(&.*)?$/.exec(window.location)[1]
101
+ console.info('state', state);
102
+ var name = JSON.parse(atob(state)).name;
103
+ console.info('name', name);
104
+ window.location.pathname = name;
105
+ }
106
+ catch(err) {
107
+ console.warn(err);
108
+ }
109
+ </script>
110
+ </head>
111
+ <body>
112
+
113
+ </body>
114
+ </html>`);
115
+ });
116
+
117
+ app.use(`/cookies`, require(`./cookies`));
118
+ app.use(`/json`, require(`./json`));
119
+ app.use(`/form`, require(`./form`));
120
+ app.use(`/files`, require(`./files`));
121
+ app.use(`/jwt`, require(`@webex/test-helper-appid`).router);
122
+
123
+ app.get(`/requires-basic-auth`, (req, res) => {
124
+ if (req.headers.authorization === `Basic ${btoa(`basicuser:basicpass`)}`) {
125
+ res.status(200).send().end();
126
+ } else {
127
+ res.status(403).send().end();
128
+ }
129
+ });
130
+
131
+ app.get(`/requires-bearer-auth`, (req, res) => {
132
+ if (req.headers.authorization === `Bearer bearertoken`) {
133
+ res.status(200).send().end();
134
+ } else {
135
+ res.status(403).send().end();
136
+ }
137
+ });
138
+
139
+ app.get(`/return-qs-as-object`, (req, res) => {
140
+ res.status(200).json(req.query).end();
141
+ });
142
+
143
+ app.get(`/embargoed`, (req, res) => {
144
+ res.status(451).end();
145
+ });
146
+
147
+ // Enable static routes
148
+ // --------------------
149
+
150
+ const fixturePattern = `packages/{*,*/*}/test/automation/fixtures`;
151
+
152
+ glob.sync(fixturePattern).forEach((fixturePath) => {
153
+ const packageName = fixturePath.replace(`packages/`, ``).replace(`/test/automation/fixtures`, ``);
154
+
155
+ app.get(`/${packageName}`, (req, res, next) => {
156
+ if (!req.query.code) {
157
+ next();
158
+
159
+ return;
160
+ }
161
+
162
+ const state = JSON.parse(base64.decode(req.query.state));
163
+
164
+ if (state.exchange === false) {
165
+ next();
166
+
167
+ return;
168
+ }
169
+
170
+ request(
171
+ {
172
+ /* eslint-disable camelcase */
173
+ method: `POST`,
174
+ uri: `${
175
+ process.env.IDBROKER_BASE_URL || `https://idbroker.webex.com`
176
+ }/idb/oauth2/v1/access_token`,
177
+ form: {
178
+ grant_type: `authorization_code`,
179
+ redirect_uri: process.env.WEBEX_REDIRECT_URI,
180
+ code: req.query.code,
181
+ self_contained_token: true,
182
+ },
183
+ auth: {
184
+ user: process.env.WEBEX_CLIENT_ID,
185
+ pass: process.env.WEBEX_CLIENT_SECRET,
186
+ sendImmediately: true,
187
+ },
188
+ /* eslint-enable camelcase */
189
+ },
190
+ (err, response) => {
191
+ if (err) {
192
+ console.warn(`Request to CI failed with non-HTTP error`);
193
+ next(err);
194
+
195
+ return;
196
+ }
197
+ if (response.statusCode >= 400) {
198
+ console.warn(`Got unexpected response from CI`);
199
+ next(new Error(response.body));
200
+
201
+ return;
202
+ }
203
+ let redirect = url.parse(req.url, true);
204
+ const qs = querystring.stringify({state: req.query.state, ...JSON.parse(response.body)});
205
+
206
+ redirect = `${redirect.pathname}#${qs}`;
207
+
208
+ console.info(`redirecting to ${redirect}`);
209
+ res.redirect(redirect);
210
+ }
211
+ );
212
+ });
213
+ app.use(`/${packageName}`, express.static(fixturePath));
214
+ });
215
+
216
+ app.post(`/refresh`, bodyParser.json(), (req, res, next) => {
217
+ if (!req.body.refresh_token) {
218
+ next(new Error(`\`refresh_token\` is required`));
219
+
220
+ return;
221
+ }
222
+ console.info(`Refreshing access token`);
223
+ request(
224
+ {
225
+ /* eslint-disable camelcase */
226
+ method: `POST`,
227
+ uri: `${
228
+ process.env.IDBROKER_BASE_URL || `https://idbroker.webex.com`
229
+ }/idb/oauth2/v1/access_token`,
230
+ form: {
231
+ grant_type: `refresh_token`,
232
+ redirect_uri: process.env.WEBEX_REDIRECT_URI,
233
+ refresh_token: req.body.refresh_token,
234
+ },
235
+ auth: {
236
+ user: process.env.WEBEX_CLIENT_ID,
237
+ pass: process.env.WEBEX_CLIENT_SECRET,
238
+ sendImmediately: true,
239
+ },
240
+ /* eslint-enable camelcase */
241
+ },
242
+ (err, response) => {
243
+ if (err) {
244
+ console.warn(`Request to CI failed with non-HTTP error`);
245
+ next(err);
246
+
247
+ return;
248
+ }
249
+ if (response.statusCode >= 400) {
250
+ console.warn(`Got unexpected response from CI`);
251
+ next(new Error(response.body));
252
+
253
+ return;
254
+ }
255
+
256
+ console.info(`Returning new access token`);
257
+ res.status(200).json(JSON.parse(response.body)).end();
258
+ }
259
+ );
260
+ });
261
+
262
+ app.use(express.static(path.resolve(__dirname, '..', `static`)));
263
+
264
+ // Start the server
265
+ // ----------------
266
+
267
+ const port = parseInt(process.env.SERVER_PORT, 10) || 8000;
268
+
269
+ http.createServer(app).listen(port, () => {
270
+ console.log(`Express server listening on port ${port}`);
271
+ });
272
+
273
+ const fixtureport = parseInt(process.env.FIXTURE_PORT, 10) || 3000;
274
+
275
+ http.createServer(app).listen(fixtureport, () => {
276
+ console.log(`Express server listening on port ${fixtureport}`);
277
+ });
278
+
279
+ const corsport = parseInt(process.env.CORS_PORT, 10) || 3002;
280
+
281
+ http.createServer(app).listen(corsport, () => {
282
+ console.log(`Express server listening on port ${corsport}`);
283
+ });
package/src/json.js CHANGED
@@ -1,27 +1,27 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- const bodyParser = require(`body-parser`);
6
- const express = require(`express`);
7
- const reflect = require(`./reflect`);
8
-
9
- /* eslint new-cap: [0] */
10
- const router = express.Router();
11
-
12
- // Configure JSON processing
13
- // -------------------------
14
-
15
- router.use(bodyParser.json());
16
-
17
- router.get(`/get`, (req, res) => {
18
- res.send({
19
- isObject: true,
20
- });
21
- });
22
-
23
- router.patch(`/set`, reflect);
24
- router.post(`/set`, reflect);
25
- router.put(`/set`, reflect);
26
-
27
- module.exports = router;
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ const bodyParser = require(`body-parser`);
6
+ const express = require(`express`);
7
+ const reflect = require(`./reflect`);
8
+
9
+ /* eslint new-cap: [0] */
10
+ const router = express.Router();
11
+
12
+ // Configure JSON processing
13
+ // -------------------------
14
+
15
+ router.use(bodyParser.json());
16
+
17
+ router.get(`/get`, (req, res) => {
18
+ res.send({
19
+ isObject: true,
20
+ });
21
+ });
22
+
23
+ router.patch(`/set`, reflect);
24
+ router.post(`/set`, reflect);
25
+ router.put(`/set`, reflect);
26
+
27
+ module.exports = router;
package/src/redirect.html CHANGED
@@ -1,18 +1,18 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Redirect Dispatcher</title>
5
- <script>
6
- try {
7
- var state = /state=(.+?)(&.*)?$/.exec(window.location)[1];
8
- console.info('state', state);
9
- var name = JSON.parse(atob(state)).name;
10
- console.info('name', name);
11
- window.location.pathname = name;
12
- } catch (err) {
13
- console.warn(err);
14
- }
15
- </script>
16
- </head>
17
- <body></body>
18
- </html>
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Redirect Dispatcher</title>
5
+ <script>
6
+ try {
7
+ var state = /state=(.+?)(&.*)?$/.exec(window.location)[1];
8
+ console.info('state', state);
9
+ var name = JSON.parse(atob(state)).name;
10
+ console.info('name', name);
11
+ window.location.pathname = name;
12
+ } catch (err) {
13
+ console.warn(err);
14
+ }
15
+ </script>
16
+ </head>
17
+ <body></body>
18
+ </html>