Skip to content

Instantly share code, notes, and snippets.

@ChrisCinelli
Created August 16, 2017 21:02
Show Gist options
  • Save ChrisCinelli/655ae316a9cd0b453aacbd5922209312 to your computer and use it in GitHub Desktop.
Save ChrisCinelli/655ae316a9cd0b453aacbd5922209312 to your computer and use it in GitHub Desktop.
Superagent fail on 4xx with content-type:"text/plain" and a body
// Related tp: https://github.com/visionmedia/superagent/issues/1263
const agent = require("superagent");
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.status(400).end("not good");
})
var mockPort;
var callCounter = 0;
function makeACall(headers, body) {
var _callCounter = ++callCounter;
var request = agent('GET', "http://127.0.0.1:" + mockPort + "/");
request.set(headers);
request.send(body);
console.log(_callCounter, ') superagent calling', JSON.stringify(headers), JSON.stringify(body));
request.then(function (res) {
console.log(_callCounter, ') superagent body ->', res.body);
})
.catch(function (err){
console.error(_callCounter, ') superagent catch -> status ==>', err.status);
console.error(err.stack || err);
console.error();
});
}
// port 0 let operation system choose port
const listener = app.listen(0, function () {
mockPort = listener.address().port;
makeACall({"content-type":"text/plain"});
makeACall({"content-type":"text/plain"}, {foo: 1});
makeACall({});
makeACall({}, {foo: 1});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment