mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Polish
This commit is contained in:
parent
3e50836b1a
commit
29f7a596fe
@ -51,9 +51,8 @@ public class HttpExchangesAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
HttpExchangesFilter httpExchangesFilter(HttpExchangeRepository repository,
|
||||
HttpExchangesProperties traceProperties) {
|
||||
return new HttpExchangesFilter(repository, traceProperties.getInclude());
|
||||
HttpExchangesFilter httpExchangesFilter(HttpExchangeRepository repository, HttpExchangesProperties properties) {
|
||||
return new HttpExchangesFilter(repository, properties.getInclude());
|
||||
}
|
||||
|
||||
}
|
||||
@ -65,8 +64,8 @@ public class HttpExchangesAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
HttpExchangesWebFilter httpExchangesWebFilter(HttpExchangeRepository repository,
|
||||
HttpExchangesProperties traceProperties) {
|
||||
return new HttpExchangesWebFilter(repository, traceProperties.getInclude());
|
||||
HttpExchangesProperties properties) {
|
||||
return new HttpExchangesWebFilter(repository, properties.getInclude());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import org.springframework.boot.actuate.web.exchanges.Include;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for HTTP tracing.
|
||||
* Configuration properties for recording HTTP exchanges.
|
||||
*
|
||||
* @author Wallace Wadge
|
||||
* @author Phillip Webb
|
||||
@ -36,8 +36,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
public class HttpExchangesProperties {
|
||||
|
||||
/**
|
||||
* Items to be included in the trace. Defaults to request headers (excluding
|
||||
* Authorization and Cookie), response headers (excluding Set-Cookie), and time taken.
|
||||
* Items to be included in the exchange recording. Defaults to request headers
|
||||
* (excluding Authorization and Cookie), response headers (excluding Set-Cookie), and
|
||||
* time taken.
|
||||
*/
|
||||
private Set<Include> include = new HashSet<>(Include.defaultIncludes());
|
||||
|
||||
|
@ -104,7 +104,7 @@ class HttpExchangesAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(HttpExchange trace) {
|
||||
public void add(HttpExchange exchange) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class HttpExchangesEndpoint {
|
||||
|
||||
/**
|
||||
* Create a new {@link HttpExchangesEndpoint} instance.
|
||||
* @param repository the trace repository
|
||||
* @param repository the exchange repository
|
||||
*/
|
||||
public HttpExchangesEndpoint(HttpExchangeRepository repository) {
|
||||
Assert.notNull(repository, "Repository must not be null");
|
||||
|
@ -37,7 +37,7 @@ public class InMemoryHttpExchangeRepository implements HttpExchangeRepository {
|
||||
private final List<HttpExchange> httpExchanges = new LinkedList<>();
|
||||
|
||||
/**
|
||||
* Flag to say that the repository lists traces in reverse order.
|
||||
* Flag to say that the repository lists exchanges in reverse order.
|
||||
* @param reverse flag value (default true)
|
||||
*/
|
||||
public void setReverse(boolean reverse) {
|
||||
@ -64,16 +64,16 @@ public class InMemoryHttpExchangeRepository implements HttpExchangeRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(HttpExchange trace) {
|
||||
public void add(HttpExchange exchange) {
|
||||
synchronized (this.httpExchanges) {
|
||||
while (this.httpExchanges.size() >= this.capacity) {
|
||||
this.httpExchanges.remove(this.reverse ? this.capacity - 1 : 0);
|
||||
}
|
||||
if (this.reverse) {
|
||||
this.httpExchanges.add(0, trace);
|
||||
this.httpExchanges.add(0, exchange);
|
||||
}
|
||||
else {
|
||||
this.httpExchanges.add(trace);
|
||||
this.httpExchanges.add(exchange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,8 +44,8 @@ class HttpExchangesEndpointTests {
|
||||
repository.add(HttpExchange.start(createRequest("GET")).finish(createResponse(), NO_PRINCIPAL, NO_SESSION_ID));
|
||||
List<HttpExchange> httpExchanges = new HttpExchangesEndpoint(repository).httpExchanges().getExchanges();
|
||||
assertThat(httpExchanges).hasSize(1);
|
||||
HttpExchange trace = httpExchanges.get(0);
|
||||
assertThat(trace.getRequest().getMethod()).isEqualTo("GET");
|
||||
HttpExchange exchange = httpExchanges.get(0);
|
||||
assertThat(exchange.getRequest().getMethod()).isEqualTo("GET");
|
||||
}
|
||||
|
||||
private SourceHttpRequest createRequest(String method) {
|
||||
|
@ -46,10 +46,10 @@ class InMemoryHttpExchangeRepositoryTests {
|
||||
this.repository.add(createHttpExchange("GET"));
|
||||
this.repository.add(createHttpExchange("POST"));
|
||||
this.repository.add(createHttpExchange("DELETE"));
|
||||
List<HttpExchange> traces = this.repository.findAll();
|
||||
assertThat(traces).hasSize(2);
|
||||
assertThat(traces.get(0).getRequest().getMethod()).isEqualTo("DELETE");
|
||||
assertThat(traces.get(1).getRequest().getMethod()).isEqualTo("POST");
|
||||
List<HttpExchange> exchanges = this.repository.findAll();
|
||||
assertThat(exchanges).hasSize(2);
|
||||
assertThat(exchanges.get(0).getRequest().getMethod()).isEqualTo("DELETE");
|
||||
assertThat(exchanges.get(1).getRequest().getMethod()).isEqualTo("POST");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -59,10 +59,10 @@ class InMemoryHttpExchangeRepositoryTests {
|
||||
this.repository.add(createHttpExchange("GET"));
|
||||
this.repository.add(createHttpExchange("POST"));
|
||||
this.repository.add(createHttpExchange("DELETE"));
|
||||
List<HttpExchange> traces = this.repository.findAll();
|
||||
assertThat(traces).hasSize(2);
|
||||
assertThat(traces.get(0).getRequest().getMethod()).isEqualTo("POST");
|
||||
assertThat(traces.get(1).getRequest().getMethod()).isEqualTo("DELETE");
|
||||
List<HttpExchange> exchanges = this.repository.findAll();
|
||||
assertThat(exchanges).hasSize(2);
|
||||
assertThat(exchanges.get(0).getRequest().getMethod()).isEqualTo("POST");
|
||||
assertThat(exchanges.get(1).getRequest().getMethod()).isEqualTo("DELETE");
|
||||
}
|
||||
|
||||
private HttpExchange createHttpExchange(String method) {
|
||||
|
@ -52,7 +52,7 @@ class HttpExchangesWebFilterIntegrationTests {
|
||||
.withUserConfiguration(Config.class);
|
||||
|
||||
@Test
|
||||
void traceForNotFoundResponseHas404Status() {
|
||||
void exchangeForNotFoundResponseHas404Status() {
|
||||
this.contextRunner.run((context) -> {
|
||||
WebTestClient.bindToApplicationContext(context).build().get().uri("/").exchange().expectStatus()
|
||||
.isNotFound();
|
||||
@ -63,7 +63,7 @@ class HttpExchangesWebFilterIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void traceForMonoErrorWithRuntimeExceptionHas500Status() {
|
||||
void exchangeForMonoErrorWithRuntimeExceptionHas500Status() {
|
||||
this.contextRunner.run((context) -> {
|
||||
WebTestClient.bindToApplicationContext(context).build().get().uri("/mono-error").exchange().expectStatus()
|
||||
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
@ -74,7 +74,7 @@ class HttpExchangesWebFilterIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void traceForThrownRuntimeExceptionHas500Status() {
|
||||
void exchangeForThrownRuntimeExceptionHas500Status() {
|
||||
this.contextRunner.run((context) -> {
|
||||
WebTestClient.bindToApplicationContext(context).build().get().uri("/thrown").exchange().expectStatus()
|
||||
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
@ -49,14 +49,14 @@ class HttpExchangesWebFilterTests {
|
||||
EnumSet.allOf(Include.class));
|
||||
|
||||
@Test
|
||||
void filterTracesExchange() {
|
||||
void filterRecordsExchange() {
|
||||
executeFilter(MockServerWebExchange.from(MockServerHttpRequest.get("https://api.example.com")),
|
||||
(exchange) -> Mono.empty());
|
||||
assertThat(this.repository.findAll()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterCapturesSessionIdWhenSessionIsUsed() {
|
||||
void filterRecordsSessionIdWhenSessionIsUsed() {
|
||||
executeFilter(MockServerWebExchange.from(MockServerHttpRequest.get("https://api.example.com")),
|
||||
(exchange) -> exchange.getSession().doOnNext((session) -> session.getAttributes().put("a", "alpha"))
|
||||
.then());
|
||||
@ -67,7 +67,7 @@ class HttpExchangesWebFilterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterDoesNotCaptureIdOfUnusedSession() {
|
||||
void filterDoesNotRecordIdOfUnusedSession() {
|
||||
executeFilter(MockServerWebExchange.from(MockServerHttpRequest.get("https://api.example.com")),
|
||||
(exchange) -> exchange.getSession().then());
|
||||
assertThat(this.repository.findAll()).hasSize(1);
|
||||
@ -76,7 +76,7 @@ class HttpExchangesWebFilterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterCapturesPrincipal() {
|
||||
void filterRecordsPrincipal() {
|
||||
Principal principal = mock(Principal.class);
|
||||
given(principal.getName()).willReturn("alice");
|
||||
executeFilter(new ServerWebExchangeDecorator(
|
||||
@ -90,10 +90,10 @@ class HttpExchangesWebFilterTests {
|
||||
|
||||
}, (exchange) -> exchange.getSession().doOnNext((session) -> session.getAttributes().put("a", "alpha")).then());
|
||||
assertThat(this.repository.findAll()).hasSize(1);
|
||||
org.springframework.boot.actuate.web.exchanges.HttpExchange.Principal tracedPrincipal = this.repository
|
||||
org.springframework.boot.actuate.web.exchanges.HttpExchange.Principal recordedPrincipal = this.repository
|
||||
.findAll().get(0).getPrincipal();
|
||||
assertThat(tracedPrincipal).isNotNull();
|
||||
assertThat(tracedPrincipal.getName()).isEqualTo("alice");
|
||||
assertThat(recordedPrincipal).isNotNull();
|
||||
assertThat(recordedPrincipal.getName()).isEqualTo("alice");
|
||||
}
|
||||
|
||||
private void executeFilter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
|
@ -54,16 +54,16 @@ class SourceServerHttpRequestTests {
|
||||
|
||||
@Test
|
||||
void getMethod() {
|
||||
SourceServerHttpRequest traceableRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(traceableRequest.getMethod()).isEqualTo("GET");
|
||||
SourceServerHttpRequest sourceRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(sourceRequest.getMethod()).isEqualTo("GET");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUri() {
|
||||
URI uri = URI.create("http://localhost:8080/");
|
||||
given(this.request.getURI()).willReturn(uri);
|
||||
SourceServerHttpRequest traceableRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(traceableRequest.getUri()).isSameAs(uri);
|
||||
SourceServerHttpRequest sourceRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(sourceRequest.getUri()).isSameAs(uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -71,24 +71,24 @@ class SourceServerHttpRequestTests {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add("name", "value");
|
||||
given(this.request.getHeaders()).willReturn(httpHeaders);
|
||||
SourceServerHttpRequest traceableRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(traceableRequest.getHeaders()).containsOnly(entry("name", Collections.singletonList("value")));
|
||||
SourceServerHttpRequest sourceRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(sourceRequest.getHeaders()).containsOnly(entry("name", Collections.singletonList("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUnresolvedRemoteAddress() {
|
||||
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("unresolved.example.com", 8080);
|
||||
given(this.request.getRemoteAddress()).willReturn(socketAddress);
|
||||
SourceServerHttpRequest traceableRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(traceableRequest.getRemoteAddress()).isNull();
|
||||
SourceServerHttpRequest sourceRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(sourceRequest.getRemoteAddress()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRemoteAddress() {
|
||||
InetSocketAddress socketAddress = new InetSocketAddress(0);
|
||||
given(this.request.getRemoteAddress()).willReturn(socketAddress);
|
||||
SourceServerHttpRequest traceableRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(traceableRequest.getRemoteAddress()).isEqualTo(socketAddress.getAddress().toString());
|
||||
SourceServerHttpRequest sourceRequest = new SourceServerHttpRequest(this.request);
|
||||
assertThat(sourceRequest.getRemoteAddress()).isEqualTo(socketAddress.getAddress().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -56,13 +56,13 @@ class HttpExchangesFilterTests {
|
||||
private final HttpExchangesFilter filter = new HttpExchangesFilter(this.repository, EnumSet.allOf(Include.class));
|
||||
|
||||
@Test
|
||||
void filterTracesExchange() throws ServletException, IOException {
|
||||
void filterRecordsExchange() throws ServletException, IOException {
|
||||
this.filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
|
||||
assertThat(this.repository.findAll()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterCapturesSessionId() throws ServletException, IOException {
|
||||
void filterRecordsSessionId() throws ServletException, IOException {
|
||||
this.filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(),
|
||||
new MockFilterChain(new HttpServlet() {
|
||||
|
||||
@ -80,17 +80,17 @@ class HttpExchangesFilterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterCapturesPrincipal() throws ServletException, IOException {
|
||||
void filterRecordsPrincipal() throws ServletException, IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
Principal principal = mock(Principal.class);
|
||||
given(principal.getName()).willReturn("alice");
|
||||
request.setUserPrincipal(principal);
|
||||
this.filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
|
||||
assertThat(this.repository.findAll()).hasSize(1);
|
||||
org.springframework.boot.actuate.web.exchanges.HttpExchange.Principal tracedPrincipal = this.repository
|
||||
org.springframework.boot.actuate.web.exchanges.HttpExchange.Principal recordedPrincipal = this.repository
|
||||
.findAll().get(0).getPrincipal();
|
||||
assertThat(tracedPrincipal).isNotNull();
|
||||
assertThat(tracedPrincipal.getName()).isEqualTo("alice");
|
||||
assertThat(recordedPrincipal).isNotNull();
|
||||
assertThat(recordedPrincipal.getName()).isEqualTo("alice");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -61,8 +61,8 @@ class ServletSourceHttpRequestTests {
|
||||
}
|
||||
|
||||
private void validate(String expectedUri) {
|
||||
ServletSourceHttpRequest trace = new ServletSourceHttpRequest(this.request);
|
||||
assertThat(trace.getUri().toString()).isEqualTo(expectedUri);
|
||||
ServletSourceHttpRequest sourceRequest = new ServletSourceHttpRequest(this.request);
|
||||
assertThat(sourceRequest.getUri().toString()).isEqualTo(expectedUri);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user