Skip to content

Commit 4269bb3

Browse files
dagerberspencergibb
authored andcommitted
Implements DefaultServiceInstance.getScheme().
This fixes the problem, that a https URI could not be overriden in Unit tests with http (avoiding certificate problems on the build server)
1 parent 9551cfb commit 4269bb3

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ public DefaultServiceInstance() {
110110
* @return URI of the form (secure)?https:http + "host:port".
111111
*/
112112
public static URI getUri(ServiceInstance instance) {
113-
String scheme = (instance.isSecure()) ? "https" : "http";
113+
String scheme = instance.getScheme();
114114
String uri = String.format("%s://%s:%s", scheme, instance.getHost(),
115115
instance.getPort());
116116
return URI.create(uri);
@@ -206,4 +206,9 @@ public int hashCode() {
206206
return Objects.hash(instanceId, serviceId, host, port, secure, metadata);
207207
}
208208

209+
@Override
210+
public String getScheme() {
211+
return isSecure() ? "https" : "http";
212+
}
213+
209214
}

spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientPropertiesMappingTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public void testDiscoveryClientShouldResolveSimpleValues() {
7979
then(s1.getPort()).isEqualTo(8080);
8080
then(s1.getUri()).isEqualTo(URI.create("http://s11:8080"));
8181
then(s1.isSecure()).isEqualTo(false);
82+
then(s1.getScheme()).isEqualTo("http");
83+
84+
ServiceInstance s2 = this.discoveryClient.getInstances("service1").get(1);
85+
then(s2.getHost()).isEqualTo("s12");
86+
then(s2.getPort()).isEqualTo(8443);
87+
then(s2.getUri()).isEqualTo(URI.create("https://s12:8443"));
88+
then(s2.isSecure()).isEqualTo(true);
89+
then(s2.getScheme()).isEqualTo("https");
8290
}
8391

8492
@Test
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2012-2021 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+
* https://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.cloud.client.discovery.simple.reactive;
18+
19+
import java.net.URI;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import reactor.core.publisher.Flux;
24+
import reactor.test.StepVerifier;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.cloud.client.ServiceInstance;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.test.context.junit4.SpringRunner;
32+
33+
import static org.assertj.core.api.BDDAssertions.then;
34+
35+
/**
36+
* Tests for mapping properties to instances in {@link SimpleReactiveDiscoveryClient}
37+
*
38+
* @author Daniel Gerber
39+
*/
40+
@RunWith(SpringRunner.class)
41+
@SpringBootTest(properties = { "spring.application.name=service0",
42+
"spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s11:8080",
43+
"spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s12:8443",
44+
"spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s21:8080",
45+
"spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s22:443" })
46+
public class SimpleReactiveDiscoveryClientPropertiesMappingTests {
47+
48+
@Autowired
49+
private SimpleReactiveDiscoveryProperties props;
50+
51+
@Autowired
52+
private SimpleReactiveDiscoveryClient discoveryClient;
53+
54+
@Test
55+
public void propsShouldGetCleanlyMapped() {
56+
then(props.getInstances().size()).isEqualTo(2);
57+
then(props.getInstances().get("service1").size()).isEqualTo(2);
58+
then(props.getInstances().get("service1").get(0).getHost()).isEqualTo("s11");
59+
then(props.getInstances().get("service1").get(0).getPort()).isEqualTo(8080);
60+
then(props.getInstances().get("service1").get(0).getUri()).isEqualTo(URI.create("http://s11:8080"));
61+
then(props.getInstances().get("service1").get(0).isSecure()).isEqualTo(false);
62+
63+
then(props.getInstances().get("service2").size()).isEqualTo(2);
64+
then(props.getInstances().get("service2").get(0).getHost()).isEqualTo("s21");
65+
then(props.getInstances().get("service2").get(0).getPort()).isEqualTo(8080);
66+
then(props.getInstances().get("service2").get(0).getUri()).isEqualTo(URI.create("https://s21:8080"));
67+
then(props.getInstances().get("service2").get(0).isSecure()).isEqualTo(true);
68+
}
69+
70+
@Test
71+
public void testDiscoveryClientShouldResolveSimpleValues() {
72+
then(discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client");
73+
74+
Flux<ServiceInstance> services = discoveryClient.getInstances("service1");
75+
StepVerifier.create(services)
76+
.expectNextMatches(inst -> inst.getHost().equals("s11") && inst.getPort() == 8080
77+
&& inst.getUri().toString().equals("http://s11:8080") && !inst.isSecure()
78+
&& inst.getScheme().equals("http"))
79+
.expectNextMatches(inst -> inst.getHost().equals("s12") && inst.getPort() == 8443
80+
&& inst.getUri().toString().equals("https://s12:8443") && inst.isSecure()
81+
&& inst.getScheme().equals("https"))
82+
.expectComplete().verify();
83+
}
84+
85+
@Test
86+
public void testGetANonExistentServiceShouldReturnAnEmptyList() {
87+
then(discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client");
88+
89+
Flux<ServiceInstance> services = discoveryClient.getInstances("nonexistent");
90+
91+
StepVerifier.create(services).expectNextCount(0).expectComplete().verify();
92+
}
93+
94+
@Configuration(proxyBeanMethods = false)
95+
@EnableAutoConfiguration
96+
public static class SampleConfig {
97+
98+
}
99+
100+
}

0 commit comments

Comments
 (0)