0

For my API testing, my code behaving in different ways.I am using jasmine framework with node js.

  1. In my first code , I m using a sample API(REST GET request)
  2. In my second code, I m using my application API(REST GET request)

1.The code in my test case is

describe('PIN Data Verification',()=>{

    it('Verify whether 600042 retruns Velacheri PIN data',function(done){
        var path=require('path');
        let endpoint=require(path.resolve('./config/endpointp.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 pinCode=pincodes.Vlcy;
        get(baseUrl,pinCode)
        .then(jsonRes=>{
            //console.log(jsonRes);
            expect(jsonRes).not.toBeUndefined();
            expect(jsonRes.PostOffice[0].Name).toBe('Velacheri');
        })
        .then(done)

    })


})
  1. The code in my second test case is

    describe('Verification of BS_001_retrieveListDN',()=>{

        it('Verify success response for BS_001_retrieveListDN',function(done){
            var path=require('path');
            let endpoint=require(path.resolve('./config/endpoint.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 userID=endpoint.userID;
            let quantity=endpoint.quantity;
            let operatorId=endpoint.operatorId;
            let orderNumber=endpoint.orderNumber;
            let numberCategory=endpoint.numberCategory;
        let fullUrlWithQueryParameters= baseUrl + "?apikey=" + apikey + "&userID=" + userID + "&quantity=" + quantity + "&operatorId=" + operatorId + "&orderNumber=" + orderNumber + "&numberCategory=" + numberCategory
            //let pinCode=pincodes.Vlcy;
            console.log(fullUrlWithQueryParameters);
            console.log("test");
            getR(fullUrlWithQueryParameters)
            .then(jsonRes=>{
                //console.log(jsonRes);
                expect(jsonRes).not.toBeUndefined();
                expect(jsonRes.PostOffice[0].Name).toBe('Velacheri');
            })
            .then(done)
    
        })
    
    })
    

for both 1 and 2 the common get and getR function i uses is below

const fetch=require('node-fetch');
//var testfile=require('./test');
let url,id;

const HttpProxyAgent = require('http-proxy-agent');
const HttpsProxyAgent = require('https-proxy-agent');

let validateResponse = function(response) {
    //console.log(response);
    return response.json()
        .catch(() => {
            if (response.status !== 200) {
                throw ({
                    status: response.status,
                    statusText: response.statusText
                });
            }
            else {
                return undefined;
            }
        })
        .then(function(json) {console.log('hi');
            console.log(json);
            console.log('hi2');
            if (response.status !== 200) {
                console.log('hi');
                throw ({
                    status: response.status,
                    statusText: response.statusText,
                    body: json
                });
            }
            else {
                return json;
            }
        });
};

let get = (url, id) => {
    console.log('test');
    console.log(url);
    console.log(id);
    return fetch(url +'/'+ id, {method: 'GET', agent: new HttpProxyAgent('http://10.10.104.4:50683')})
        .then(validateResponse);
};

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.ProviderInfo[0].valueType);
        return res;

    })


};
global.get=get;
global.getR=getR;

When I run the first test case, I get success, but when I run the second test case I get the below error

(node:32740) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at getR.then.jsonRes (F:\johny\node_from_home\spec\BS_001_retrieveListDN.spec.js:32:38)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:32740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:32740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
F

Failures:
1) Verification of BS_001_retrieveListDN Verify success response for BS_001_retrieveListDN
  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)
Pending:
johny
  • 51
  • 1
  • 11
  • 1
    It looks like the `json` object does not have a `PostOffice` property. For you to check why that is. – trincot Jul 30 '18 at 15:41
  • In both test cases you do not handle the case when the Promise is rejected. And the error is reported because the Promise returned by `getR(baseUrl,pinCode)` is rejected. – t.niese Jul 30 '18 at 15:43
  • @t.niese, I don't think it is that promise, but the promise returned by the `then` method on it. Although, you could be right if `Header.ProviderInfo` does not exist as a property in one of the two tests, but that seems unlikely to me. – trincot Jul 30 '18 at 15:45
  • @trincot yes that's true. Both lines looked identical so I mixed them up. The last Promise that is created as result of the chaining in the test case is rejected and that rejection is not handled. It is not really important where the rejection happed, it's important that the test case does not handle that at the end of the chain. – t.niese Jul 30 '18 at 15:46
  • thank you. Its my mistake. I dont have PostOffice property in my second case.Sorry for trouble as I am a beginner to javascript. I have a doubt like.consider the function getR.. Is the value for resRaw will be the value which we get in "fetch(url, {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683')})"???/ Is the value for resJson will be the value which we get in "return resRaw.text();"???Is the value for jsonRes (in my test case js) will be the value whicb we get in "return res;"??? – johny Jul 30 '18 at 16:36

0 Answers0