I prepared and extended this existing spring example which is running fine. You can simply login with "user" and "password" and afterwards you get forwarded to user/index.
Using this controller
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/login-error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "login";
}
}
But as soon i run the example test which is using WebClient the same login is causing the exception:
com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 405 Request method 'POST' not supported for http://localhost:8080/login
which is strange because the application itself works just fine.
EDIT: This is the test method causing the problem
@And("^the user clicks the login button$")
public void theUserClicksTheLoginButton() throws IOException {
page = page.getElementById("login").click();
}
I didn't expect that the click method of WebClient is using POST instead of realy executing the input field in the html.
<form th:action="@{/login}" method="post">
<label for="username">Username</label>:
<input type="text" id="username" name="username" autofocus="autofocus" /> <br />
<label for="password">Password</label>:
<input type="password" id="password" name="password" /> <br />
<input type="submit" id="login" value="Log in" />
</form>
EDIT 2:
Ok maybee i schould claryfy my question a bit.
I know a login should done over POST, and my @Controller
is only providing @GetMapping
but this is ok because spring security is handling the POST requests as i can see in the header while login in:
My question is why is it working fine while running the app, and why hasn't it the same behavior when using WebClient.