How to test CSRF protected sites using gatling?

Viewed 672

I am testing a django powered site using gatling. My forms are protected by a CSRF token:

<input type="hidden" name="csrfmiddlewaretoken" value="EqFQPv1fdfJjAfq4wFcWkVsecmWSisQzQU0ee1utyOEpJd7edxk3DMhAQNMpI2DK">

How can I test my forms using Gatling testing framework?

2 Answers

Use a css selector check to capture it and send it back in the next request. You probably should also have a look at the official tutorials: part1 and part2.

An alternative to CSS selector test would be to use regex to capture the token:

val scn: ScenarioBuilder = scenario("HisUpdateHealthProfile")
    .exec(http("request_-1")
      .get("/")
      .check(
        regex(
          """<input type="hidden" name="csrfmiddlewaretoken" value="([^"]*)">"""
        ).saveAs("csrf"))
      .headers(headers_0))
Related