Giter Club home page Giter Club logo

jmeter-post-requestverificationtoken's Introduction

JMeter - POST requests with __RequestVerificationToken

JMeter is a really useful load testing tool.

There is an Azure sample that allows you to automate the process of creating load test infrastructure using Azure DevOps and Azure Container Instances (ACI).

Web applications that implement cross site request forgery countermeasures often create difficulties when recording test scripts as these merely record and playback the same countermeasure tokens, causing the HTTP POST requests to be rejected by the web server.

This repository shows you how you can amend the requests in JMeter to send the correct token on each form POST. It implements the advice in this stack overflow article.

Sample Application

The web page:

<form method="post" enctype="multipart/form-data" asp-controller="Form" asp-action="Index">
    <table>
        <tr>
            <td>First Name: </td>
            <td><input type="text" id="txtFirstName" name="FirstName" /></td>
        </tr>
        <tr>
            <td>Last Name: </td>
            <td><input type="text" id="txtLastName" name="LastName" /></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit" /></td>
        </tr>
    </table>
    <hr />
</form>

When this web page gets generated on the web server, an extra field "__RequestVerificationToken" is added to the HTML form which has been generated using a key that resides in the web app. A form POST using this token is required to provide that the POST was from the same site.

The controller is decorated with ValidateAntiForgeryToken which tells the controller to evaulate whether the value of the extra field is correct and if not throws an exception.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(IFormCollection collection)
{
   try
      {
         ViewBag.Name = string.Format("Name: {0} {1}", collection["FirstName"], collection["LastName"]);
         return View();
      }
      catch
      {
         return View();
      }
}

Load Test Problems

Load tests generally record a series of HTTP requests and play them back. The __RequestVerificationToken will be sent on any later POST requests, but it will fail the validation and so will be rejected by the web server (quite rightly).

Rejected request

So what we need to do is to extract the __RequestVerificationToken from the preceding HTML page response and then apply that token to the subsequent POST request.

The solution

The solution is to add a POST request CSS Selector Extractor to the previous page (the one that generated the HTML form) and then put this value in a variable to then be later used in the next POST request:

CSS Selector Extractor As can be seen above, the selector finds the input field __RequestVerficationToken and then puts its value into a variable, which we have named token

Updated post request The value of token using the expression ${__V(token)} is applied to the POST request.

The result is a "green" response.

Response from post request

Try it yourself

This repository has a sample JMeter test plan (JMX) FormPostTest.jmx that illustrates how to implement this. In addition is the sample application code that can be used to validate this test script.

jmeter-post-requestverificationtoken's People

Contributors

jometzg avatar

Watchers

James Cloos avatar  avatar

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.