Last active
February 9, 2016 01:28
-
-
Save ariutta/972aba5adfb4063d1876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('es6-promise').Promise; | |
var superagent = require('superagent'); | |
var prune = function(res) { | |
return { | |
body: res.body, | |
text: res.text, | |
headers: res.headers, | |
statusCode: res.statusCode, | |
status: res.status, | |
ok: res.ok, | |
redirects: res.redirects | |
}; | |
}; | |
// Try disabling this to see it pass | |
require('superagent-cache')(superagent, null, {prune: prune}); | |
function runSuperagent(url, redirectsExpected) { | |
return new Promise(function(resolve, reject) { | |
superagent | |
.get(url) | |
.set('Accept', 'application/ld+json, application/json') | |
.end(function(err, res) { | |
if (err) { | |
reject(err); | |
} | |
var redirectsActualString = JSON.stringify(res.redirects); | |
var redirectsExpectedString = JSON.stringify(redirectsExpected); | |
if (redirectsActualString !== redirectsExpectedString) { | |
var message = 'Incorrect redirects value for ' + url + '\n' + | |
'Expected: ' + redirectsExpectedString + '\n' + | |
'Actual: ' + redirectsActualString; | |
var err = new Error(message); | |
reject(err); | |
} else { | |
resolve(); | |
} | |
}); | |
}); | |
} | |
function runNoRedirects(input) { | |
return runSuperagent( | |
'http://json-ld.org/test-suite/tests/remote-doc-0001-in.jsonld', | |
[]); | |
} | |
var runWithRedirectsList = [ | |
function(input) { | |
return runSuperagent( | |
'http://json-ld.org/test-suite/tests/remote-doc-0005-in.jsonld', | |
['http://json-ld.org/test-suite/tests/remote-doc-0001-in.jsonld']); | |
}, | |
function(input) { | |
return runSuperagent( | |
'http://json-ld.org/test-suite/tests/remote-doc-0006-in.jsonld', | |
['http://json-ld.org/test-suite/tests/remote-doc-0001-in.jsonld']); | |
} | |
]; | |
// each of the following fails with superagent-cache | |
// but not just with superagent alone | |
// Uncomment them individually to test. | |
/* | |
runNoRedirects() | |
.then(runWithRedirectsList[0]) | |
.then(runWithRedirectsList[0]) | |
.then(runWithRedirectsList[1]) | |
.catch(console.error); | |
//*/ | |
/* | |
runNoRedirects() | |
.then(runWithRedirectsList[0]) | |
.then(runWithRedirectsList[1]) | |
.then(runWithRedirectsList[0]) | |
.catch(console.error); | |
//*/ | |
/* | |
runNoRedirects() | |
.then(runWithRedirectsList[1]) | |
.then(runWithRedirectsList[0]) | |
.then(runWithRedirectsList[0]) | |
.catch(console.error); | |
//*/ | |
/* | |
runWithRedirectsList[1]() | |
.then(runNoRedirects) | |
.then(runWithRedirectsList[0]) | |
.then(runWithRedirectsList[0]) | |
.catch(console.error); | |
//*/ | |
/* | |
runWithRedirectsList[1]() | |
.then(runWithRedirectsList[0]) | |
.then(runNoRedirects) | |
.then(runWithRedirectsList[0]) | |
.catch(console.error); | |
//*/ | |
/* | |
runWithRedirectsList[1]() | |
.then(runWithRedirectsList[0]) | |
.then(runWithRedirectsList[0]) | |
.then(runNoRedirects) | |
.catch(console.error); | |
//*/ | |
//* | |
runWithRedirectsList[0]() | |
.then(runWithRedirectsList[1]) | |
.then(runWithRedirectsList[0]) | |
.then(runNoRedirects) | |
.catch(console.error); | |
//*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment