笑话返回“网络错误"使用axios执行经过身份验证的请求时 [英] Jest returns "Network Error" when doing an authenticated request with axios

查看:596
本文介绍了笑话返回“网络错误"使用axios执行经过身份验证的请求时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说似乎有点奇怪.我正在尝试用Jest测试实际(即真实网络)请求.

This seems a bit weird to me. I'm trying to test an actual (ie. real network) request with Jest.

这些是经过测试的方案:

These are the tested scenarios:

  • 测试没有标题的外部API(fixer.io)< ---可行
  • 使用标头< ---测试本地API服务器
  • 使用来自node终端< ---的标头测试相同的本地API
  • Test an external API (fixer.io) with no headers <--- This works
  • Test a local API server with headers <--- This does NOT work
  • Test same local API with headers from node terminal <--- This works

此行为背后的原因可能是什么?那么解决方案是什么?

What could be the reason behind this behavior? And what is the solution?

//This WORKS
test('testing no headers', () => {
  return axios.get('http://api.fixer.io/latest')
        .then( res => console.log(res) )
});

//This DOES NOT work
test('testing no headers', () => {
  return axios.get('http://localhost:3000/users/4/profile', 
                      {headers:{authorization:`Bearer ${mytoken}`}})
        .then( res => console.log(res) )
});

//...

//Node Terminal
//This WORKS
> axios.get('http://localhost:3000/users/4/profile', 
                   {headers:{authorization:`Bearer ${mytoken}`}})
        .then( res => console.log(res) )

推荐答案

可笑的是,axios通过主要使用XMLHttpRequest,而ajax请求无法跨域访问,因此您的测试失败,因此您可以让代码通过设置axios适配器.

That is funny,that the axios used XMLHttpRequest by primary,and ajax request can't access across domain,so your test failed ,so you can let your code pass by set the axios adapter.

 function getDefaultAdapter() {
    var adapter;
    if (typeof XMLHttpRequest !== 'undefined') {
        // For browsers use XHR adapter
        adapter = require('./adapters/xhr');
    } else if (typeof process !== 'undefined') {
        // For node use HTTP adapter
        adapter = require('./adapters/http');   
    }
    return adapter;
 }

解决方案将axios适配器更改为http

import axios from 'axios';
//This WORKS
test('testing with headers', (done) => {
    var path=require('path');
    var lib=path.join(path.dirname(require.resolve('axios')),'lib/adapters/http');
    var http=require(lib);
    axios.get('http://192.168.1.253', {
        adapter: http,
        headers: {
            Authorization: "Basic YWRtaW46bHVveGlueGlhbjkx"
        }
    }).then((res) => {
        expect(res.status).toBe(200);
        done();
    }).catch(done.fail);
});

解决方案更改package.json中的jest testURL

"jest": {
   "testURL":"http://192.168.1.253"
}

然后可以通过ajax访问http

then the test can be access http via ajax

import axios from 'axios';
    //This WORKS
    test('testing with headers', (done) => {
        axios.get('http://192.168.1.253', {
            headers: {
                Authorization: "Basic YWRtaW46bHVveGlueGlhbjkx"
            }
        }).then((res) => {
            expect(res.status).toBe(200);
            done();
        }).catch(done.fail);
    });

这篇关于笑话返回“网络错误"使用axios执行经过身份验证的请求时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆