forked from embermap/ember-cli-fastboot-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
140 lines (116 loc) · 3.32 KB
/
helpers.js
File metadata and controls
140 lines (116 loc) · 3.32 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const { URL } = require('url');
const JSONfn = require('json-fn');
const FastBoot = require('fastboot');
const bodyParser = require('body-parser');
const { deprecate } = require('util');
let _nock;
function getNock() {
if (!_nock) {
_nock = require('nock');
}
return _nock;
}
function createMockRequest(app) {
app.post(
'/__mock-request',
bodyParser.json({ limit: '50mb' }),
(req, res) => {
const requestOrigin = req.body.origin || req.headers.origin;
let mock = getNock()(requestOrigin)
.persist()
.intercept(req.body.path, req.body.method)
.reply(
req.body.statusCode,
req.body.response,
req.body.responseHeaders,
);
res.json({ mocks: mock.pendingMocks() });
},
);
}
function createCleanUpMocks(app) {
app.use('/__cleanup-mocks', (req, res) => {
getNock().cleanAll();
res.json({ ok: true });
});
}
function createFastbootTest(app, callback) {
app.post('/__fastboot-testing', bodyParser.json(), (req, res) => {
const urlToVisit = decodeURIComponent(req.body.url);
const origin = req.headers.origin
? req.headers.origin // Available when tests are run with `--server`
: new URL(req.headers.referer).origin; // Otherwise extract origin from referer
const parsed = new URL(urlToVisit, origin);
const headers = Object.assign(
{},
req.headers,
JSONfn.parse(req.body.options).headers || {},
);
const defaultOptions = {
request: {
method: 'GET',
protocol: 'http',
url: parsed.pathname,
query: Object.fromEntries(parsed.searchParams),
headers,
},
response: {},
};
const options = Object.assign(
defaultOptions,
JSONfn.parse(req.body.options),
);
res.set('x-fastboot-testing', true);
callback({ req, res, options, urlToVisit });
});
}
function createFastbootEcho(app) {
app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => {
res.send(req.body);
});
}
function reloadServer(fastboot, distPath) {
fastboot.reload({ distPath });
}
function createServer(distPath, pkg) {
const options = makeFastbootTestingConfig({ distPath }, pkg);
return new FastBoot(options);
}
function makeFastbootTestingConfig(config, pkg) {
const defaults = {
setupFastboot() {},
};
let configPath = 'config';
if (pkg['ember-addon'] && pkg['ember-addon']['configPath']) {
configPath = pkg['ember-addon']['configPath'];
}
const fastbootTestConfigPath = path.resolve(
configPath,
'fastboot-testing.js',
);
let customized = {};
if (fs.existsSync(fastbootTestConfigPath)) {
const fastbootTestConfig = require(fastbootTestConfigPath);
if (typeof fastbootTestConfig === 'function') {
customized = fastbootTestConfig();
} else {
deprecate(
() => Object.assign(customized, fastbootTestConfig),
`Exporting an object from ${fastbootTestConfigPath} has been deprecated. Please export a function which returns an object instead.`,
)();
}
}
return Object.assign({}, config, defaults, customized);
}
module.exports = {
createMockRequest,
createCleanUpMocks,
createFastbootTest,
createFastbootEcho,
reloadServer,
createServer,
makeFastbootTestingConfig,
};