Skip to content

Commit

Permalink
Expose localAddress in WebFlux server
Browse files Browse the repository at this point in the history
Closes gh-24174
  • Loading branch information
rstoyanchev committed Dec 10, 2019
1 parent 2bd821c commit 1b172c1
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 31 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Nullable
private final InetSocketAddress remoteAddress;

@Nullable
private final InetSocketAddress localAddress;

@Nullable
private final SslInfo sslInfo;

Expand All @@ -69,13 +72,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {

private MockServerHttpRequest(HttpMethod httpMethod, URI uri, @Nullable String contextPath,
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
@Nullable InetSocketAddress remoteAddress, @Nullable SslInfo sslInfo,
Publisher<? extends DataBuffer> body) {
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {

super(uri, contextPath, headers);
this.httpMethod = httpMethod;
this.cookies = cookies;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
this.sslInfo = sslInfo;
this.body = Flux.from(body);
}
Expand All @@ -97,6 +101,12 @@ public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
}

@Nullable
@Override
public InetSocketAddress getLocalAddress() {
return this.localAddress;
}

@Nullable
@Override
protected SslInfo initSslInfo() {
Expand Down Expand Up @@ -254,6 +264,12 @@ public interface BaseBuilder<B extends BaseBuilder<B>> {
*/
B remoteAddress(InetSocketAddress remoteAddress);

/**
* Set the local address to return.
* @since 5.2.3
*/
B localAddress(InetSocketAddress localAddress);

/**
* Set SSL session information and certificates.
*/
Expand Down Expand Up @@ -408,6 +424,9 @@ private static class DefaultBodyBuilder implements BodyBuilder {
@Nullable
private InetSocketAddress remoteAddress;

@Nullable
private InetSocketAddress localAddress;

@Nullable
private SslInfo sslInfo;

Expand Down Expand Up @@ -441,6 +460,12 @@ public BodyBuilder remoteAddress(InetSocketAddress remoteAddress) {
return this;
}

@Override
public BodyBuilder localAddress(InetSocketAddress localAddress) {
this.localAddress = localAddress;
return this;
}

@Override
public void sslInfo(SslInfo sslInfo) {
this.sslInfo = sslInfo;
Expand Down Expand Up @@ -545,7 +570,7 @@ private Charset getCharset() {
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
applyCookiesIfNecessary();
return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath,
this.headers, this.cookies, this.remoteAddress, this.sslInfo, body);
this.headers, this.cookies, this.remoteAddress, this.localAddress, this.sslInfo, body);
}

private void applyCookiesIfNecessary() {
Expand Down
Expand Up @@ -93,6 +93,9 @@ public final class MockServerRequest implements ServerRequest {
@Nullable
private final InetSocketAddress remoteAddress;

@Nullable
private final InetSocketAddress localAddress;

private final List<HttpMessageReader<?>> messageReaders;

@Nullable
Expand All @@ -103,8 +106,8 @@ private MockServerRequest(HttpMethod method, URI uri, String contextPath, MockHe
MultiValueMap<String, HttpCookie> cookies, @Nullable Object body,
Map<String, Object> attributes, MultiValueMap<String, String> queryParams,
Map<String, String> pathVariables, @Nullable WebSession session, @Nullable Principal principal,
@Nullable InetSocketAddress remoteAddress, List<HttpMessageReader<?>> messageReaders,
@Nullable ServerWebExchange exchange) {
@Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress,
List<HttpMessageReader<?>> messageReaders, @Nullable ServerWebExchange exchange) {

this.method = method;
this.uri = uri;
Expand All @@ -118,6 +121,7 @@ private MockServerRequest(HttpMethod method, URI uri, String contextPath, MockHe
this.session = session;
this.principal = principal;
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
this.messageReaders = messageReaders;
this.exchange = exchange;
}
Expand Down Expand Up @@ -163,6 +167,11 @@ public Optional<InetSocketAddress> remoteAddress() {
return Optional.ofNullable(this.remoteAddress);
}

@Override
public Optional<InetSocketAddress> localAddress() {
return Optional.ofNullable(this.localAddress);
}

@Override
public List<HttpMessageReader<?>> messageReaders() {
return this.messageReaders;
Expand Down Expand Up @@ -303,6 +312,8 @@ public interface Builder {

Builder remoteAddress(InetSocketAddress remoteAddress);

Builder localAddress(InetSocketAddress localAddress);

Builder messageReaders(List<HttpMessageReader<?>> messageReaders);

Builder exchange(ServerWebExchange exchange);
Expand Down Expand Up @@ -343,6 +354,9 @@ private static class BuilderImpl implements Builder {
@Nullable
private InetSocketAddress remoteAddress;

@Nullable
private InetSocketAddress localAddress;

private List<HttpMessageReader<?>> messageReaders = HandlerStrategies.withDefaults().messageReaders();

@Nullable
Expand Down Expand Up @@ -470,6 +484,13 @@ public Builder remoteAddress(InetSocketAddress remoteAddress) {
return this;
}

@Override
public Builder localAddress(InetSocketAddress localAddress) {
Assert.notNull(localAddress, "'localAddress' must not be null");
this.localAddress = localAddress;
return this;
}

@Override
public Builder messageReaders(List<HttpMessageReader<?>> messageReaders) {
Assert.notNull(messageReaders, "'messageReaders' must not be null");
Expand All @@ -489,16 +510,16 @@ public MockServerRequest body(Object body) {
this.body = body;
return new MockServerRequest(this.method, this.uri, this.contextPath, this.headers,
this.cookies, this.body, this.attributes, this.queryParams, this.pathVariables,
this.session, this.principal, this.remoteAddress, this.messageReaders,
this.exchange);
this.session, this.principal, this.remoteAddress, this.localAddress,
this.messageReaders, this.exchange);
}

@Override
public MockServerRequest build() {
return new MockServerRequest(this.method, this.uri, this.contextPath, this.headers,
this.cookies, null, this.attributes, this.queryParams, this.pathVariables,
this.session, this.principal, this.remoteAddress, this.messageReaders,
this.exchange);
this.session, this.principal, this.remoteAddress, this.localAddress,
this.messageReaders, this.exchange);
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -181,9 +181,6 @@ private static class MutatedServerHttpRequest extends AbstractServerHttpRequest

private final MultiValueMap<String, HttpCookie> cookies;

@Nullable
private final InetSocketAddress remoteAddress;

@Nullable
private final SslInfo sslInfo;

Expand All @@ -199,7 +196,6 @@ public MutatedServerHttpRequest(URI uri, @Nullable String contextPath,
super(uri, contextPath, headers);
this.methodValue = methodValue;
this.cookies = cookies;
this.remoteAddress = originalRequest.getRemoteAddress();
this.sslInfo = sslInfo != null ? sslInfo : originalRequest.getSslInfo();
this.body = body;
this.originalRequest = originalRequest;
Expand All @@ -218,7 +214,13 @@ protected MultiValueMap<String, HttpCookie> initCookies() {
@Nullable
@Override
public InetSocketAddress getRemoteAddress() {
return this.remoteAddress;
return this.originalRequest.getRemoteAddress();
}

@Nullable
@Override
public InetSocketAddress getLocalAddress() {
return this.originalRequest.getLocalAddress();
}

@Nullable
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -151,6 +151,11 @@ public InetSocketAddress getRemoteAddress() {
return this.request.remoteAddress();
}

@Override
public InetSocketAddress getLocalAddress() {
return this.request.hostAddress();
}

@Override
@Nullable
protected SslInfo initSslInfo() {
Expand Down
Expand Up @@ -72,6 +72,15 @@ default InetSocketAddress getRemoteAddress() {
return null;
}

/**
* Return the local address the request was accepted on, if available.
* 5.2.3
*/
@Nullable
default InetSocketAddress getLocalAddress() {
return null;
}

/**
* Return the SSL session information if the request has been transmitted
* over a secure protocol including SSL certificates, if available.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -101,6 +101,11 @@ public InetSocketAddress getRemoteAddress() {
return getDelegate().getRemoteAddress();
}

@Override
public InetSocketAddress getLocalAddress() {
return getDelegate().getLocalAddress();
}

@Nullable
@Override
public SslInfo getSslInfo() {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -178,6 +178,11 @@ public InetSocketAddress getRemoteAddress() {
return new InetSocketAddress(this.request.getRemoteHost(), this.request.getRemotePort());
}

@Override
public InetSocketAddress getLocalAddress() {
return new InetSocketAddress(this.request.getLocalAddr(), this.request.getLocalPort());
}

@Override
@Nullable
protected SslInfo initSslInfo() {
Expand Down
Expand Up @@ -102,6 +102,11 @@ public InetSocketAddress getRemoteAddress() {
return this.exchange.getSourceAddress();
}

@Override
public InetSocketAddress getLocalAddress() {
return this.exchange.getDestinationAddress();
}

@Nullable
@Override
protected SslInfo initSslInfo() {
Expand Down

0 comments on commit 1b172c1

Please sign in to comment.