-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathfastboot-express-middleware-test.mjs
More file actions
120 lines (103 loc) · 3.89 KB
/
fastboot-express-middleware-test.mjs
File metadata and controls
120 lines (103 loc) · 3.89 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
import qunit from 'qunit';
import { merge } from 'lodash-es';
import { join } from 'path';
import { appScenarios } from './scenarios.mjs';
import fastbootMiddleware from 'fastboot-express-middleware';
import TestHTTPServer from './helpers/test-http-server.mjs';
const { module: Qmodule, test } = qunit;
Object.assign(qunit.assert, {
matches(actual, regex, message) {
var success = !!regex && !!actual && new RegExp(regex).test(actual);
var expected = 'String matching /' + regex.toString() + '/';
this.push(success, actual, expected, message);
},
notMatches(actual, regex, message) {
var success = !!regex && !!actual && !new RegExp(regex).test(actual);
var expected = 'String should not be matching /' + regex.toString() + '/';
this.push(success, actual, expected, message);
},
});
appScenarios
.map('fastboot-express-middleware', (project) => {
merge(project.files, {
app: {
routes: {
'index.js': `import Route from '@ember/routing/route';
import * as serviceModule from '@ember/service';
const service = serviceModule.service || serviceModule.inject;
function isEmptyObject(obj) {
return Object.keys(obj).length === 0 && obj.constructor.name === 'Object';
}
export default class IndexRoute extends Route {
@service fastboot;
model() {
// this.fastboot.metadata defaults to an empty object so we need to check for that case here
if (this.fastboot && this.fastboot.metadata && !isEmptyObject(this.fastboot.metadata)) {
return this.fastboot.metadata;
}
return "Hello Ember!";
}
}`,
'application.js': `import Route from '@ember/routing/route';
import * as serviceModule from '@ember/service';
const service = serviceModule.service || serviceModule.inject;
export default class ApplicationRoute extends Route {
@service fastboot;
afterModel() {
if (this.fastboot.isFastBoot) {
this.fastboot.response.headers.append('X-FastBoot', 'a');
this.fastboot.response.headers.append('X-FastBoot', 'b');
this.fastboot.response.headers.append('X-FastBoot', 'c');
}
}
}
`,
},
templates: {
'index.hbs': '{{@model}}',
},
},
});
})
.forEachScenario((scenario) => {
Qmodule(scenario.name, function (hooks) {
let app; // PreparedApp
let server;
hooks.before(async () => {
app = await scenario.prepare();
await app.execute(`node node_modules/ember-cli/bin/ember build`);
});
hooks.afterEach(async () => {
await server.stop();
});
test('it appends multivalue headers', async function (assert) {
server = new TestHTTPServer(fastbootMiddleware(join(app.dir, 'dist')));
await server.start();
let { headers } = await server.request('/', { resolveWithFullResponse: true });
assert.deepEqual(headers['x-fastboot'], 'a, b, c');
});
test('can pass metadata info to the app', async function (assert) {
server = new TestHTTPServer(
fastbootMiddleware({
distPath: join(app.dir, 'dist'),
visitOptions: {
metadata: 'Fastboot Metadata',
},
})
);
await server.start();
let html = await server.request('/');
assert.matches(html, /Fastboot Metadata/);
});
test('works without metadata passed', async function (assert) {
server = new TestHTTPServer(
fastbootMiddleware({
distPath: join(app.dir, 'dist'),
})
);
await server.start();
let html = await server.request('/');
assert.matches(html, /Hello Ember!/);
});
});
});