diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java index 673496ab9..bed1a02b3 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulAutoConfiguration.java @@ -24,7 +24,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; -import java.util.Collections; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; @@ -242,8 +241,7 @@ public boolean resolve(Object argument, MethodParameter parameter, HttpRequestVa } if (params.getConsistencyMode() != ConsulClient.ConsistencyMode.DEFAULT) { - builder.configureRequestParams( - map -> map.put(params.getConsistencyMode().getParamName(), Collections.emptyList())); + builder.addRequestParameter(params.getConsistencyMode().getParamName(), ""); } if (params.getWaitTime() != -1) { diff --git a/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/QueryParamsArgumentResolverTests.java b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/QueryParamsArgumentResolverTests.java new file mode 100644 index 000000000..0b04b9f4c --- /dev/null +++ b/spring-cloud-consul-core/src/test/java/org/springframework/cloud/consul/QueryParamsArgumentResolverTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2013-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.consul; + +import java.net.URI; + +import org.junit.Test; + +import org.springframework.cloud.consul.ConsulAutoConfiguration.QueryParamsArgumentResolver; +import org.springframework.cloud.consul.ConsulClient.ConsistencyMode; +import org.springframework.cloud.consul.ConsulClient.QueryParams; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpMethod; +import org.springframework.web.service.invoker.HttpRequestValues; +import org.springframework.web.util.DefaultUriBuilderFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link QueryParamsArgumentResolver}. + */ +public class QueryParamsArgumentResolverTests { + + @Test + public void staleFlagIsSent() throws Exception { + assertThat(resolve(new QueryParams(ConsistencyMode.STALE)).getQuery()).isEqualTo("stale="); + } + + @Test + public void consistentFlagIsSent() throws Exception { + assertThat(resolve(new QueryParams(ConsistencyMode.CONSISTENT)).getQuery()).isEqualTo("consistent="); + } + + @Test + public void defaultModeSendsNoFlag() throws Exception { + assertThat(resolve(QueryParams.DEFAULT).getQuery()).isNull(); + } + + private URI resolve(QueryParams params) throws Exception { + HttpRequestValues.Builder builder = HttpRequestValues.builder() + .setHttpMethod(HttpMethod.GET) + .setUriTemplate("/v1/catalog/services"); + MethodParameter parameter = new MethodParameter(getClass().getDeclaredMethod("target", QueryParams.class), 0); + new QueryParamsArgumentResolver().resolve(params, parameter, builder); + HttpRequestValues values = builder.build(); + return new DefaultUriBuilderFactory("http://localhost:8500").expand(values.getUriTemplate(), + values.getUriVariables()); + } + + void target(QueryParams params) { + } + +}