Browse Source
Add a ContextCustomizerFactory to provide TestRestTemplate as a bean for tests annotated with WebIntegrationTests. Additionally provide support for automatically expanding URLs of the form `/example` to `http://localhost:${local.server.port}/example`. Fixes gh-5227pull/5488/head
6 changed files with 220 additions and 0 deletions
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.test.context.web; |
||||
|
||||
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler; |
||||
import org.springframework.boot.test.web.client.TestRestTemplate; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.core.env.Environment; |
||||
import org.springframework.test.context.ContextCustomizer; |
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
|
||||
/** |
||||
* {@link ContextCustomizer} for {@link WebIntegrationTest} that provides a |
||||
* {@link TestRestTemplate} bean that can automatically resolve |
||||
* {@literal local.server.port}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
class WebIntegrationTestContextCustomizer implements ContextCustomizer { |
||||
|
||||
@Override |
||||
public void customizeContext(ConfigurableApplicationContext context, |
||||
MergedContextConfiguration mergedContextConfiguration) { |
||||
TestRestTemplate restTemplate = getRestTemplate(context.getEnvironment()); |
||||
context.getBeanFactory().registerSingleton("testRestTemplate", restTemplate); |
||||
} |
||||
|
||||
private TestRestTemplate getRestTemplate(Environment environment) { |
||||
TestRestTemplate template = new TestRestTemplate(); |
||||
template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment)); |
||||
return template; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return getClass().hashCode(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (obj == null || obj.getClass() != getClass()) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.test.context.web; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils; |
||||
import org.springframework.test.context.ContextConfigurationAttributes; |
||||
import org.springframework.test.context.ContextCustomizer; |
||||
import org.springframework.test.context.ContextCustomizerFactory; |
||||
|
||||
/** |
||||
* {@link ContextCustomizerFactory} for {@link WebIntegrationTest}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @see WebIntegrationTestContextCustomizer |
||||
*/ |
||||
class WebIntegrationTestContextCustomizerFactory implements ContextCustomizerFactory { |
||||
|
||||
@Override |
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass, |
||||
List<ContextConfigurationAttributes> configAttributes) { |
||||
if (AnnotatedElementUtils.findMergedAnnotation(testClass, |
||||
WebIntegrationTest.class) != null) { |
||||
return new WebIntegrationTestContextCustomizer(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.test.web.client; |
||||
|
||||
import org.springframework.core.env.Environment; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.util.DefaultUriTemplateHandler; |
||||
import org.springframework.web.util.UriTemplateHandler; |
||||
|
||||
/** |
||||
* {@link UriTemplateHandler} will automatically prefix relative URLs with |
||||
* <code>localhost:${local.server.port}</code>. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.4.0 |
||||
*/ |
||||
public class LocalHostUriTemplateHandler extends DefaultUriTemplateHandler { |
||||
|
||||
private final Environment environment; |
||||
|
||||
public LocalHostUriTemplateHandler(Environment environment) { |
||||
Assert.notNull(environment, "Environment must not be null"); |
||||
this.environment = environment; |
||||
} |
||||
|
||||
@Override |
||||
public String getBaseUrl() { |
||||
String port = this.environment.getProperty("local.server.port", "8080"); |
||||
return "http://localhost:" + port; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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.boot.test.web.client; |
||||
|
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
|
||||
import org.springframework.mock.env.MockEnvironment; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link LocalHostUriTemplateHandler}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class LocalHostUriTemplateHandlerTests { |
||||
|
||||
@Rule |
||||
public ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
@Test |
||||
public void createWhenEnvironmentIsNullShouldThrowException() { |
||||
this.thrown.expect(IllegalArgumentException.class); |
||||
this.thrown.expectMessage("Environment must not be null"); |
||||
new LocalHostUriTemplateHandler(null); |
||||
} |
||||
|
||||
@Test |
||||
public void getBaseUrlShouldUseLocalServerPort() throws Exception { |
||||
MockEnvironment environment = new MockEnvironment(); |
||||
environment.setProperty("local.server.port", "1234"); |
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( |
||||
environment); |
||||
assertThat(handler.getBaseUrl()).isEqualTo("http://localhost:1234"); |
||||
} |
||||
|
||||
@Test |
||||
public void getBaseUrlWhenLocalServerPortMissingShouldUsePort8080() throws Exception { |
||||
MockEnvironment environment = new MockEnvironment(); |
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( |
||||
environment); |
||||
assertThat(handler.getBaseUrl()).isEqualTo("http://localhost:8080"); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue