Skip to content

Commit

Permalink
Merge pull request #20 from Financial-Times/rhys/no-jsonp
Browse files Browse the repository at this point in the history
removed jsonp now we have a same domain proxy
  • Loading branch information
wheresrhys authored Jul 28, 2016
2 parents c0c4446 + 71c509b commit 3a273cf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 103 deletions.
68 changes: 4 additions & 64 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,15 @@
'use strict';

var sessionServiceEndpoint = 'https://session-next.ft.com';
var jsonpTimeout = 2000;

window.FT = window.FT || {};

var jsonpCallbacks = [];

function generateCallbackName(){
var base = 'sessionServiceJsonpCallback';
var callbackName = base + '_' + (jsonpCallbacks.length+1);
jsonpCallbacks.push(callbackName);
return callbackName;
}

function fetchRequest(url){
return fetch(sessionServiceEndpoint + url, {credentials:'include'});
}

function jsonpRequest(url){
return new Promise(function(resolve, reject){
var callbackName = generateCallbackName();
var timeout;
window.FT[callbackName] = function(response){

try {
response = JSON.parse(response);
} catch (e) {
reject(new Error('Error parsing session response'));
return;
}

if (response.success) {
resolve({
ok:response.success,
json: function(){
return Promise.resolve(response.data);
}
});
} else {
reject(new Error('JSONp session request failed: ' + url));
}

if (timeout){
clearTimeout(timeout);
}
};

var scriptTag = document.createElement('script');
scriptTag.async = true;
scriptTag.defer = true;
scriptTag.src = sessionServiceEndpoint + url + '?callback=FT.'+callbackName;
document.body.appendChild(scriptTag);

timeout = setTimeout(function(){
reject(new Error('JSONP request to ' + url + ' timed out'));
}, jsonpTimeout);
});
}




function request(url){
// if we don't have a session token cookie, don't bother..
if (document.cookie.indexOf('FTSession=') === -1) {
return Promise.reject(new Error('No session cookie found'));
}

var requestFunc = ('XDomainRequest' in window) ? jsonpRequest : fetchRequest;
return requestFunc(url).then(function(response){
return fetch(`https://session-next.ft.com${url}`, {
credentials:'include',
useCorsProxy: true
}).then(function(response){
if (response.ok){
return (url === '/validate') ? Promise.resolve(true) : response.json();
} else {
Expand Down
40 changes: 1 addition & 39 deletions test/request.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
'use strict';
/*global describe, it, before, afterEach*/

var expect = require('chai').expect;
var sinon = require('sinon');
var request = require('../src/request');

describe('request', function(){

var jsonpCallbackName = 'sessionServiceJsonpCallback_';
var jsonpCallbackNames = [];

var callbackName;

before(function(){
if(document.cookie.indexOf('FTSession=') < 0){
Expand All @@ -19,7 +14,6 @@ describe('request', function(){
});

afterEach(function(){
delete window.XDomainRequest;
if(document.body.appendChild.restore){
document.body.appendChild.restore();
}
Expand All @@ -33,48 +27,16 @@ describe('request', function(){
window.fetch = sinon.stub().returns(Promise.resolve({ok:true, json:jsonFunc}));
}

function setupJsonp(response){
jsonpCallbackNames.push(jsonpCallbackName + (jsonpCallbackNames.length+1));
callbackName = jsonpCallbackNames[jsonpCallbackNames.length-1];
sinon.stub(document.body, 'appendChild', function(){
var jsonpResponse = JSON.stringify(response);
setTimeout(function(){
window.FT[callbackName](jsonpResponse);
}, 500);
});
}

it('Should select the correct mechanism based on CORS support', function(done){
window.XDomainRequest = {};
setupJsonp({success:true,data:{}});
request('/').then(function(){
sinon.assert.called(document.body.appendChild);
done();
}).catch(done);
});

it('Should be able to make a CORS request to the session service', function(done){
var email = 'paul.i.wilson@ft.com';
setupFetch({email:email});
request('/').then(function(){
try{
sinon.assert.calledWith(window.fetch, 'https://session-next.ft.com/', {credentials:'include'});
sinon.assert.calledWith(window.fetch, 'https://session-next.ft.com/', {credentials:'include', useCorsProxy: true});
}catch(e){
done(e);
}
done();
}).catch(done);
});

it('Should be able to make a JSONP request to the session service', function(done){
var email = 'paul.i.wilson@ft.com';
window.XDomainRequest = {};
setupJsonp({success:true,data:{email:email}});
request('/').then(function(){
sinon.assert.called(document.body.appendChild);
var script = document.body.appendChild.lastCall.args[0];
expect(script.src).to.equal('https://session-next.ft.com/?callback=FT.'+callbackName);
done();
}).catch(done);
});
});

0 comments on commit 3a273cf

Please sign in to comment.