-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotFoundTest.java
149 lines (127 loc) · 6.74 KB
/
NotFoundTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.example.demo;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ProblemDetail;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.resource.ResourceWebHandler;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
class NotFoundTest {
//******************************************************************************************************************
//** WebMVC ********************************************************************************************************
//******************************************************************************************************************
@Nested
class WebMvcTests {
@Nested
@TestPropertySource(
properties = {
"spring.main.web-application-type=servlet",
"spring.mvc.problemdetails.enabled=true"
}
)
class DefaultConfig extends Tests {
}
@Nested
@TestPropertySource(
properties = {
"spring.mvc.static-path-pattern=/static/**"
}
)
class WithoutResourceHandlerOnRoot extends DefaultConfig {
}
}
//******************************************************************************************************************
//** Webflux *******************************************************************************************************
//******************************************************************************************************************
@Nested
class WebfluxTests {
@Nested
@TestPropertySource(
properties = {
"spring.main.web-application-type=reactive",
"spring.webflux.problemdetails.enabled=true",
}
)
class DefaultConfig extends Tests {
}
@Nested
@TestPropertySource(
properties = {
"spring.webflux.static-path-pattern=/static/**"
}
)
class WithoutResourceHandlerOnRoot extends DefaultConfig {
}
}
//******************************************************************************************************************
//** Tests *********************************************************************************************************
//******************************************************************************************************************
@SpringBootTest(
classes = TestConfig.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
static abstract class Tests {
@LocalServerPort
int port;
WebTestClient webTestClient;
@BeforeEach
void initWebClient() {
webTestClient = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + port)
.responseTimeout(Duration.ofMinutes(10)) // To be able to do debugging
.build();
}
/**
* This test fails for both WebMVC and WebFlux when using the standard configuration, meaning the static
* resource handler is configured to handle the {@code /**} path pattern.
*
* Reason:
* - For WebMVC the {@link ResourceHttpRequestHandler} handles this request because it is mapped on {@code /**}.
* It will not find any static resources for the non existing path, so it will then call the {@link HttpServletResponse#sendError(int)} with a 404 status.
* This bypasses the {@link ResponseEntityExceptionHandler} to handle the 404.
*
* If we change the static resource path such that no handler is found for this request, and {@code spring.mvc.throw-exception-if-no-handler-found} is set not to {@code false},
* then this will result in a {@link NoHandlerFoundException} that will be handled by the {@link ResponseEntityExceptionHandler}.
*
* - For Webflux the {@link ResourceWebHandler} does return a Mono with a {@link ResponseStatusException} error,
* but it doesn't get handled by the {@link ResponseEntityExceptionHandler}, but instead will be handled by the {@link DefaultErrorWebExceptionHandler}.
*
* If we change the static resource path such that no handler is found for this request,
* then this will also result in a {@link ResponseStatusException}, but at an earlier stage where it will be handled by the {@link DispatcherHandler#handleDispatchError} method.
* This then ensures it does get handled by the {@link ResponseEntityExceptionHandler}.
*/
@Test
void should_return_404_problem_details_on_non_existing_path() {
webTestClient.get()
.uri("/non-existing")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isNotFound()
.expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_PROBLEM_JSON)
.expectBody(ProblemDetail.class).consumeWith(actual -> {
assertThat(actual.getResponseBody().getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
});
}
}
//******************************************************************************************************************
//** Configuration *************************************************************************************************
//******************************************************************************************************************
@Configuration
@EnableAutoConfiguration
static class TestConfig {}
}