In REST Assured, what do the three arguments of FormAuthConfig tell auth().form(...) to do?
answer
- three strings describe the login form
- action plus two input names
- credentials go to form(), not the config
- no config means an extra parse
- springSecurity() is a canned instance
basics
~20 sFormAuthConfig takes the form action, the username input name and the password input name. REST Assured posts your credentials to that action under those two field names. Supplying all three saves it from fetching and parsing the login page.
solid answer
~50 s`new FormAuthConfig(formAction, userNameInputTagName, passwordInputTagName)` describes the login form so REST Assured does not have to discover it. Given a catalogue login page whose form posts to `/catalogue/perform_login` with inputs named `staff_id` and `staff_pin`, you write `auth().form("clerk", "opensesame", new FormAuthConfig("/catalogue/perform_login", "staff_id", "staff_pin"))`. REST Assured then `POST`s `staff_id=clerk&staff_pin=opensesame` as form parameters to that action, on the same scheme, host and port as the request under test, and copies the cookies from the login response onto your real call. Leave the config out, or leave any of the three values blank, and `FormAuthConfig.requiresParsingOfLoginPage()` becomes true: REST Assured makes an extra request and parses the returned HTML to guess all three. `FormAuthConfig.springSecurity()` is a ready-made instance holding the Spring Security defaults `/j_spring_security_check`, `j_username` and `j_password`. Build one config and share it across the suite; every setter on it returns a new instance.
code
java · 23 linesimport io.restassured.authentication.FormAuthConfig;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasItem;
public class CatalogueOverdueLoansTest {
private static final FormAuthConfig CATALOGUE_LOGIN =
new FormAuthConfig("/catalogue/perform_login", "staff_id", "staff_pin");
@Test
void staffSeesOverdueLoans() {
given().
baseUri("http://localhost:8080").
auth().form("clerk", "opensesame", CATALOGUE_LOGIN).
when().
get("/catalogue/loans/overdue").
then().
statusCode(200).
body("title", hasItem("Dune"));
}
}go deeper
Be ready to name the three arguments in order — form action, username input name, password input name — and to point at the matching attributes in a snippet of login-page HTML.
Explain that the credentials leave as form parameters posted to that action on the request's own scheme, host and port, and that the login response's cookies are copied onto your real request.
Show that you verify against the rendered markup: springSecurity() is right only for the Spring Security defaults, and a wrong action produces a login response that quietly establishes no session at all.
Decide whether every suite should re-authenticate through the login form, and where the shared credentials and form details live so one product change does not ripple through hundreds of tests.
## The three arguments, in order `FormAuthConfig` has one public three-argument constructor, and the arguments are all strings describing the **form**, never the user: 1. `formAction` — the path the login form submits to, for example `/catalogue/perform_login`. This is the `action` attribute of the `<form>` element, not the address of the page the form is displayed on. 2. `userNameInputTagName` — the `name` attribute of the input that carries the username, for example `staff_id`. 3. `passwordInputTagName` — the `name` attribute of the password input, for example `staff_pin`. The credentials themselves go to the DSL call: `auth().form("clerk", "opensesame", config)`. A common first mistake is to put the username and password into the config; the config's job is to say *where* they go and *what they are called*, so it can be built once and shared across a suite. ## What REST Assured does with them Form authentication is implemented by an internal filter, not by a header. Once the filter has the three values it builds the login request itself: - It creates a fresh specification with `auth().none()` and CSRF disabled, so the login call is not itself authenticated or token-decorated. - It adds two form parameters — the username input name mapped to your username, the password input name mapped to your password. - It builds the login URL from the **scheme, host and port of the request under test** plus the form action, prefixing a `/` if the action does not already start with one. - It `POST`s that request, then copies `loggedInResponse.cookies()` onto your original request specification and lets the real call proceed. That last step is the whole point: the session cookie the catalogue hands back is what makes `GET /catalogue/loans/overdue` succeed a moment later. ## Why the config exists at all Without it, REST Assured has to work the same three values out for itself. `FormAuthConfig.requiresParsingOfLoginPage()` returns `true` when the action, the username input name or the password input name is blank — which is exactly what the two-argument `auth().form(user, password)` leaves behind. The filter then fetches a login page, parses it with `XmlPath` in HTML compatibility mode, and greps for the first `<form>` element's action plus one input typed `text` and one typed `password`. That costs a round trip on every test and is only as reliable as the markup: | With the three-argument config | With no config | |---|---| | one request: the login `POST` | two requests: fetch the page, then `POST` | | the names you read off the page | names guessed from the HTML | | unaffected by extra forms or inputs | broken by a second text input or an earlier form | ## `springSecurity()` and the other shortcuts `FormAuthConfig` carries a few statics and builders worth knowing: - `FormAuthConfig.springSecurity()` returns a config pre-filled with `/j_spring_security_check`, `j_username` and `j_password` — the classic Spring Security names, and nothing more clever than those three strings. - `FormAuthConfig.formAuthConfig()` (and the no-argument constructor) returns an **empty** config, which is the explicit way of saying "detect everything". - `withAdditionalField("branch")` and `withAdditionalFields("a", "b", ...)` name hidden inputs that the server also wants submitted. REST Assured reads their values out of the login HTML, so using them forces the extra fetch back on even if you supplied all three main values. - `withLoggingEnabled()` and its `LogDetail` / `LogConfig` overloads print the login request and response, which is otherwise invisible because the exchange happens inside a filter. - Every one of these returns a **new** `FormAuthConfig` — the class is immutable, so chain the calls rather than expecting an instance to mutate. ## Reading the values off a real page Given a municipal catalogue login screen like this: ```html <form action="/catalogue/perform_login" method="POST"> <input type="text" name="staff_id"> <input type="password" name="staff_pin"> <input type="submit" value="Sign in"> </form> ``` the config writes itself: `new FormAuthConfig("/catalogue/perform_login", "staff_id", "staff_pin")`. Three checks are worth making every time you build one: - Take the action from the `<form>` tag, not from the browser's address bar. - Take the `name` attributes, not the `id` attributes — the server reads names when the form is submitted. - Confirm the method really is `POST`; the filter always posts, so a form the server expects as a `GET` will not work through this path. ## Where it fits in a suite Because the config is immutable and cheap, the usual shape is one constant shared by every test that needs a signed-in catalogue session, with the credentials themselves coming from configuration rather than the source. If a test does **not** need a session, leave form auth off entirely rather than authenticating and ignoring the result — every `auth().form(...)` costs at least one extra request, and two if the config is incomplete.
- What exactly does FormAuthConfig.springSecurity() return?A `FormAuthConfig` pre-filled with the classic Spring Security defaults: form action `/j_spring_security_check`, username input `j_username` and password input `j_password`. It is a shortcut for those three strings and nothing else, so if the catalogue's login form uses different names you must build the config yourself.
- How do you get REST Assured to submit a hidden field the login form also carries?Chain `withAdditionalField("name")` or `withAdditionalFields(...)` onto the config. REST Assured reads each named input's `value` out of the login HTML and sends it as an extra form parameter — which means the login page must be fetched and parsed, even when you supplied the action and both field names.
saying these in an interview costs you the question
- Thinks the arguments are the username and password themselves
- Passes the login page's URL instead of the form's action
- Uses springSecurity() against a form that is not Spring Security's
- Expects REST Assured to submit every field on the form
- Assumes the config produces an Authorization header, not form parameters
- Treats FormAuthConfig as mutable and drops the chained result