Skip to content

Commit f096425

Browse files
committed
make IndexController render a proper HTML page, LH compliant
1 parent 24bf466 commit f096425

File tree

5 files changed

+122
-13
lines changed

5 files changed

+122
-13
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.sergiofreire.xray.tutorials.springboot.boundary;
22

3+
import org.springframework.stereotype.Controller;
34
import org.springframework.web.bind.annotation.GetMapping;
4-
import org.springframework.web.bind.annotation.RestController;
55

6-
@RestController
6+
@Controller
77
public class IndexController {
88

99
@GetMapping("/")
1010
public String index() {
11-
return "Welcome to this amazing website!";
11+
return "index";
1212
}
1313

1414
}
Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
1-
<!DOCTYPE HTML>
2-
<html xmlns:th="http://www.thymeleaf.org" lang="en">
3-
<head>
4-
<title>Getting Started: Serving Web Content</title>
5-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="description" content="A simple Thymeleaf web page example saying hello to a user." />
8+
<meta name="robots" content="index, follow" />
9+
10+
<title>Hello Page</title>
11+
12+
<!-- Preconnect for faster font loading -->
13+
<link rel="preconnect" href="https://fonts.googleapis.com" />
14+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
15+
16+
<!-- Accessible, performant font -->
17+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
18+
19+
<!-- Minimal inline CSS (fast load, high contrast) -->
20+
<style>
21+
body {
22+
font-family: 'Inter', system-ui, sans-serif;
23+
margin: 0;
24+
padding: 2rem;
25+
background-color: #ffffff;
26+
color: #111827;
27+
font-size: 1.125rem;
28+
line-height: 1.6;
29+
}
30+
main {
31+
max-width: 600px;
32+
margin: auto;
33+
text-align: center;
34+
}
35+
h1 {
36+
font-size: 2rem;
37+
color: #1d4ed8;
38+
margin-bottom: 1rem;
39+
}
40+
p {
41+
margin: 0;
42+
}
43+
</style>
644
</head>
45+
746
<body>
8-
<p th:text="|Hello, ${name}!|" />
47+
<main>
48+
<h1>Hello!</h1>
49+
<p th:text="|Hello, ${name}!|">Hello, Guest!</p>
50+
</main>
51+
52+
<!-- Lightweight script placeholder (deferred for performance) -->
53+
<script defer>
54+
// Example: Lighthouse likes scripts to be deferred
55+
console.info("Page loaded successfully.");
56+
</script>
957
</body>
10-
</html>
58+
</html>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="description" content="A simple Thymeleaf web page example saying hello to a user." />
8+
<meta name="robots" content="index, follow" />
9+
10+
<title>Hello Page</title>
11+
12+
<!-- Preconnect for faster font loading -->
13+
<link rel="preconnect" href="https://fonts.googleapis.com" />
14+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
15+
16+
<!-- Accessible, performant font -->
17+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
18+
19+
<!-- Minimal inline CSS (fast load, high contrast) -->
20+
<style>
21+
body {
22+
font-family: 'Inter', system-ui, sans-serif;
23+
margin: 0;
24+
padding: 2rem;
25+
background-color: #ffffff;
26+
color: #111827;
27+
font-size: 1.125rem;
28+
line-height: 1.6;
29+
}
30+
main {
31+
max-width: 600px;
32+
margin: auto;
33+
text-align: center;
34+
}
35+
h1 {
36+
font-size: 2rem;
37+
color: #1d4ed8;
38+
margin-bottom: 1rem;
39+
}
40+
p {
41+
margin: 0;
42+
}
43+
</style>
44+
</head>
45+
46+
<body>
47+
<main>
48+
<h1>Welcome to this amazing website!</h1>
49+
</main>
50+
51+
<!-- Lightweight script placeholder (deferred for performance) -->
52+
<script defer>
53+
// Example: Lighthouse likes scripts to be deferred
54+
console.info("Page loaded successfully.");
55+
</script>
56+
</body>
57+
</html>

src/test/java/com/sergiofreire/xray/tutorials/springboot/IndexControllerIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.boot.test.context.SpringBootTest;
66
import org.springframework.boot.test.web.client.TestRestTemplate;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
79
import org.springframework.http.ResponseEntity;
810
import static org.assertj.core.api.Assertions.assertThat;
911

@@ -18,6 +20,8 @@ class IndexControllerIT {
1820
void getWelcomeMessage() throws Exception {
1921
ResponseEntity<String> response = template.getForEntity("/", String.class);
2022

21-
assertThat(response.getBody()).isEqualTo("Welcome to this amazing website!");
23+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
24+
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_HTML);
25+
assertThat(response.getBody()).contains("Welcome to this amazing website!");
2226
}
2327
}

src/test/java/com/sergiofreire/xray/tutorials/springboot/IndexControllerMockedIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sergiofreire.xray.tutorials.springboot;
22

3-
import static org.hamcrest.Matchers.equalTo;
3+
import static org.hamcrest.Matchers.containsString;
44
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
55
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
66
import org.junit.jupiter.api.Test;
@@ -30,6 +30,6 @@ class IndexControllerMockedIT {
3030
void getWelcomeMessage() throws Exception {
3131
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.TEXT_PLAIN))
3232
.andExpect(status().isOk())
33-
.andExpect(content().string(equalTo("Welcome to this amazing website!")));
33+
.andExpect(content().string(containsString("Welcome to this amazing website!")));
3434
}
3535
}

0 commit comments

Comments
 (0)