add more test

This commit is contained in:
nnhai1991@gmail.com 2018-08-11 16:10:53 +08:00
parent e92843493f
commit 4e4d11574a
2 changed files with 40 additions and 16 deletions

View File

@ -1,20 +1,29 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="security"
uri="http://www.springframework.org/security/tags" %>
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<sec:csrfMetaTags />
<title>Home Page</title>
</head>
<body>
<security:authorize access="isAuthenticated()">
<sec:authorize access="isAuthenticated()">
AUTHENTICATED
</security:authorize>
<security:authorize access="hasRole('ADMIN')">
</sec:authorize>
<sec:authorize access="hasRole('ADMIN')">
ADMIN ROLE
</security:authorize>
</sec:authorize>
<h2>principal.username: <sec:authentication property="principal.username" /> </h2>
<form method="post" action="/do/something">
<sec:csrfInput />
Text Field:<br /> <input type="text" name="textField" />
</form>
</body>
</html>

View File

@ -17,11 +17,26 @@ public class HomeControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void home() throws Exception {
String body = this.restTemplate.withBasicAuth("ADMIN", SecurityConfig.DEFAULT_PASSWORD).getForEntity("/", String.class).getBody();
System.out.println(body);
assertTrue(body.contains("AUTHENTICATED"));
assertTrue(body.contains("ADMIN ROLE"));
}
@Test
public void home() throws Exception {
String body = this.restTemplate.withBasicAuth("ADMIN", SecurityConfig.DEFAULT_PASSWORD)
.getForEntity("/", String.class)
.getBody();
System.out.println(body);
// test <sec:authorize access="isAuthenticated()">
assertTrue(body.contains("AUTHENTICATED"));
// test <sec:authorize access="hasRole('ADMIN')">
assertTrue(body.contains("ADMIN ROLE"));
// test <sec:authentication property="principal.username" />
assertTrue(body.contains("principal.username: ADMIN"));
// test <sec:csrfInput />
assertTrue(body.contains("<input type=\"hidden\" name=\"_csrf\" value=\""));
// test <sec:csrfMetaTags />
assertTrue(body.contains("<meta name=\"_csrf_parameter\" content=\"_csrf\" />"));
}
}