Skip to content

Commit 4a0784b

Browse files
committed
Test HTTPS in ReactorServerHttpRequest URI
This commit adds a test for ReactorServerHttpRequest.getUri() to check whether it returns a HTTPS scheme when configured with SSL. Issue: SPR-15931
1 parent e214d69 commit 4a0784b

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.server.reactive;
18+
19+
import java.net.URI;
20+
21+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
22+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
23+
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
24+
import org.apache.http.impl.client.CloseableHttpClient;
25+
import org.apache.http.impl.client.HttpClients;
26+
import org.apache.http.ssl.SSLContextBuilder;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.Parameterized;
32+
import reactor.core.publisher.Mono;
33+
34+
import org.springframework.http.HttpStatus;
35+
import org.springframework.http.RequestEntity;
36+
import org.springframework.http.ResponseEntity;
37+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
38+
import org.springframework.http.server.reactive.bootstrap.HttpServer;
39+
import org.springframework.http.server.reactive.bootstrap.ReactorHttpsServer;
40+
import org.springframework.web.client.RestTemplate;
41+
42+
import static org.junit.Assert.*;
43+
44+
/**
45+
* HTTPS-specific integration test for {@link ServerHttpRequest}.
46+
* @author Arjen Poutsma
47+
*/
48+
@RunWith(Parameterized.class)
49+
public class ServerHttpsRequestIntegrationTests {
50+
51+
private int port;
52+
53+
@Parameterized.Parameter(0)
54+
public HttpServer server;
55+
56+
private RestTemplate restTemplate;
57+
58+
@Parameterized.Parameters(name = "server [{0}]")
59+
public static Object[][] arguments() {
60+
return new Object[][]{
61+
{new ReactorHttpsServer()},
62+
};
63+
}
64+
65+
@Before
66+
public void setup() throws Exception {
67+
this.server.setHandler(new CheckRequestHandler());
68+
this.server.afterPropertiesSet();
69+
this.server.start();
70+
71+
// Set dynamically chosen port
72+
this.port = this.server.getPort();
73+
74+
SSLContextBuilder builder = new SSLContextBuilder();
75+
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
76+
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
77+
builder.build(), NoopHostnameVerifier.INSTANCE);
78+
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
79+
socketFactory).build();
80+
HttpComponentsClientHttpRequestFactory requestFactory =
81+
new HttpComponentsClientHttpRequestFactory(httpclient);
82+
this.restTemplate = new RestTemplate(requestFactory);
83+
}
84+
85+
@After
86+
public void tearDown() throws Exception {
87+
this.server.stop();
88+
this.port = 0;
89+
}
90+
91+
@Test
92+
public void checkUri() throws Exception {
93+
URI url = new URI("https://localhost:" + port + "/foo?param=bar");
94+
RequestEntity<Void> request = RequestEntity.post(url).build();
95+
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
96+
assertEquals(HttpStatus.OK, response.getStatusCode());
97+
}
98+
99+
public static class CheckRequestHandler implements HttpHandler {
100+
101+
@Override
102+
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
103+
URI uri = request.getURI();
104+
assertEquals("https", uri.getScheme());
105+
assertNotNull(uri.getHost());
106+
assertNotEquals(-1, uri.getPort());
107+
assertNotNull(request.getRemoteAddress());
108+
assertEquals("/foo", uri.getPath());
109+
assertEquals("param=bar", uri.getQuery());
110+
return Mono.empty();
111+
}
112+
}
113+
114+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.server.reactive.bootstrap;
18+
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
import reactor.ipc.netty.NettyContext;
22+
23+
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
24+
25+
/**
26+
* @author Stephane Maldini
27+
*/
28+
public class ReactorHttpsServer extends AbstractHttpServer {
29+
30+
private ReactorHttpHandlerAdapter reactorHandler;
31+
32+
private reactor.ipc.netty.http.server.HttpServer reactorServer;
33+
34+
private AtomicReference<NettyContext> nettyContext = new AtomicReference<>();
35+
36+
37+
@Override
38+
protected void initServer() throws Exception {
39+
this.reactorHandler = createHttpHandlerAdapter();
40+
this.reactorServer = reactor.ipc.netty.http.server.HttpServer.create(builder -> {
41+
builder.host(getHost()).port(getPort()).sslSelfSigned();
42+
});
43+
}
44+
45+
private ReactorHttpHandlerAdapter createHttpHandlerAdapter() {
46+
return new ReactorHttpHandlerAdapter(resolveHttpHandler());
47+
}
48+
49+
@Override
50+
protected void startInternal() {
51+
NettyContext nettyContext = this.reactorServer.newHandler(this.reactorHandler).block();
52+
setPort(nettyContext.address().getPort());
53+
this.nettyContext.set(nettyContext);
54+
}
55+
56+
@Override
57+
protected void stopInternal() {
58+
this.nettyContext.get().dispose();
59+
}
60+
61+
@Override
62+
protected void resetInternal() {
63+
this.reactorServer = null;
64+
this.reactorHandler = null;
65+
this.nettyContext.set(null);
66+
}
67+
68+
}

0 commit comments

Comments
 (0)