From d31dbac69ef2df730e797fe68af808f11c5579e5 Mon Sep 17 00:00:00 2001 From: Mihhail Lapushkin Date: Fri, 18 May 2018 11:31:51 +0300 Subject: [PATCH 1/2] Support custom UriTemplateHandler in LocalHostUriTemplateHandler See gh-13208 --- .../web/client/LocalHostUriTemplateHandler.java | 16 +++++++++++++++- .../client/LocalHostUriTemplateHandlerTests.java | 7 +++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 136475ea831..e8fd5f8dc49 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 @@ -58,9 +58,23 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { * @since 1.4.1 */ public LocalHostUriTemplateHandler(Environment environment, String scheme) { - super(new DefaultUriBuilderFactory()); + this(environment, scheme, new DefaultUriBuilderFactory()); + } + + /** + * Create a new {@code LocalHostUriTemplateHandler} that will generate URIs with the + * given {@code scheme}, use the given {@code environment} to determine the + * context-path and port and delegate to the given template {@code handler}. + * @param environment the environment used to determine the port + * @param scheme the scheme of the root uri + * @param handler the delegate handler + * @since 2.0.3 + */ + 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 04f9aab156e..faa150fd228 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 @@ -50,6 +50,13 @@ public class LocalHostUriTemplateHandlerTests { new LocalHostUriTemplateHandler(new MockEnvironment(), null); } + @Test + public void createWhenHandlerIsNullShouldThrowException() { + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Handler must not be null"); + new LocalHostUriTemplateHandler(new MockEnvironment(), "http", null); + } + @Test public void getRootUriShouldUseLocalServerPort() { MockEnvironment environment = new MockEnvironment(); From 300f6bf4d7cb47d40a393cd7108b80b3fd92d5a0 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 18 May 2018 14:20:31 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-13208 --- .../client/LocalHostUriTemplateHandler.java | 4 ++-- .../LocalHostUriTemplateHandlerTests.java | 24 ++++++++++++++++++- .../web/client/RootUriTemplateHandler.java | 3 ++- 3 files changed, 27 insertions(+), 4 deletions(-) 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 e8fd5f8dc49..3a059b432a2 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 faa150fd228..d60e7bc90cf 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 c8c8f31e257..f5e7712f13d 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; }