Commit c1236ed8 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #13208 from mihhail-lapushkin:master

* pr/13208:
  Polish contribution
  Support custom UriTemplateHandler in LocalHostUriTemplateHandler
parents 48cf0250 300f6bf4
......@@ -58,7 +58,21 @@ 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");
this.environment = environment;
......
/*
* 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}.
......@@ -50,6 +58,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();
......@@ -84,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<String, ?> 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);
}
}
/*
* 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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment