-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathapp-server-test.js
More file actions
145 lines (126 loc) · 4.77 KB
/
app-server-test.js
File metadata and controls
145 lines (126 loc) · 4.77 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
141
142
143
144
145
"use strict";
const path = require('path');
const fork = require('child_process').fork;
const expect = require('chai').expect;
const alchemistRequire = require('broccoli-module-alchemist/require');
const FastBootAppServer = alchemistRequire('fastboot-app-server');
const request = require('request-promise').defaults({ simple: false, resolveWithFullResponse: true });
let server;
describe("FastBootAppServer", function() {
this.timeout(3000);
afterEach(function() {
if (server) {
server.kill();
}
});
it("throws if no distPath or downloader is provided", function() {
expect(() => {
new FastBootAppServer();
}).to.throw(/must be provided with either a distPath or a downloader/);
});
it("throws if both a distPath and downloader are provided", function() {
expect(() => {
new FastBootAppServer({
downloader: {},
distPath: 'some/dist/path'
});
}).to.throw(/FastBootAppServer must be provided with either a distPath or a downloader option, but not both/);
});
it("serves an HTTP 500 response if the app can't be found", function() {
return runServer('not-found-server')
.then(() => request('http://localhost:3000'))
.then(response => {
expect(response.statusCode).to.equal(500);
expect(response.body).to.match(/No Application Found/);
});
});
it("serves static assets", function() {
return runServer('basic-app-server')
.then(() => request('http://localhost:3000/assets/fastboot-test.js'))
.then(response => {
expect(response.statusCode).to.equal(200);
expect(response.body).to.contain('"use strict";');
})
.then(() => request('http://localhost:3000/'))
.then(response => {
expect(response.statusCode).to.equal(200);
expect(response.body).to.contain('Welcome to Ember');
});
});
it("returns a 404 status code for non-existent assets", function() {
return runServer('basic-app-server')
.then(() => request('http://localhost:3000/assets/404-does-not-exist.js'))
.then(response => {
expect(response.statusCode).to.equal(404);
expect(response.body).to.match(/Not Found/);
})
.then(() => request('http://localhost:3000/'))
.then(response => {
expect(response.statusCode).to.equal(200);
expect(response.body).to.contain('Welcome to Ember');
});
});
it("returns a 404 status code for forbidden assets", function() {
return runServer('basic-app-server')
.then(() => request('http://localhost:3000/package.json'))
.then(response => {
expect(response.statusCode).to.equal(404);
expect(response.body).to.match(/Not Found/);
})
.then(() => request('http://localhost:3000/'))
.then(response => {
expect(response.statusCode).to.equal(200);
expect(response.body).to.contain('Welcome to Ember');
});
});
it("executes beforeMiddleware", function() {
return runServer('before-middleware-server')
.then(() => request('http://localhost:3000'))
.then(response => {
expect(response.statusCode).to.equal(418);
expect(response.headers['x-test-header']).to.equal('testing');
expect(response.body).to.equal(JSON.stringify({ send: 'json back'}));
});
});
it("executes afterMiddleware when there is an error", function() {
return runServer('after-middleware-server')
.then(() => request('http://localhost:3000'))
.then(response => {
expect(response.body).to.not.match(/error/);
expect(response.headers['x-test-header']).to.equal('testing');
});
});
it("returns a 401 status code for non-authenticated request", function() {
return runServer('auth-app-server')
.then(() => request('http://localhost:3000/'))
.then(response => {
expect(response.statusCode).to.equal(401);
expect(response.headers['www-authenticate']).equal('Basic realm=Authorization Required');
})
.then(() => request({ uri: 'http://localhost:3000/', headers: { 'Authorization': 'Basic dG9tc3Rlcjp6b2V5' }}))
.then(response => {
expect(response.statusCode).to.equal(200);
expect(response.body).to.contain('Welcome to Ember');
});
});
});
function runServer(name) {
return new Promise((res, rej) => {
let serverPath = path.join(__dirname, 'fixtures', `${name}.js`);
server = fork(serverPath, {
silent: true
});
server.on('error', rej);
server.stdout.on('data', data => {
if (data.toString().match(/HTTP server started/)) {
res();
}
});
server.stderr.on('data', data => {
console.log(data.toString());
});
server.stdout.on('data', data => {
console.log(data.toString());
});
});
}