Skip to content

Commit

Permalink
Merge pull request #64 from bobisjan/async-await-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed May 6, 2020
2 parents 0407a0c + cf26df6 commit 9918911
Showing 1 changed file with 88 additions and 118 deletions.
206 changes: 88 additions & 118 deletions test/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,27 @@ describe('FastBoot', function() {
expect(fn).to.throw(/You must instantiate FastBoot with a distPath option/);
});

it('can provide distPath as the first argument', function() {
it('can provide distPath as the first argument', async function() {
let middleware = fastbootMiddleware(fixture('basic-app'));
server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(() => server.request('/'))
.then(html => {
expect(html).to.match(/Welcome to Ember/);
});
let html = await server.request('/');
expect(html).to.match(/Welcome to Ember/);
});

it('can provide distPath as an option', function() {
it('can provide distPath as an option', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('basic-app'),
});
server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(() => server.request('/'))
.then(html => {
expect(html).to.match(/Welcome to Ember/);
});
let html = await server.request('/');
expect(html).to.match(/Welcome to Ember/);
});

it('can be provided with a custom FastBoot instance', function() {
it('can be provided with a custom FastBoot instance', async function() {
let fastboot = new FastBoot({
distPath: fixture('basic-app'),
});
Expand All @@ -62,16 +56,13 @@ describe('FastBoot', function() {
});

server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(() => server.request('/'))
.then(html => {
expect(html).to.match(/Welcome to Ember/);
});
let html = await server.request('/');
expect(html).to.match(/Welcome to Ember/);
});

it('can reload the FastBoot instance', function() {
it('can reload the FastBoot instance', async function() {
let fastboot = new FastBoot({
distPath: fixture('basic-app'),
});
Expand All @@ -81,190 +72,169 @@ describe('FastBoot', function() {
});

server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(requestFirstApp)
.then(hotReloadApp)
.then(requestSecondApp);

function requestFirstApp() {
return server.request('/').then(function(html) {
expect(html).to.match(/Welcome to Ember/);
});
}
let html = await server.request('/');
expect(html).to.match(/Welcome to Ember/);

function hotReloadApp() {
fastboot.reload({
distPath: fixture('hot-swap-app'),
});
}
fastboot.reload({
distPath: fixture('hot-swap-app'),
});

function requestSecondApp() {
return server.request('/').then(function(html) {
expect(html).to.match(/Goodbye from Ember/);
});
}
html = await server.request('/');
expect(html).to.match(/Goodbye from Ember/);
});

/* eslint-disable mocha/no-setup-in-describe */
[true, false].forEach(chunkedResponse => {
describe(`when chunked response is ${chunkedResponse ? 'enabled' : 'disabled'}`, function() {
if (chunkedResponse) {
it('responds with a chunked response', function() {
it('responds with a chunked response', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('basic-app'),
chunkedResponse,
});
server = new TestHTTPServer(middleware, { errorHandling: true });
await server.start();

return server
.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.then(({ body, headers }) => {
expect(headers['transfer-encoding']).to.eq('chunked');
expect(body).to.match(/Welcome to Ember/);
});
let { body, headers } = await server.request('/', { resolveWithFullResponse: true });
expect(headers['transfer-encoding']).to.eq('chunked');
expect(body).to.match(/Welcome to Ember/);
});
}

it("returns 404 when navigating to a URL that doesn't exist", function() {
it("returns 404 when navigating to a URL that doesn't exist", async function() {
let middleware = fastbootMiddleware({
distPath: fixture('basic-app'),
chunkedResponse,
});
server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(() => server.request('/foo-bar-baz/non-existent'))
.catch(result => {
expect(result.statusCode).to.equal(404);
});
try {
await server.request('/foo-bar-baz/non-existent');
} catch (error) {
expect(error.statusCode).to.equal(404);
}
});

it('returns a 500 error if an error occurs', function() {
it('returns a 500 error if an error occurs', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
chunkedResponse,
});
server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(() => server.request('/'))
.catch(err => {
expect(err.message).to.match(/Rejected on purpose/);
});
try {
await server.request('/');
} catch (error) {
expect(error.message).to.match(/Rejected on purpose/);
}
});

describe('when reslient mode is enabled', function() {
it('renders no FastBoot markup', function() {
describe('when resilient mode is enabled', function() {
it('renders no FastBoot markup', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
resilient: true,
chunkedResponse,
});
server = new TestHTTPServer(middleware);
await server.start();

return server
.start()
.then(() => server.request('/'))
.then(html => {
expect(html).to.not.match(/error/);
});
let html = await server.request('/');
expect(html).to.not.match(/error/);
});

it('propagates to error handling middleware', function() {
it('propagates to error handling middleware', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
resilient: true,
chunkedResponse,
});
server = new TestHTTPServer(middleware, { errorHandling: true });
await server.start();

return server
.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.then(({ body, statusCode, headers }) => {
expect(statusCode).to.equal(200);
expect(headers['x-test-error']).to.match(/error handler called/);
expect(body).to.match(/hello world/);
});
let { body, statusCode, headers } = await server.request('/', {
resolveWithFullResponse: true,
});
expect(statusCode).to.equal(200);
expect(headers['x-test-error']).to.match(/error handler called/);
expect(body).to.match(/hello world/);
});

it('is does not propagate errors when and there is no error handling middleware', function() {
it('is does not propagate errors when and there is no error handling middleware', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
resilient: true,
chunkedResponse,
});
server = new TestHTTPServer(middleware, { errorHandling: false });
await server.start();

return server
.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.then(({ body, statusCode, headers }) => {
expect(statusCode).to.equal(200);
expect(headers['x-test-error']).to.not.match(/error handler called/);
expect(body).to.not.match(/error/);
expect(body).to.match(/hello world/);
});
let { body, statusCode, headers } = await server.request('/', {
resolveWithFullResponse: true,
});
expect(statusCode).to.equal(200);
expect(headers['x-test-error']).to.not.match(/error handler called/);
expect(body).to.not.match(/error/);
expect(body).to.match(/hello world/);
});

it('allows post-fastboot middleware to recover the response when it fails', function() {
it('allows post-fastboot middleware to recover the response when it fails', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
resilient: true,
chunkedResponse,
});
server = new TestHTTPServer(middleware, { recoverErrors: true });
await server.start();

return server
.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.then(({ body, statusCode, headers }) => {
expect(statusCode).to.equal(200);
expect(headers['x-test-recovery']).to.match(/recovered response/);
expect(body).to.match(/hello world/);
});
let { body, statusCode, headers } = await server.request('/', {
resolveWithFullResponse: true,
});
expect(statusCode).to.equal(200);
expect(headers['x-test-recovery']).to.match(/recovered response/);
expect(body).to.match(/hello world/);
});
});

describe('when reslient mode is disabled', function() {
it('propagates to error handling middleware', function() {
describe('when resilient mode is disabled', function() {
it('propagates to error handling middleware', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
resilient: false,
chunkedResponse,
});
server = new TestHTTPServer(middleware, { errorHandling: true });
await server.start();

return server
.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.catch(({ statusCode, response: { headers } }) => {
expect(statusCode).to.equal(500);
expect(headers['x-test-error']).to.match(/error handler called/);
try {
await server.request('/', {
resolveWithFullResponse: true,
});
} catch ({ statusCode, response: { headers } }) {
expect(statusCode).to.equal(500);
expect(headers['x-test-error']).to.match(/error handler called/);
}
});

it('allows post-fastboot middleware to recover the response when it fails', function() {
it('allows post-fastboot middleware to recover the response when it fails', async function() {
let middleware = fastbootMiddleware({
distPath: fixture('rejected-promise'),
resilient: false,
chunkedResponse,
});
server = new TestHTTPServer(middleware, { recoverErrors: true });
await server.start();

return server
.start()
.then(() => server.request('/', { resolveWithFullResponse: true }))
.then(({ body, statusCode, headers }) => {
expect(statusCode).to.equal(200);
expect(headers['x-test-recovery']).to.match(/recovered response/);
expect(body).to.match(/hello world/);
});
let { body, statusCode, headers } = await server.request('/', {
resolveWithFullResponse: true,
});
expect(statusCode).to.equal(200);
expect(headers['x-test-recovery']).to.match(/recovered response/);
expect(body).to.match(/hello world/);
});
});
});
Expand Down

0 comments on commit 9918911

Please sign in to comment.