Duplicate Content-Length with Axios running in NodeJS/JEST - jestjs

I face really weird problem with Axios / nanoexpress. I started to write JEST test and copied working piece of code from Vuex that calls my backend. This code works reliably in Vue but here it fails with a network error. The Axios version is the same on both SPA and JEST test.
JEST:
let response = await axios.get(`${API}/users/1234`, getAuthHeader());
function getAuthHeader() {
const config = { headers: { } };
config.headers.Authorization = "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxZTQwdjBiMWo1Iiwibmlja25hbWUiOiJsaXRlcmFrbCIsInB3ZFRpbWVzdGFtcCI6IjIwMjAtMDMtMjJUMTE6MTA6NDkuNDg2WiIsInJvbGVzIjpbImFkbWluOnBvbGwiXSwiaWF0IjoxNTg2NjkwNzc2LCJleHAiOjE1ODkzNjkxNzZ9.N5MfpZ9i9Sjv-izdYItR4gXCmzVkqkuVcVSEL_6Q89c";
return config;
}
When I remove the config with Authorization header, the code works well:
let response = await axios.get(`${API}/users/1234`, getAuthHeader());
This is the error:
Network Error
at createError (node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (node_modules/axios/lib/adapters/xhr.js:83:14)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:316:27)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at XMLHttpRequestEventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at fireAnEvent (node_modules/jsdom/lib/jsdom/living/helpers/events.js:17:36)
at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:121:3)
at Object.dispatchError (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:51:3)
at EventEmitter.<anonymous> (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:384:47)
at Request.onRequestError (node_modules/request/request.js:877:8)
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Error: Parse Error: Duplicate Content-Length
at Object.dispatchError (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:54:19)
at EventEmitter.<anonymous> (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:675:20)
at EventEmitter.emit (events.js:327:22)
at Request.<anonymous> (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:384:47)
at Request.emit (events.js:315:20)
at Request.onRequestError (C:\Users\leos\WebstormProjects\nano-options\node_modules\request\request.js:877:8)
at ClientRequest.emit (events.js:315:20)
at Socket.socketOnData (_http_client.js:486:9)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:297:12) undefined
The service is nothing fancy and it is never called when Authorization header is present and it is called when the Authorization header is missing.
app.get('/v1/users/:userId', async (req, res) => {
console.log("getUser handler starts");
const response = {};
res.send(response);
return res;
});
I have spent several hours trying to see the raw network communication but failed. It must be something with Axios because I can run successfuly exactly the same request with the Authorization header from Postman.
Update:
it seems to be related to NodeJS version. I submitted an issue: https://github.com/axios/axios/issues/2889
Update 2:
I switched from Axios to Got and the test is working properly. https://github.com/literakl/mezinamiridici/commit/e667c5661429fc8cba273716dfced2244e211abf

Related

How to upload file from the back-end to another back-end?

I am trying to upload file from another back-end to currently existing API.
For doing so, I am trying to replicate what's being done in the front-end.
To create FormData and then send it to the API.
But the request seems to hang...
And if I would try to replicate the request express would console log error
Error: Request aborted
at IncomingMessage.<anonymous> (C:\Users\TCP_BCP\node_modules\formidable\lib\incoming_form.js:122:19)
at IncomingMessage.emit (events.js:314:20)
at IncomingMessage.EventEmitter.emit (domain.js:506:15)
at abortIncoming (_http_server.js:533:9)
at socketOnEnd (_http_server.js:549:5)
at Socket.emit (events.js:326:22)
at Socket.EventEmitter.emit (domain.js:483:12)
at endReadableNT (_stream_readable.js:1241:12)
I understand that if I am trying to perform new request the old one gets aborted probably.
But why the request could hang? The target file is small in size (500kb +-).
In the front-end there would be used $(".form").ajaxSubmit
In the back-end I am trying to create form with FormData.
What could be wrong?
So the issues was with headers.
Formidable at other API would hang without passing FormData headers for axios.
Code bellow allowed me to successfully upload files to a other API.
const data = await fs.readFile(zipPath);
const form = new FormData();
form.append("file", data, zipName);
const headers = form.getHeaders();
Axios.defaults.headers.cookie = cookies;
await Axios({
method: "POST",
url: endpoint,
headers: {
...headers,
cookie: cookies,
},
data: form,
});

Cloud Functions 400 Bad Request with invalid JSON in hello world app

I have hello world cloud functions app, it's working fine either locally by firebase serve or online after firebase deploy
const functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Locally I can open http://localhost:5000/testing-98b0b/us-central1/helloWorld without any issues, but if I use postman to send post call with an invalid body like "#" it crashes and keep loading with below error:
error: { SyntaxError: Unexpected token # in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:158:10)
at parse (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:83:15)
at /usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/read.js:121:18
at invokeCallback (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:224:16)
at done (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
expose: true,
statusCode: 400,
status: 400,
body: '#',
type: 'entity.parse.failed' }
Also after firebase deploy I can open
https://us-central1-staff-testing-98b0b.cloudfunctions.net/helloWorld
without any issues but if I use postman to send post call with an invalid body like "#" it crashes and returns 400 bad request with below error:
SyntaxError: Unexpected token # in JSON at position 0
at Object.parse (native)
at createStrictSyntaxError (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:157:10)
at parse (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:83:15)
at /var/tmp/worker/node_modules/body-parser/lib/read.js:121:18
at invokeCallback (/var/tmp/worker/node_modules/raw-body/index.js:224:16)
at done (/var/tmp/worker/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/var/tmp/worker/node_modules/raw-body/index.js:273:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
Is it bug in the firebase-functions library? Is there any way to handle this type of invalid requests?

Not able to receive an attachment from Skype in BotFramework

On BotFramework (NodeJS), I was trying to replicate the demo available at https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-receive-attachments . It actually works well.
Code in case ms article change:
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function (session) {
var msg = session.message;
if (msg.attachments && msg.attachments.length > 0) {
// Echo back attachment
var attachment = msg.attachments[0];
session.send({
text: "You sent:",
attachments: [
{
contentType: attachment.contentType,
contentUrl: attachment.contentUrl,
name: attachment.name
}
]
});
} else {
// Echo back users text
session.send("You said: %s", session.message.text);
}
});
However, the problem I am facing is that when I do a call from Skype (Normal), I receive an error message:
2017-12-07T02:16:15.815Z Error: POST to 'https://smba.trafficmanager.net/apis/v3/conversations/<My Conversation>/activities' failed: [400] Bad Request
at Request._callback (/app/node_modules/botbuilder/lib/bots/ChatConnector.js:545:46)
at Request.self.callback (/app/node_modules/request/request.js:186:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/app/node_modules/request/request.js:1163:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (/app/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
Any ideas?
[Update: It happens only when I create the attachment response. So I expect that's where I have an issue]
Actually the code on the MS Website is not up-to-date (in a way).
If I follow the code visible at : https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-ReceiveAttachment
By example, I can receive the attachment and save it somewhere on a public folder. Once completed, then I can send back the "public" URL as attachment and then it works.

Node server is getting down with error buffer.js:488 throw new Error('toString failed');

We have an app build on nodejs (Server A), which is fetching data from another nodejs app(Server B) using REST API's.
Similarly for downloading files Server A make request on Server B and Server B fetches files from AWS S3 and pipes the data back to server A.
This system works perfectly and we are able to download files upto 1GB.
Issue is sometimes Server A crashes with below error:
buffer.js:488
throw new Error('toString failed');
^
Error: toString failed
at Buffer.toString (buffer.js:488:11)
at Request.<anonymous> (/var/www/sky/portal/node_modules/request/request.js:1145:39)
at emitOne (events.js:95:20)
at Request.emit (events.js:182:7)
at IncomingMessage.<anonymous> (/var/www/sky/portal/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:273:16)
at emitNone (events.js:85:20)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Server A is calling server B using below code:
exports.downloadAsset = function(req, res, assetUrl, cb) {
res.cookie('fileDownload', 'true');
request(
config.api.baseUrl + assetUrl
,
function(error, response, body) {
}
).pipe(res);
}
We are not sure from where app is getting crashed.
Please suggest.
The Solution I used to sort above issue:
I had to modify our flow. We were streaming data from Server B to Server A, but now Server B is sending signed URL to Server A and file is being downloaded directly from S3 using that signed url.

Node.js unexpected token R

I am getting this error from my terminal:
SyntaxError: Unexpected token R
at Object.parse (native)
at Client.exports.json [as parse] (/Users/terrychen/Desktop/Git/ControlPanel/node_modules/hippie/lib/hippie/parsers.js:15:18)
at Request._callback (/Users/terrychen/Desktop/Git/ControlPanel/node_modules/hippie/lib/hippie/client.js:388:10)
at Request.self.callback (/Users/terrychen/Desktop/Git/ControlPanel/node_modules/hippie/node_modules/request/request.js:129:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request.<anonymous> (/Users/terrychen/Desktop/Git/ControlPanel/node_modules/hippie/node_modules/request/request.js:873:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage.<anonymous> (/Users/terrychen/Desktop/Git/ControlPanel/node_modules/hippie/node_modules/request/request.js:824:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
While I am just executing a simple script with hippie API testing module:
var hippie = require('hippie');
hippie()
.json().get('https://api.github.com/users/vesln')
.expectStatus(200)
.end(function(err, res, body) {
if (err) throw err;
});
Any idea why this is happening? Appreciate your answers!
The issue here is that at some point hippie expects the API to be returning JSON to be parsed. And you probably manually checked that URL and saw it was JSON. But this type of error is an indication of bad input into JSON.parse. Commonly you'll see unexpected token u when undefined is passed in.
First step in debugging I took was putting console.log(arguments) before if(err.. while actually only interested in the contents of the res object, it reveals GitHub actually returned an error here.
{
...,
body: 'Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.' }
}
And this is the R of unexpected token, this is not valid JSON. And as the message states, the missing User-Agent header is the likeliest cause.
This code works.
var hippie = require('hippie');
hippie()
.header("user-agent", "derpbot")
.json().get('https://api.github.com/users/vesln')
.expectStatus(200)
.end(function(err, res, body) {
if (err) throw err;
});

Resources