在NodeJS / JEST中运行的Axios具有重复的内容长度

时间:2020-04-12 14:40:17

标签: jestjs axios nanoexpress

我面对Axios / nanoexpress确实很奇怪的问题。我开始编写JEST测试,并从Vuex复制了称为后端的工作代码。该代码可在Vue中可靠地工作,但此处会因网络错误而失败。 SPA和JEST测试中的Axios版本都相同。

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;
}

当我删除带有Authorization标头的配置时,代码运行良好:

let response = await axios.get(`${API}/users/1234`, getAuthHeader()); 

这是错误:

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

该服务没什么花哨的,当存在Authorization标头时就永远不会调用该服务,而在缺少Authorization标头时就不会调用它。

app.get('/v1/users/:userId', async (req, res) => {
    console.log("getUser handler starts");
    const response = {};
    res.send(response);
    return res;
});

我花了几个小时试图查看原始网络通信,但失败了。 Axios一定有问题,因为我可以使用Postman的Authorization标头成功运行完全相同的请求。

更新:

它似乎与NodeJS版本有关。我提交了一个问题:https://github.com/axios/axios/issues/2889

更新2:

我从Axios切换到Got,并且测试正常进行。 https://github.com/literakl/mezinamiridici/commit/e667c5661429fc8cba273716dfced2244e211abf

0 个答案:

没有答案