Git Product home page Git Product logo

Comments (21)

vlucas avatar vlucas commented on August 29, 2024

Frisby tests cannot be run sequentially, and your test suite should never depend on the order of tests that are run.

If there is setup you need to do for each successive test in your suite, you have a few options. You can use frisby.use(withTokenAuth).get(...).expect(...) in each test you need it in. Your withTokenAuth function might look like this:

// Define your helper function
function withTokenAuth(spec) {
  const token = '123abc';

  return spec.setup({
    request: {
      headers: {
        'Authorization': 'Bearer ' +token,
        'header':'value'
      }
    }
  });
}


it('make call USING GLOBAL SETUP' , function() {
    console.log('########### In IT  call ###########')
   return frisby
    .use(withTokenAuth) // USE the `withTokenAuth` function here in your test (and in every test you need it in)
    .post('url/call', 
    {
      // json body
    })
    .expect('status', 200);
  });

You can also just use the setup method directly in each test as well.

from frisby.

styrus avatar styrus commented on August 29, 2024

Thanks @vlucas .
cant I run my tests sequentially using JEST -i option with it.

What if need to use globalSetup as a funtion in one of the helpers file and the use token from function in my test in different file.

Will it work. Please suggest.

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024
  • It looks like you haven't set token in first it block.
  • -i option is enabled in case of multiple test files in jest.

from frisby.

styrus avatar styrus commented on August 29, 2024

@vlucas Thanks for your response.
with your solution I am trying little variation like..

Now when I call postCall(url,body) passing all required parameters in my spec file I am getting

from frisby.

styrus avatar styrus commented on August 29, 2024

@vlucas can you please provide your input on this.
thanks in advance

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024

HTTP status 415 has wrong Content-Type.
What is Content-Type of request set to ?
Have you changed Content-Type in globalSetup ?

from frisby.

styrus avatar styrus commented on August 29, 2024

@vlucas when I am passing token as the hard coded value and calling postCall (url,body) with .inspectRequestHeaders() it works and shows me correct headers getting passed

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024

Is there issues with getToken() ?

from frisby.

styrus avatar styrus commented on August 29, 2024

@vlucas can you please provide you thought on this.
thanks in advance

from frisby.

styrus avatar styrus commented on August 29, 2024

@H1Gdev if you check my comment above getToken() works fine and returns me token.

from frisby.

vlucas avatar vlucas commented on August 29, 2024

Can you dump the request headers so we can see what is actually being sent? 415 is a response from your server, and we cannot debug that or provide support for it.

Also:

return spec.setup({
    request: {
      headers: {
        'Authorization': 'Bearer ' +token,
        'header':'value' // Did you remember to delete this line? This was just an example...
      }
    }
  });

from frisby.

styrus avatar styrus commented on August 29, 2024

@vlucas thanks for response
For token creation function I am using these set of headers

 'Authorization': 'Basic ' + Buffer.from("username:password").toString('base64'),
 'ouc_header':'admin',
'Content-Type': 'application/scim+json'

token from above I use in below
and for method withTokenAuth() these are header that are required

'Authorization': Bearer  TOKEN,
 'ouc_header':'non_admin',
 'Content-Type': 'application/scim+json'

NOTE:
When I call these method independently it works fine.

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024

When I call these method independently it works fine.

Please paste each code.

from frisby.

styrus avatar styrus commented on August 29, 2024

@H1Gdev @vlucas Please find code snippet I am using.


function getToken(url){
 
 return frisby
       .setup({
           request:{
               headers: 
               {
                'Authorization': 'Basic ' + Buffer.from("username:password").toString('base64'),
				'ouc_header':'admin',
				'Content-Type': 'application/scim+json'
            },
              
           }
         })
		 
       //body : this method works fine
    });   
	});

**============= new file where I am calling getToken() to get my token.**	
function withTokenAuth1(spec) {    
    
`const instance_token= getToken(url);

    return spec.setup({
      request: {
        headers: 
                {
                  'Authorization': 'Bearer '+instance_token,
                  'ouc_header':'non_admin',
				  'Content-Type': 'application/scim+json'
                }
                
      }
    });
  }




   function  postCalltest()(){
     
    return frisby
        .use(withTokenAuth1)
		//body
             })
}

//

now I am calling this postCalltest()() in my spec ...and then getToken() returns me token as console show it.

After that I am getting this.

Request Headers:
        Content-Type: application/scim+json
        User-Agent: frisby/2.1.2 (+https://github.com/vlucas/frisby)
        Authorization: Basic jhkjhkhkhkk=
        ouc_header: admin

//header from getToken method


FAILURE Status: 401
JSON: {
    "response": [          //this is from withTokenAuth1() as i am calling it in postCalltest()()
        "value:Error"
    ],
    "status": 401,
    "scimType": "invalidValue",
    "detail": "Attribute \"ouc_header\" is required."   //not sure why it is not able to set header here for token and ouc_header
}
F*

Failures:
1) Token Suite Add operation
  Message:
    AssertionError: HTTP status 200 !== 415

I am not sure why it is not able to set header in withTokenAuth1() for ouc_header or token

from frisby.

vlucas avatar vlucas commented on August 29, 2024

We cannot debug your server response for you. This is not a free support forum.

I am closing this issue for now.

You can re-open it If you can give us code for both server and client in an isolated and reproducible way that proves the issue with the Frisby library itself, then we can help with a fix. Otherwise, we cannot spend any more time on this issue.

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024

I can't understand this code:confused:
At the very least, make code that completes frisby.
(For example, list tests that pass and fail.)

from frisby.

styrus avatar styrus commented on August 29, 2024

@H1Gdev @vlucas
you can close this this ticket if you want...but could not understand what you can not get of code snippet.

It clearly shows like headers handling is an issue when it getting passed from getToken(url) [this method returns token from body reponse.] to withTokenAuth1().

Now withTokenAuth1() method setups the header to be used for different rest method ie. postCalltest().

Well if you dont want to debug thats okay.
thanks for your time.

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024

@styrus

getToken(url) [this method returns token from body reponse.]

#562 (comment)

getToken(url) returns frisby (that is, Promise).

I don't know how to give advice because the code is incomplete like this...
So if you want to reopen it, write some code that works correctly.

from frisby.

styrus avatar styrus commented on August 29, 2024

@H1Gdev check this code sinppet for getToken(url)

getToken(URL){
   return frisby
    .setup
        ({
            request:
                {
                    headers: 
                        {
                            'Authorization': 'Basic ' + Buffer.from("usrname:pwd").toString('base64'),
                            'ouc_header':'admin',
                            'Content-Type': 'application/scim+json'
                        }
                }
    })
    .get(apiURL)
    .inspectResponse()
    .inspectRequestHeaders()
    .expect('status', 200)
    .then(res => {
        var token=res._token;
        console.log(token);
    });    
} 

is it now clear ???

from frisby.

vlucas avatar vlucas commented on August 29, 2024

We need both the client and server code to help you. We need an independent code repository that we can clone and run that has both the server and frisby tests for it.

One test that shows where it is working.
One test that shows where it is not.

You have all the code and the full context so it might be clear to you, but it is not clear at all to us. We need something isolated and reproducible in order to help you.

from frisby.

H1Gdev avatar H1Gdev commented on August 29, 2024

It's no good returning Promise.
If you use async and await like below, you will get the expected behavior.

async function getToken(URL) {
   return frisby
    .setup
        ({
            request:
                {
                    headers: 
                        {
                            'Authorization': 'Basic ' + Buffer.from("usrname:pwd").toString('base64'),
                            'ouc_header':'admin',
                            'Content-Type': 'application/scim+json'
                        }
                }
    })
    .get(apiURL)
    .inspectResponse()
    .inspectRequestHeaders()
    .expect('status', 200)
    .then(res => {
        var token=res._token; // Can you really get it with this ???
        console.log(token);
        return token;
    });
}
const instance_token= await getToken(url);

But first, it's better to understand Promise of JavaScript.

from frisby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.