none
Dynamic Header and Cookie Values in Web Performance Tests RRS feed

  • Question

  • I am trying to set up automated tests of an enterprise SPA in Visual Studio 2012 and I am having difficulties in managing the login process. 

    The login process involves a single request that contains the username and password values. The response to this request is a token that is used to authenticate the user. This token is placed in a special header that we can call DynamicToken. In addition, the same token is stored in a cookie that is also used with the same name. Also stored in the cookie is a field for the authenticated username that we can call DynamicUsername. 

    The result is a request with headers that look like the following

    ...

    Cookie: DynamicToken=abcdef....12345; DynamicUsername=username

    DynamicToken: abcdef....12345

    ...

    The login request executes correctly (200 OK) but the following request results in a 401 Unauthorized response code. I have been able to insert the custom header field named DynamicToken and populated it with the token value returned by the previous response using an extraction rule. What I haven't been able to do yet is dynamically build the Cookie: header field to contain the 2 fields that I need. The username field I can populate manually since I already know the username I am using, but ideally this would by dynamic as well. 

    What is the best way to do this? I have tried using the Promote Dynamic Parameters dialog but it doesn't detect any dynamic parameters (which seems odd since at least surely username and password would be detected as dynamic). 

    Would there be a reason that my browser (Chrome with developer tools open and cache/cookies cleared) automatically detects these cookie fields and the recording plugin in IE/the web test engine misses them? Is it normal that I would have to manually specify these fields, because they will be required in every subsequent request as well. 

    Thanks for any suggestions,

    Luke

    Friday, September 27, 2013 7:16 PM

Answers

  • Like every problem that I post to forums I found the answer immediately after posting the question. There may be more extensible ways of doing this but the quick and dirty way is as follows:

    When you do a extraction rule, you give it a name. You can reference this name as {{ExtractionRuleName}} in a lot of places that it isn't obvious. I got thrown because in my dynamic header field (right click on a request in the .webtest, click add header) the option seemed to be static or a datasource (which could be one of your extracted rule elements). You can instead just type in (for example) Cookie for the name and "CookieField1=abcdefg; CookieField2={{ExtractedField}}" minus the quotes where ExtractedField came from an extraction rule (which in my case was set to ExtractText (from the body of the response). The same thing can be done for the non-cookie related header, and is easier to just type than selecting datasource from the dropdown arrow. One other issue I had while doing this was there was quotes around the returned token that I had to re-use (which was actually the reason it wasn't working in the first place, turned out the cookies must be optional or are only used as a backup) which needed to be stripped. There is probably a good way to do this with regular expressions but the quick and dirty way is to use the "starts with" and "end with" elements on the Extraction Rule>Extract Text dialog and enter " in each, which basically takes them off the beginning and the end of the token. 

    The result is a printf-esque style string that could dynamically combine multiple values obtained from previous queries. A code-based technique would be easier (to me) but this allows for some nifty load and performance testing that VS2012 ultimate has available. Hope this helps someone else down the road. 

    • Marked as answer by Luke Rice Friday, September 27, 2013 9:14 PM
    Friday, September 27, 2013 9:14 PM

All replies

  • Like every problem that I post to forums I found the answer immediately after posting the question. There may be more extensible ways of doing this but the quick and dirty way is as follows:

    When you do a extraction rule, you give it a name. You can reference this name as {{ExtractionRuleName}} in a lot of places that it isn't obvious. I got thrown because in my dynamic header field (right click on a request in the .webtest, click add header) the option seemed to be static or a datasource (which could be one of your extracted rule elements). You can instead just type in (for example) Cookie for the name and "CookieField1=abcdefg; CookieField2={{ExtractedField}}" minus the quotes where ExtractedField came from an extraction rule (which in my case was set to ExtractText (from the body of the response). The same thing can be done for the non-cookie related header, and is easier to just type than selecting datasource from the dropdown arrow. One other issue I had while doing this was there was quotes around the returned token that I had to re-use (which was actually the reason it wasn't working in the first place, turned out the cookies must be optional or are only used as a backup) which needed to be stripped. There is probably a good way to do this with regular expressions but the quick and dirty way is to use the "starts with" and "end with" elements on the Extraction Rule>Extract Text dialog and enter " in each, which basically takes them off the beginning and the end of the token. 

    The result is a printf-esque style string that could dynamically combine multiple values obtained from previous queries. A code-based technique would be easier (to me) but this allows for some nifty load and performance testing that VS2012 ultimate has available. Hope this helps someone else down the road. 

    • Marked as answer by Luke Rice Friday, September 27, 2013 9:14 PM
    Friday, September 27, 2013 9:14 PM
  • Hello,

    Glad to see this issue has been resolved.

    Thanks,


    Amanda Zhu <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 30, 2013 8:17 AM
    Moderator
  • Can you please provide sample code for how you added custom filed to Request Header
    Friday, June 10, 2016 12:41 PM
  • Hi Luke

    You don't give the extraction rule a name you give it the name of the context parameter you want to store the extracted data in.

    Friday, June 10, 2016 12:46 PM