Для моего тестирования API мой код ведет себя по-разному. Я использую jasmine framework с узлом js. Мой запрос GET даст 2 ответа.
- Успех json с кодом состояния 200. Время отклика 400-500 мс
- Сбой json, с кодом состояния 200 (действительный сбой), время отклика более 10000 мс
В первом случае код для моих тестовых случаев приведен ниже.
describe('Verification of BS_004_addressCheck',()=>{
it('Verify success response for BS_004_addressCheck',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpoint_BS_004.json'));
// let pincodes=require(path.resolve('./config/pincodes.json'));
//let request=require(path.resolve('./config/postPin.json'));
//console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let apikey=endpoint.apikey;
let country=endpoint.country;
let postcode=endpoint.postcode;
let picklistcount=endpoint.picklistcount;
let uniqueAddressReference=endpoint.uniqueAddressReference;
let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&country=" + country + "&postcode=" + postcode + "&picklistcount=" + picklistcount + "&uniqueAddressReference=" + uniqueAddressReference
//let pinCode=pincodes.Vlcy;
console.log(fullUrlWithQueryParameters);
console.log("test");
getR(fullUrlWithQueryParameters)
.then(jsonRes=>{
console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.Header.ActivityStatusEnum).toBe('SUCCESS');
})
.then(done)
})
})
Код в моем втором случае такой же, как и выше. Но в моем первом случае я получаю спецификацию как пропуск, а во втором случае я получаю следующую ошибку
1) Verification of BS_004_addressCheck Verify success response for BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
1 spec, 1 failure
Finished in 5.012 seconds
Randomized with seed 21049 (jasmine --random=true --seed=21049)
Мой ответ об успехе займет почти 400 мс, но мой ответ об ошибке займет почти 10000 мс. Итак, я догадался и добавил тайм-аут жасмина, как показано ниже (см. блок выше)
describe('Verification of BS_004_addressCheck',()=>{
beforeAll(function(done) {
jasmine.DEFAULT_TIMEOUT_INTERVAL= 10000;
});
//jasmine.DEFAULT_TIMEOUT_INTERVAL= 10000;
it('Verify success response for BS_004_addressCheck',function(done){
var path=require('path');
let endpoint=require(path.resolve('./config/endpoint_BS_004.json'));
// let pincodes=require(path.resolve('./config/pincodes.json'));
//let request=require(path.resolve('./config/postPin.json'));
//console.log(request.id,request.Name);
const fetch=require('node-fetch');
let baseUrl=endpoint.url;
let apikey=endpoint.apikey;
let country=endpoint.country;
let postcode=endpoint.postcode;
let picklistcount=endpoint.picklistcount;
let uniqueAddressReference=endpoint.uniqueAddressReference;
let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&country=" + country + "&postcode=" + postcode + "&picklistcount=" + picklistcount + "&uniqueAddressReference=" + uniqueAddressReference
//let pinCode=pincodes.Vlcy;
console.log(fullUrlWithQueryParameters);
console.log("test");
getR(fullUrlWithQueryParameters)
.then(jsonRes=>{
console.log(jsonRes);
expect(jsonRes).not.toBeUndefined();
expect(jsonRes.Header.ActivityStatusEnum).toBe('SUCCESS');
})
.then(done)
})
})
Но теперь у меня есть 2 ошибки ..
Failures:
1) Verification of BS_004_addressCheck Verify success response for BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Suite error: Verification of BS_004_addressCheck
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at <Jasmine>
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
1 spec, 2 failures
Finished in 15.015 seconds
Randomized with seed 21999 (jasmine --random=true --seed=21999).
Я не знаю, как это исправить. Пожалуйста, помогите мне. Функция getR, которую я вызываю, приведена ниже.
let getR = (url) => {
console.log('test');
//console.log(url);
//console.log(id);
return fetch(url, {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683')})
.then(resRaw =>{
console.log(resRaw);
return resRaw.text();
})
.then(resJson=>{
console.log(resJson);
let res=JSON.parse(resJson);
//return res;
console.log(res.Header.ActivityStatusEnum);
return res;
})
//.then(validateResponse);
};
global.get=get;
global.getR=getR;
Думаю, проблема в длительном времени отклика. Но не знаю, как это исправить. Пожалуйста, порекомендуйте.