From 3f386fd82cc2d8733d080d3204420223ecc57666 Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Tue, 17 May 2016 18:41:16 -0700 Subject: [PATCH] Return redirect information in body for 3xx responses --- lib/result.js | 15 +++++++ test/fastboot-response-test.js | 1 - test/result-test.js | 71 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/result-test.js diff --git a/lib/result.js b/lib/result.js index e9c08fc..8fd0947 100644 --- a/lib/result.js +++ b/lib/result.js @@ -22,6 +22,21 @@ class Result { * @returns {Promise} 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 = ''; + this._head = ''; + this._body = ''; + + if (location) { + this._body = `

Redirecting to ${location}

`; + } + } + return Promise.resolve(insertIntoIndexHTML(this._html, this._head, this._body)); } diff --git a/test/fastboot-response-test.js b/test/fastboot-response-test.js index 6c8ad10..74c498b 100644 --- a/test/fastboot-response-test.js +++ b/test/fastboot-response-test.js @@ -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'); diff --git a/test/result-test.js b/test/result-test.js new file mode 100644 index 0000000..273af40 --- /dev/null +++ b/test/result-test.js @@ -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 = ` + `; + + 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(''); + expect(result).to.include('Redirecting to'); + expect(result).to.include('http://some.example.com/page'); + expect(result).to.include(''); + }); + }); + }); + + describe('when the response status code is not 3XX', function () { + var HEAD = ''; + var BODY = '

A normal response document

'; + + 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); + }); + }); + }); + }); +});