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

unexpected response when posting request with content type x-www-form-urlencoded #565

Closed amrsa1 closed 4 years ago

amrsa1 commented 4 years ago

im getting unexpected response when im using post request with content type x-www-form-urlencoded i have tried to post the request with different ways but no luck, url and data are correct since same data is working fine with postman

  beforeAll(()=> {
        let form = frisby.formData();

        form.append('client_id', '1f3g3eff-acc2-4sdsd4');
        form.append('grant_type', 'password');
        form.append('username','test@example.com');
        form.append('password', 'test');
        form.append('client_secret', 'secret');
        form.append('redirect_uri', 'https://example.com/signin-callback');

        return frisby
            .setup({
                request: {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    }
                }
            })
            .post('https://example/token', {
                    body : form 
            })
            .expect('status', 200)
    beforeAll(()=> {
        return frisby
            .setup({
                request: {
                    // redirect: 'manual',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    }
                }
            })
            .post('https://example.com/token', {
                request:{
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body : 
                        qs.stringify ({
                    'client_id': '1f3g3eff-ac',
                    'grant_type': 'password',
                     'username': D.email('myAccount'),
                     'password': D.password('myAccount'),
                     'client_secret': 'secret',
                     'redirect_uri': 'https://example.com/signin-callback'
                         })
                }

            })
            .expect('status', 200)

expected response is 200 but im getting

    FAILURE Status: 400 
    JSON: {
        "error": "invalid_request",
        "error_description": "no client authentication mechanism provided"
    }
amrsa1 commented 4 years ago

@H1Gdev

H1Gdev commented 4 years ago

application/x-www-form-urlencoded sample is the following.

return frisby.post('https://example.com/token', {
  body: qs.stringify({
    'client_id': '1f3g3eff-ac',
    'grant_type': 'password',
    'username': D.email('myAccount'),
    'password': D.password('myAccount'),
    'client_secret': 'secret',
    'redirect_uri': 'https://example.com/signin-callback'
  }),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  }
})
  .expect('status', 200);
amrsa1 commented 4 years ago

application/x-www-form-urlencoded sample is the following.

return frisby.post('https://example.com/token', {
  body: qs.stringify({
    'client_id': '1f3g3eff-ac',
    'grant_type': 'password',
    'username': D.email('myAccount'),
    'password': D.password('myAccount'),
    'client_secret': 'secret',
    'redirect_uri': 'https://example.com/signin-callback'
  }),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  }
})
  .expect('status', 200);

didnt work same response

amrsa1 commented 4 years ago

i got it works like code below

  beforeAll(()=> {

        const myHeaders = new Headers();
        myHeaders.append("Content-Type", "application/x-www-form-urlencoded");

        const urlencoded = new URLSearchParams();
        urlencoded.append('client_id', 'sdsdsdf-b3d5-1c434ae452bb');
        urlencoded.append('grant_type', 'password');
        urlencoded.append("username", "acc@email.com");
        urlencoded.append("password", "test");
        urlencoded.append('client_secret', 'any');

        const requestOptions = {
            method: 'POST',
            headers: myHeaders,
            body: urlencoded,
            redirect: 'follow'
        };

        return frisby
            .fetch("https://example/default/token", requestOptions)
            .expect('status', 200)
vlucas commented 4 years ago

Glad to see you got this working. 👍

H1Gdev commented 4 years ago

After all, is it parameters value problem ?

The following values are different.

amrsa1 commented 4 years ago

After all, is it parameters value problem ?

The following values are different.

  • client_id
  • username
  • password
  • client_secret
  • redirect_uri

No not parameters im changing them randomly here for security purposes

Qs.stringfy didnt help in that case however it working on some other

Only way worked for me is the one i shared above

H1Gdev commented 4 years ago

https://github.com/vlucas/frisby/issues/565#issue-635314848

'client_secret': 'secret',

https://github.com/vlucas/frisby/issues/565#issuecomment-641239204

urlencoded.append('client_secret', 'any');

As mentioned above, you were changing the value of parameters. I thought this is the cause.

Both do URL encoding, so I don't understand why URLSearchParams solves this issue.

However, it is nice to solve this !!

amrsa1 commented 4 years ago

#565 (comment)

'client_secret': 'secret',

#565 (comment)

urlencoded.append('client_secret', 'any');

As mentioned above, you were changing the value of parameters. I thought this is the cause.

Both do URL encoding, so I don't understand why URLSearchParams solves this issue.

However, it is nice to solve this !!

actually you were right, your way is working as well

i just noticed the difference, i wrapped headers and body inside request maybe this was the issue