Skip to content

Commit c224eeb

Browse files
committed
Polish "Fix NullPointerException with empty X-Forwarded-For header"
Closes gh-16046
1 parent 64a74c3 commit c224eeb

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest {
5050
this.remoteAddress = getRemoteAddress(request);
5151
}
5252

53+
private static String getRemoteAddress(ServerHttpRequest request) {
54+
InetSocketAddress remoteAddress = request.getRemoteAddress();
55+
InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null;
56+
return (address != null) ? address.toString() : null;
57+
}
58+
5359
@Override
5460
public String getMethod() {
5561
return this.method;
@@ -70,10 +76,4 @@ public String getRemoteAddress() {
7076
return this.remoteAddress;
7177
}
7278

73-
private static String getRemoteAddress(ServerHttpRequest request) {
74-
InetSocketAddress remoteAddress = request.getRemoteAddress();
75-
InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null;
76-
return (address != null) ? address.toString() : null;
77-
}
78-
7979
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.assertj.core.api.Assertions.entry;
31-
import static org.mockito.Mockito.doReturn;
31+
import static org.mockito.BDDMockito.given;
3232
import static org.mockito.Mockito.mock;
3333

3434
/**
@@ -46,13 +46,13 @@ public class ServerWebExchangeTraceableRequestTests {
4646
public void setUp() {
4747
this.exchange = mock(ServerWebExchange.class);
4848
this.request = mock(ServerHttpRequest.class);
49-
doReturn(this.request).when(this.exchange).getRequest();
49+
given(this.exchange.getRequest()).willReturn(this.request);
5050
}
5151

5252
@Test
5353
public void getMethod() {
5454
String method = "POST";
55-
doReturn(method).when(this.request).getMethodValue();
55+
given(this.request.getMethodValue()).willReturn(method);
5656
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
5757
this.exchange);
5858
assertThat(traceableRequest.getMethod()).isSameAs(method);
@@ -61,7 +61,7 @@ public void getMethod() {
6161
@Test
6262
public void getUri() {
6363
URI uri = URI.create("http://localhost:8080/");
64-
doReturn(uri).when(this.request).getURI();
64+
given(this.request.getURI()).willReturn(uri);
6565
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
6666
this.exchange);
6767
assertThat(traceableRequest.getUri()).isSameAs(uri);
@@ -71,7 +71,7 @@ public void getUri() {
7171
public void getHeaders() {
7272
HttpHeaders httpHeaders = new HttpHeaders();
7373
httpHeaders.add("name", "value");
74-
doReturn(httpHeaders).when(this.request).getHeaders();
74+
given(this.request.getHeaders()).willReturn(httpHeaders);
7575
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
7676
this.exchange);
7777
assertThat(traceableRequest.getHeaders())
@@ -80,18 +80,18 @@ public void getHeaders() {
8080

8181
@Test
8282
public void getUnresolvedRemoteAddress() {
83-
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("", 0);
84-
doReturn(socketAddress).when(this.request).getRemoteAddress();
83+
InetSocketAddress socketAddress = InetSocketAddress
84+
.createUnresolved("unresolved.example.com", 8080);
85+
given(this.request.getRemoteAddress()).willReturn(socketAddress);
8586
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
8687
this.exchange);
8788
assertThat(traceableRequest.getRemoteAddress()).isNull();
88-
8989
}
9090

9191
@Test
9292
public void getRemoteAddress() {
9393
InetSocketAddress socketAddress = new InetSocketAddress(0);
94-
doReturn(socketAddress).when(this.request).getRemoteAddress();
94+
given(this.request.getRemoteAddress()).willReturn(socketAddress);
9595
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
9696
this.exchange);
9797
assertThat(traceableRequest.getRemoteAddress())

0 commit comments

Comments
 (0)