This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.js
66 lines (51 loc) · 2.08 KB
/
setup.js
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
'use strict';
const App = require('ember-cli-addon-tests').App;
const visit = require('./visit');
const debug = require('debug')('ember-fastboot-app-tests');
const port = 49741;
function setupTestsForFastboot(/* options */) {
let app;
before(function() {
this.visit = function(requestOptions) {
let url = requestOptions;
if (typeof requestOptions === 'object') {
url = requestOptions.uri || requestOptions.url;
if (requestOptions.headers
&& requestOptions.headers.Accept
&& requestOptions.headers.Accept.indexOf('text/html')) {
throw new Error('Accept header must include \'text/html\'');
}
}
if (typeof url === 'undefined') {
throw new Error('visit helper was called without URL. Either pass a string or an object containing an `url` key.');
}
if (!url.match(/^https?:\/\//)) {
let host = `http://localhost:${port}`;
let separator = url.charAt(0) === '/' ? '' : '/';
url = host + separator + url;
}
requestOptions = Object.assign({}, typeof requestOptions === 'object' ? requestOptions : {}, { url });
delete requestOptions.uri;
requestOptions.headers = requestOptions.headers || {};
if (!requestOptions.headers.Accept) {
// We have to send the `Accept` header so the ember-cli server sees this as a request to `index.html` and sets
// `req.serveUrl`, that ember-cli-fastboot needs in its middleware
// See https://github.com/ember-cli/ember-cli/blob/master/lib/tasks/server/middleware/history-support/index.js#L55
// and https://github.com/ember-fastboot/ember-cli-fastboot/blob/master/index.js#L160
requestOptions.headers.Accept = 'text/html';
}
debug(`visiting ${url}`, requestOptions);
return visit(requestOptions);
};
debug('Starting FastBoot server');
app = new App(process.cwd());
return app.startServer({
port: port
});
});
after(function() {
debug('Stopping FastBoot server');
return app.stopServer();
});
}
module.exports = setupTestsForFastboot;