Forum Discussion

Tim_OShea's avatar
Tim_OShea
Icon for Neophyte rankNeophyte
10 months ago
Solved

Multi Step website test with variable

We have a website test on a site that uses session based tokens, whcih are passed by the authentication process.

I can curl the website and get a token, and then paste the token as a bearer token in another curl, but can’t figure out:-

  1. how to do this with a website test
    or
  2. how to build a new datasource to do this.

The token expires after 15 minutes, so setting up as a persistant value in the headers for the webtest doesnt work.

Can anyone help?

13 Replies

  • hi all, still struggling with this. I submitted a call with LM support, but reached the point where i was told it is a custom script so they couldn’t help further.

    This is why I don’t pay for premier support. The stuff I need help with is the stuff they run away from like chicken pox.

    Mind sharing your code (redact any private data).

  • Many thanks.
    I have set up the following test using static headers, then converted to scripts in LM. With headers set as static, I can run Step 1, copy the token it generates, paste into the Authorization header in Step 2, and it works, but as the token is only valid for 15 mins, that isnt a workable solution ;)


    Step 1 Request Script...
     

    import static com.logicmonitor.service.groovyapi.PostDataType.*;
    import static com.logicmonitor.service.groovyapi.StatusCode.*;
    import com.logicmonitor.service.groovyapi.StatusCode;
    import com.logicmonitor.service.groovyapi.AuthType;
    import com.logicmonitor.service.groovyapi.LMRequest;

    LMRequest request = new LMRequest();
    request.useHttp1_1()
             .followRedirect(true)
             .needFullpageLoad(false)
             .addHeader('''sbsbrand''', '''<CUSTOMER>''')
             .addHeader('''sbsreference''', '''<REFERENCE>''')
             .addHeader('''sbslanguage''', '''EN''')
             .addHeader('''sbschannel''', '''broker''')
             .addHeader('''Content-Type''', new String('''YXBwbGljYXRpb24vanNvbg=='''.decodeBase64())) 
             .post(JSON, new String('''//<Encrypted post data>//'''.decodeBase64()));

    return LMHttpClient.request(request);
     

    ---

    With a response script of

    import static com.logicmonitor.service.groovyapi.StatusCode.*;
    import com.logicmonitor.service.groovyapi.StatusCode;


    StatusCode status = STATUS_OK;

    String body = LMResponse.getBody()

    import groovy.json.JsonSlurper
    def jsonSlurper = new JsonSlurper()
    def object = jsonSlurper.parseText(body)

    LMResponse.setContext("token", object.token);


    int[] codes = [200];
    status = STATUS_MISMATCH;
    for (int code : codes) {
        status = LMResponse.statusMatch(code);
        if (status.isOK()) {
            break;
        }
    }
    if (!status.isOK()) {
        return status;
    }


    return status;


    This generates output such as

    Response Content:
    {"exists":false,"token":"<token - 367 characters ! >"}

    it is this token I need to pass to the second step and set with header set to ‘Authorization: Bearer <token>’
            


    Step 2 Request script (a GET with headers set)

    is
     

    import static com.logicmonitor.service.groovyapi.PostDataType.*;
    import static com.logicmonitor.service.groovyapi.StatusCode.*;
    import com.logicmonitor.service.groovyapi.StatusCode;
    import com.logicmonitor.service.groovyapi.AuthType;
    import com.logicmonitor.service.groovyapi.LMRequest;

    LMRequest request = new LMRequest();
    request.useHttp1_1()
             .followRedirect(true)
             .needFullpageLoad(false)
             .addHeader('''Authorization''', 'Bearer ' + token)
             .addHeader('''sbsbrand''', '''<CUSTOMER>''')
             .addHeader('''sbsreference''', '''<reference>''')
             .addHeader('''sbslanguage''', '''EN''')
             .addHeader('''sbschannel''', '''broker''')
             .addHeader('''Content-Type''', new String('''YXBwbGljYXRpb24vanNvbg=='''.decodeBase64()))
             .get();

    return LMHttpClient.request(request);
     


  • So this line: 

    LMResponse.setContext("token", object.token);

    Saved the token value for use in the next step, though i’m not sure if it’s a method of the LMResponse object. I thought you could just do `setContext(“token”,object.token);` 

    Anyway, you’d need a `token = getContext(“token”)` in your step 2 request script to get the token into the script, then yes, you can set that token as your Auth header.

    Also, the content-type header can probably be as simple as:

    .addHeader("Content-Type", "application/json")

    Putting it in encoded threw me off.