Skip to content

Commit

Permalink
Return redirect information in body for 3xx responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjan Singh committed May 25, 2016
1 parent 4e30b91 commit 3f386fd
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ class Result {
* @returns {Promise<String>} the application's DOM serialized to HTML
*/
html() {
let response = this._fastbootInfo.response;
let statusCode = response && this._fastbootInfo.response.statusCode;

if (statusCode >= 300 && statusCode <= 399) {
let location = response.headers.get('location');

this._html = '<body><!-- EMBER_CLI_FASTBOOT_BODY --></body>';
this._head = '';
this._body = '';

if (location) {
this._body = `<h1>Redirecting to <a href="${location}">${location}</a></h1>`;
}
}

return Promise.resolve(insertIntoIndexHTML(this._html, this._head, this._body));
}

Expand Down
1 change: 0 additions & 1 deletion test/fastboot-response-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var expect = require('chai').expect;
var path = require('path');
var FastBootHeaders = require('../lib/fastboot-headers.js');
var FastBootResponse = require('../lib/fastboot-response.js');

Expand Down
71 changes: 71 additions & 0 deletions test/result-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var expect = require('chai').expect;
var Result = require('../lib/result.js');
var FastBootInfo = require('../lib/fastboot-info.js');
var SimpleDOM = require('simple-dom');

describe('Result', function() {
var doc, result;

beforeEach(function () {
var req = { get() {} };

doc = new SimpleDOM.Document();
html = `<!-- EMBER_CLI_FASTBOOT_HEAD -->
<!-- EMBER_CLI_FASTBOOT_BODY -->`;

result = new Result({
doc: doc,
html: html,
fastbootInfo: new FastBootInfo(req, {}, [ 'example.com' ])
});
});

it('constructor', function () {
expect(result).to.be.an.instanceOf(Result);
expect(result._doc).to.be.an.instanceOf(SimpleDOM.Document);
expect(result._html).to.be.a('string');
expect(result._fastbootInfo).to.be.an.instanceOf(FastBootInfo);
});

describe('html()', function () {

describe('when the response status code is 3XX', function () {
beforeEach(function () {
result._fastbootInfo.response.headers.set('location', 'http://some.example.com/page');
result._fastbootInfo.response.statusCode = 307;
result._finalize();
});

it('should return a document body with redirect information', function () {
return result.html()
.then(function (result) {
expect(result).to.include('<body>');
expect(result).to.include('Redirecting to');
expect(result).to.include('http://some.example.com/page');
expect(result).to.include('</body>');
});
});
});

describe('when the response status code is not 3XX', function () {
var HEAD = '<meta name="foo" content="bar">';
var BODY = '<h1>A normal response document</h1>';

beforeEach(function () {
doc.head.appendChild(doc.createRawHTMLSection(HEAD));
doc.body.appendChild(doc.createRawHTMLSection(BODY));

result._fastbootInfo.response.statusCode = 418;
result._finalize();
});

it('should return the FastBoot-rendered document body', function () {
return result.html()
.then(function (result) {
expect(result).to.include(HEAD);
expect(result).to.include(BODY);
});
});
});
});
});

0 comments on commit 3f386fd

Please sign in to comment.