我试图从代理后面的REST api获得响应。
使用浏览器(chrome,IE)手动测试时,我可以在浏览器中配置代理时从GET请求中获取结果。
但是,当我试图在nodejs中使用npm包request-promise(在protractor + jasmine测试中)时,我会遇到错误: 错误:无法建立隧道套接字:statusCode407
这是我的测试代码(试过两种方法):
'use strict';
describe('test suite', function() {
it('REST test 1', function(){
var request = require('request-promise');
var url = 'https://environment/getId';
var fullProxy = 'http://myProxy:8080';
var proxiedRequest = request.defaults({'proxy': fullProxy});
proxiedRequest.get(url).then(function(response){
console.log('Response: ' + response);
}).catch(function(error){
throw new Error(error);
});
});
it('REST test 2', function(){
var request = require('request-promise');
var url = 'https://environment/getId';
var fullProxy = 'http://myProxy:8080';
var options = {
uri: url,
proxy: fullProxy
};
request(options)
.then(function(response){
console.log('Response: ' + response);
})
.catch(function(err){
throw new Error(err);
});
});
});
感谢您的时间和精力!
答案 0 :(得分:0)
import rp from 'request-promise';
const url = `https://environment/getId`;
const proxiedRequest = rp.defaults({ proxy: 'http://USER:PASSWORD@myProxy:8080' });
this.response = await proxiedRequest.get(url);