vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
http://frisbyjs.com
1.53k stars 201 forks source link

unable to get the same response when using fetch method #552

Closed amrsa1 closed 4 years ago

amrsa1 commented 4 years ago

im tryin to test an API that use get method and also contains additional params in request headers (query string params)

image image

    fit('3-  API will return status code 200', ()=> {
    return frisby
        .fetch('https://example.com/sss/ssss/ssss/207', {
            method: 'GET',
            request:{
                headers:
                    qs.stringify({
                        'additionalParams[include][0]': 'Platforms',
                        'additionalParams[include][1]': 'Platforms.PlatformType',
                        'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
                        ' additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
                        ' additionalParams[include][4]': 'Country',
                        'additionalParams[include][5]': 'State'
                    })
                }
        })
        .inspectJSON()

but im getting only this response

image

instead of this response that im getting from network in browser as shown below

image

amrsa1 commented 4 years ago

@vlucas @H1Gdev

H1Gdev commented 4 years ago

@Amrkamel1

Query params are not HTTP header.

Set query params as follows.

const url = 'https://example.com/sss/ssss/ssss/207'
const params = new URLSearchParams({ name: value })

.fetch(url + '?' + params {
method: 'GET',
})
amrsa1 commented 4 years ago

@H1Gdev

i have tried it getting the same response

 fit('6- Verify by adding aaa to last created account API will return status code 200', ()=> {
    const url = 'https://sss.ssss.com/ssss/sssss/companies/207';
    const params = new URLSearchParams({
        'additionalParams[include][0]': 'Platforms',
        'additionalParams[include][1]': 'Platforms.PlatformType',
        'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
        ' additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
        ' additionalParams[include][4]': 'Country',
        'additionalParams[include][5]': 'State' });
    return frisby
        .fetch(url + '?' + qs.stringify(params) , {
             method: 'GET'
        })
        .inspectJSON()

if i removed qs.stringfy from params

i get this response

 JSON: {
    "hasErrors": true,
    "resultList": [
        {
            "message": "\" additionalParams%5Binclude%5D%5B3%5D\" is not allowed",
            "key": [
                " additionalParams%5Binclude%5D%5B3%5D"
            ]
        },
        {
            "message": "\" additionalParams%5Binclude%5D%5B4%5D\" is not allowed",
            "key": [
                " additionalParams%5Binclude%5D%5B4%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B0%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B0%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B1%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B1%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B2%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B2%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B5%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B5%5D"
            ]
        }
    ]
}
H1Gdev commented 4 years ago

I think this can generate same URL.

const url = new URL('https://example.com/sss/ssss/ssss/207');
url.searchParams.append('additionalParams[include][0]', 'Platforms');
url.searchParams.append('additionalParams[include][1]', 'Platforms.PlatformType');
url.searchParams.append('additionalParams[include][2]', 'Platforms.PlatformMobileOperator');
url.searchParams.append('additionalParams[include][3]', 'Platforms.PlatformSatelliteOperator');
url.searchParams.append('additionalParams[include][4]', 'Country');
url.searchParams.append('additionalParams[include][5]', 'State');

console.log(url.href);
/*
https://example.com/sss/ssss/ssss/207?additionalParams%5Binclude%5D%5B0%5D=Platforms&additionalParams%5Binclude%5D%5B1%5D=Platforms.PlatformType&additionalParams%5Binclude%5D%5B2%5D=Platforms.PlatformMobileOperator&additionalParams%5Binclude%5D%5B3%5D=Platforms.PlatformSatelliteOperator&additionalParams%5Binclude%5D%5B4%5D=Country&additionalParams%5Binclude%5D%5B5%5D=State
*/
amrsa1 commented 4 years ago

@H1Gdev

still same however console.log(url.herf) it prints the exact request url that i see in browser network tab i even tried it with get method and still same error

fit('6- Verify by adding aaa to last created account API will return status code 200', ()=> {

        const url = new URL('https://test.com/companies/207');
        url.searchParams.append('additionalParams[include][0]', 'Platforms');
        url.searchParams.append('additionalParams[include][1]', 'Platforms.PlatformType');
        url.searchParams.append('additionalParams[include][2]', 'Platforms.PlatformMobileOperator');
        url.searchParams.append('additionalParams[include][3]', 'Platforms.PlatformSatelliteOperator');
        url.searchParams.append('additionalParams[include][4]', 'Country');
        url.searchParams.append('additionalParams[include][5]', 'State');

        console.log(url.href);

        // const url = 'https://cisiot.test.att.com/mcapi/onboarding/companies/207';
        const params = new URLSearchParams({
            'additionalParams[include][0]': 'Platforms',
            'additionalParams[include][1]': 'Platforms.PlatformType',
            'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
            'additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
            'additionalParams[include][4]': 'Country',
            'additionalParams[include][5]': 'State' });
        return frisby
            .fetch(url.href,{
                method: 'GET'
            })
            .inspectJSON() 
})
H1Gdev commented 4 years ago

Query params look correct. There seems to be another cause of issue.(HTTP header ??)

amrsa1 commented 4 years ago

Query params look correct. There seems to be another cause of issue.(HTTP header ??)

Its really weird, here you are all list of request header

image

H1Gdev commented 4 years ago

To see what value is affecting, first request with the same HTTP header.

amrsa1 commented 4 years ago

@H1Gdev I have tried this on postman and its working fine and im getting fill response according to these additional filters that we are adding,but still curious to know what im missing on frisby it returning only response from the url and ignoring the additional params.

image

it still ignoring the additional params and get the response for the url without aditional params in the code below

    fit('6- Verify by adding aaat last created account in acme API will return status code 200', () => {

        // const url = new URL('https://test.com/api/onboarding/companies/207');
        // url.searchParams.append("additionalParams[include][0]", 'Platforms');
        // url.searchParams.append('additionalParams[include][1]', 'Platforms.PlatformType');
        // url.searchParams.append('additionalParams[include][2]', 'Platforms.PlatformMobileOperator');
        // url.searchParams.append('additionalParams[include][3]', 'Platforms.PlatformSatelliteOperator');
        // url.searchParams.append('additionalParams[include][4]', 'Country');
        // url.searchParams.append('additionalParams[include][5]', 'State');

        // console.log(url.href);
        const url = 'https://test.com/api/onboarding/companies/207';
        const params = new URLSearchParams({
            'additionalParams[include][0]': 'Platforms',
            'additionalParams[include][1]': 'Platforms.PlatformType',
            'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
            'additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
            'additionalParams[include][4]': 'Country',
            'additionalParams[include][5]': 'State'
        });
        console.log(url +'?'+ params);
        return frisby
            .setup({
                request: {
                    headers: {
'company-service': 'default',
                    }
                }
            })
            // .get(url +'?'+params)
            .fetch(url +'?', params,{
                method: 'Get'
            })
            .inspectJSON()
            .expect('status',200)

    })
H1Gdev commented 4 years ago

First, set all values here and test.

https://github.com/vlucas/frisby/issues/552#issuecomment-587445305

By the way, do you know this Web API specifications?

amrsa1 commented 4 years ago

@H1Gdev

i already did that, getting same response , its ignoring the additional params

fit('6- Verify by adding aaaa to last created account in API will return status code 200', () => {

        const url = 'https://test.com/mcapi/onboarding/companies/207';
        const params = new URLSearchParams({
            'additionalParams[include][0]': 'Platforms',
            'additionalParams[include][1]': 'Platforms.PlatformType',
            'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
            'additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
            'additionalParams[include][4]': 'Country',
            'additionalParams[include][5]': 'State'
        });
        console.log(url +'?'+ params);
        return frisby
            .setup({
                request: {
                    redirect: 'manual',
                    headers: {
                        'content-type': 'application/json',
                        'authority': 'ci.test.com',
                        'method': 'GET',
                        'path': '/api/onboarding/companies/207?additionalParams%5Binclude%5D%5B0%5D=Platforms&additionalParams%5Binclude%5D%5B1%5D=Platforms.PlatformType&additionalParams%5Binclude%5D%5B2%5D=Platforms.PlatformMobileOperator&additionalParams%5Binclude%5D%5B3%5D=Platforms.PlatformSatelliteOperator&additionalParams%5Binclude%5D%5B4%5D=Country&additionalParams%5Binclude%5D%5B5%5D=State',
                        'scheme': 'https',
                        'accept': 'application/json, text/plain, */*',
                        'accept-encoding': 'gzip, deflate, br',
                        'accept-language': 'en-US,en;q=0.9,ar;q=0.8,bs;q=0.7',
                        'client-request-id': 'sdfsdf32e',
                       'company-service': 'default',
                       'origin':' https://example.test.com',
                      'referer': 'https://example.com/companies/207',
                      'sec-fetch-dest': 'empty',
                      'sec-fetch-mode': 'cors',             
                      'sec-fetch-site': 'same-site'
                    }
                }
            })
            // .get(url +'?'+params)
            .fetch(url +'?', params,{
                method: 'Get'
            })
            .inspectJSON()
            .expect('status',200)

    })
H1Gdev commented 4 years ago

Please delete authority, method, path and scheme. And add user-agent.

amrsa1 commented 4 years ago

@H1Gdev Nothing still same

            .setup({
                request: {
                    redirect: 'manual',
                    headers: {
                        'content-type': 'application/json',
                        'accept': 'application/json, text/plain, */*',
                        'accept-encoding': 'gzip, deflate, br',
                        'accept-language': 'en-US,en;q=0.9,ar;q=0.8,bs;q=0.7',
                        // 'client-request-id': 'id_jd23j9jj4',
                        'company-service': 'default',
                        'origin': ' https://example.test.att.com',
                        'referer': 'https://example.test.att.com/companies/207',
                        'sec-fetch-dest': 'empty',
                        'sec-fetch-mode': 'cors',
                        'sec-fetch-site': 'same-site',
                        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
                    }
                }
            })
H1Gdev commented 4 years ago

Replace setup values.

    .setup({
      request: {
        redirect: 'manual',
        headers: {
          'content-type': 'application/json',
          'accept': 'application/json, text/plain, */*',
          'accept-encoding': 'gzip, deflate, br',
          'accept-language': 'en-US,en;q=0.9,ar;q=0.8,bs;q=0.7',
          // 'client-request-id': 'id_jd23j9jj4',
          'company-service': 'default',
          'origin': ' https://mc.test.att.com',
          'referer': 'https://mc.test.att.com/companies/207',
          'sec-fetch-dest': 'empty',
          'sec-fetch-mode': 'cors',
          'sec-fetch-site': 'same-site',
          'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
        }
      }
    }, true)
amrsa1 commented 4 years ago

@H1Gdev it send unauthorized since it seems override the global setup that running before each and containing cookies, so i add the cookies in the header

it returns 200,but again send the regular response and ignoring the additional params

H1Gdev commented 4 years ago

I forgot this option...

    .fetch(url.href, {
      method: 'Get',
    }, {
      urlEncode: false,
    })

I think this is issue cause, so I don't think that HTTP header settings are necessary.

amrsa1 commented 4 years ago

YUP, finally!! it works fine now thanks for you effort

just a quick one, when i should add urlEncode to true or false

H1Gdev commented 4 years ago

If build URL using URL as in this case, it is already URL-encoded.

In case it is not necessary, so urlEncode is false.