简体   繁体   English

使用 Protractor+AXIOS 进行 API 测试

[英]API Testing using Protractor+AXIOS

I am trying to hit an API and to get its response using AXIOS+Java Script+Protractor.我正在尝试使用 AXIOS+Java Script+Protractor 来访问 API 并获得其响应。 I am getting below message in console, can someone please help me?我在控制台中收到以下消息,有人可以帮助我吗?

Code:代码:

const axios = require('axios');
axios.get('jsonplaceholder.typicode.com/todos/1')
    .then(function(response) {
        console.log(response);
    })
    .catch(function(error) {
        console.log(error);
    })
    .finally(function() {
        // always executed 
    });

Please find the below console log:请找到以下控制台日志:

Error: connect ETIMEDOUT 104.27.135.11:443 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', address: '104.27.135.11', port: 443, config: { url: ' https://reqres.in/api/users/2 ', method: 'get', headers: { Accept: 'application/json, text/plain, / ', 'User-Agent': 'axios/0.19.0' }, transformRequest: [ [Function: transformRequest] ], transformResponse: [ [Function: transformResponse] ], timeout: 0, adapter: [Function: httpAdapter], xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: [Function: validateStatus], data: undefined }, request: Writable { _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: false, ended: false, finished: false, destroyed: false, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: true, bufferProcessing: false, onwrite: [Function: bound onwr错误:连接 ETIMEDOUT 104.27.135.11:443 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', address: '104.21.135. ', 端口: 443, 配置: { url: ' https://reqres.in/api/users/2 ', 方法: 'get', headers: { Accept: 'application/json, text/plain, / ', '用户代理':'axios/0.19.0'},transformRequest:[[Function:transformRequest]],transformResponse:[[Function:transformResponse]],超时:0,适配器:[Function:httpAdapter],xsrfCookieName:' XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: [Function: validateStatus], data: undefined }, request: Writable { _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled :假,needDrain:假,结束:假,结束:假,完成:假,销毁:假,decodeStrings:真,defaultEncoding:'utf8',长度:0,写作:假,corked:0,同步:真,bufferProcessing : false, onwrite: [功能:绑定 onwr ite], writecb: null, writelen: 0, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 0, prefinished: false, errorEmitted: false, emitClose: true, bufferedRequestCount: 0, corkedRequestsFree: [Object] }, writable: true, _events: { response: [Function: handleResponse], error: [Function: handleRequestError] }, _eventsCount: 2, _maxListeners: undefined, _options: { protocol: 'https:', maxRedirects: 21, maxBodyLength: 10485760, path: '/api/users/2', method: 'GET', headers: [Object], agent: undefined, auth: undefined, hostname: 'reqres.in', port: null, nativeProtocols: [Object], pathname: '/api/users/2' }, _redirectCount: 0, _redirects: [], _requestBodyLength: 0, _requestBodyBuffers: [], _onNativeResponse: [Function], _currentRequest: ClientRequest { _events: [Object], _eventsCount: 6, _maxListeners: undefined, output: [], outputEncodings: [], outputCallbacks: [], outputSize: 0, writable: true, _last: true, chunkedEncoding: false, shouldKeepAlive: false, useChunkedEncodingByDefault ite], writecb: null, writelen: 0, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 0, prefinished: false, errorEmitted: false, emitClose: true, bufferedRequestCount: 0, corkedRequestsFree: [Object] }, writable: true, _events: { response: [Function: handleResponse], error: [Function: handleRequestError] }, _eventsCount: 2, _maxListeners: undefined, _options: { protocol: 'https:', maxRedirects: 21, maxBodyLength: 10485760, path: '/ api/users/2', method: 'GET', headers: [Object], agent: undefined, auth: undefined, hostname: 'reqres.in', port: null, nativeProtocols: [Object], pathname: '/api /users/2' }, _redirectCount: 0, _redirects: [], _requestBodyLength: 0, _requestBodyBuffers: [], _onNativeResponse: [Function], _currentRequest: ClientRequest { _events: [Object], _eventsCount: 6, _maxListeners: 未定义,输出: [], outputEncodings: [], outputCallbacks: [], outputSize: 0, writable: true, _last: true, chunkedEncoding: false, shouldKeepAlive: false, useChunkedEncodingByDefault : false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, _contentLength: 0, _hasBody: true, _trailer: '', finished: true, _headerSent: true, socket: [TLSSocket], connection: [TLSSocket], _header: 'GET /api/users/2 HTTP/1.1\\r\\nAccept: application/json, text/plain, / \\r\\nUser-Agent: axios/0.19.0\\r\\nHost: reqres.in\\r\\nConnection: close\\r\\n\\r\\n', _onPendingData: [Function: noopPendingOutput], agent: [Agent], socketPath: undefined, timeout: undefined, method: 'GET', path: '/api/users/2', _ended: false, res: null, aborted: undefined, timeoutCb: null, upgradeOrConnect: false, parser: null, maxHeadersCount: null, _redirectable: [Circular], [Symbol(isCorked)]: false, [Symbol(outHeadersKey)]: [Object] }, _currentUrl: ' https://reqres.in/api/users/2 ' }, response: undefined, isAxiosError: true, toJSON: [Function] } : false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, _contentLength: 0, _hasBody: true, _trailer: '',finished: true, _headerSent: true, socket: [TLSSocket], connection: [TLSSocket] ], _header: 'GET /api/users/2 HTTP/1.1\\r\\nAccept: application/json, text/plain, / \\r\\nUser-Agent: axios/0.19.0\\r\\nHost: reqres.in\\ r\\nConnection: close\\r\\n\\r\\n', _onPendingData: [Function: noopPendingOutput], agent: [Agent], socketPath: undefined, timeout: undefined, method: 'GET', path: '/api/users /2', _ended: false, res: null, aborted: undefined, timeoutCb: null, upgradeOrConnect: false, parser: null, maxHeadersCount: null, _redirectable: [Circular], [Symbol(isCorked)]: false, [Symbol( outHeadersKey)]:[对象]},_currentUrl:' https ://reqres.in/api/users/2'},响应:未定义,isAxiosError:true,toJSON:[功能]}

The reason for this ETIMEDOUT error might be some proxy in front of this endpoint.此 ETIMEDOUT 错误的原因可能是此端点前面的某个代理。 If that is the reason, then you should modify your request config by passing the proxy host and port along with the endpoint URL.如果这是原因,那么您应该通过传递代理主机和端口以及端点 URL 来修改您的请求配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM