Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Fix: need to follow order of Accept preferences
Browse files Browse the repository at this point in the history
The previous commit broke the browser, because all responses would be JSON.  This fixes that.
  • Loading branch information
Robert committed Mar 15, 2016
1 parent fa4c801 commit 72b07c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/helpers/handle-accept-request.js
Expand Up @@ -44,7 +44,10 @@ function handleAcceptRequest(req, res, handlers, fallbackHandler) {
// always override with the SPA handler if SPA is enabled.
var handler;

allowedResponseTypes.some(function (contentType) {
accepted.some(function (contentType) {
if (allowedResponseTypes.indexOf(contentType) === -1) {
return false;
}
if (config.web.spa.enabled && contentType === 'text/html') {
handler = spaResponseHandler(config);
return true;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/produces-fixture.js
Expand Up @@ -35,6 +35,9 @@ ProducesFixture.prototype.getEndpointWithAccept = function getEndpointWithAccept
.get(stormpathConfig.web.login.uri)
.set('Accept', acceptString);
};
ProducesFixture.prototype.requestAsBrowser = function requestAsBrowser() {
return this.getEndpointWithAccept('text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
};
ProducesFixture.prototype.requestAsJson = function requestAsJson() {
return this.getEndpointWithAccept('application/json');
};
Expand Down
11 changes: 11 additions & 0 deletions test/produces.js
Expand Up @@ -38,6 +38,11 @@ describe('produces option', function () {
fixture.requestAsJson().end(fixture.assertJsonResponse(done));
});
});
describe('and Accept is a browser default', function () {
it('should respond with HTML', function (done) {
fixture.requestAsBrowser().end(fixture.assertHtmlResponse(done));
});
});
describe('and request has no Accept header', function () {
it('should respond with JSON (the first item in the produces list)', function (done) {
fixture.requestWithoutAcceptHeader().end(fixture.assertJsonResponse(done));
Expand Down Expand Up @@ -90,6 +95,12 @@ describe('produces option', function () {
});
});

describe('and Accept is a browser default', function () {
it('should respond with HTML', function (done) {
fixture.requestAsBrowser().end(fixture.assertHtmlResponse(done));
});
});

describe('and request has no Accept header', function () {
it('should respond with HTML', function (done) {
fixture.requestAsHtml().end(fixture.assertHtmlResponse(done));
Expand Down

0 comments on commit 72b07c4

Please sign in to comment.