diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java index e8fd5f8dc4..3a059b432a 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandler.java @@ -70,11 +70,11 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { * @param handler the delegate handler * @since 2.0.3 */ - public LocalHostUriTemplateHandler(Environment environment, String scheme, UriTemplateHandler handler) { + public LocalHostUriTemplateHandler(Environment environment, String scheme, + UriTemplateHandler handler) { super(handler); Assert.notNull(environment, "Environment must not be null"); Assert.notNull(scheme, "Scheme must not be null"); - Assert.notNull(handler, "Handler must not be null"); this.environment = environment; this.scheme = scheme; } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java index faa150fd22..d60e7bc90c 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/LocalHostUriTemplateHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -16,13 +16,21 @@ package org.springframework.boot.test.web.client; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.mock.env.MockEnvironment; +import org.springframework.web.util.UriTemplateHandler; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * Tests for {@link LocalHostUriTemplateHandler}. @@ -91,4 +99,18 @@ public class LocalHostUriTemplateHandlerTests { assertThat(handler.getRootUri()).isEqualTo("http://localhost:8080/foo"); } + @Test + public void expandShouldUseCustomHandler() { + MockEnvironment environment = new MockEnvironment(); + UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class); + Map uriVariables = new HashMap<>(); + URI uri = URI.create("http://www.example.com"); + given(uriTemplateHandler.expand("https://localhost:8080/", uriVariables)) + .willReturn(uri); + LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( + environment, "https", uriTemplateHandler); + assertThat(handler.expand("/", uriVariables)).isEqualTo(uri); + verify(uriTemplateHandler).expand("https://localhost:8080/", uriVariables); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java index c8c8f31e25..f5e7712f13 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RootUriTemplateHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -38,6 +38,7 @@ public class RootUriTemplateHandler implements UriTemplateHandler { private final UriTemplateHandler handler; protected RootUriTemplateHandler(UriTemplateHandler handler) { + Assert.notNull(handler, "Handler must not be null"); this.rootUri = null; this.handler = handler; }