|
| 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 | +} |
0 commit comments