Search in sources :

Example 26 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method boundRestTemplateWhenUrlIncludesDomainShouldNotPrefixRootUri.

@Test
void boundRestTemplateWhenUrlIncludesDomainShouldNotPrefixRootUri() {
    RestTemplate restTemplate = new RestTemplateBuilder().rootUri("https://example.com").build();
    MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
    server.expect(requestTo("/hello")).andRespond(withSuccess());
    assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> restTemplate.getForEntity("https://spring.io/hello", String.class)).withMessageContaining("expected:<https://example.com/hello> but was:<https://spring.io/hello>");
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.jupiter.api.Test)

Example 27 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method boundRestTemplateShouldPrefixRootUri.

@Test
void boundRestTemplateShouldPrefixRootUri() {
    RestTemplate restTemplate = new RestTemplateBuilder().rootUri("https://example.com").build();
    MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
    server.expect(requestTo("/hello")).andRespond(withSuccess());
    restTemplate.getForEntity("/hello", String.class);
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.jupiter.api.Test)

Example 28 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method bindToShouldReturnMockRestServiceServer.

@Test
void bindToShouldReturnMockRestServiceServer() {
    RestTemplate restTemplate = new RestTemplateBuilder().build();
    MockRestServiceServer bound = RootUriRequestExpectationManager.bindTo(restTemplate);
    assertThat(bound).isNotNull();
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.jupiter.api.Test)

Example 29 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class CorsSampleActuatorApplicationTests method setUp.

@BeforeEach
void setUp() {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(this.applicationContext.getEnvironment(), "http");
    builder = builder.uriTemplateHandler(handler);
    this.testRestTemplate = new TestRestTemplate(builder);
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) LocalHostUriTemplateHandler(org.springframework.boot.test.web.client.LocalHostUriTemplateHandler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class TestRestTemplateTests method useTheSameRequestFactoryClassWithBasicAuth.

@Test
void useTheSameRequestFactoryClassWithBasicAuth() {
    OkHttp3ClientHttpRequestFactory customFactory = new OkHttp3ClientHttpRequestFactory();
    RestTemplateBuilder builder = new RestTemplateBuilder().requestFactory(() -> customFactory);
    TestRestTemplate testRestTemplate = new TestRestTemplate(builder).withBasicAuth("test", "test");
    RestTemplate restTemplate = testRestTemplate.getRestTemplate();
    assertThat(restTemplate.getRequestFactory()).isEqualTo(customFactory).hasSameClassAs(customFactory);
}
Also used : OkHttp3ClientHttpRequestFactory(org.springframework.http.client.OkHttp3ClientHttpRequestFactory) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)37 Test (org.junit.jupiter.api.Test)22 RestTemplate (org.springframework.web.client.RestTemplate)21 MockRestServiceServer (org.springframework.test.web.client.MockRestServiceServer)5 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)4 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)3 Test (org.junit.Test)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 LocalHostUriTemplateHandler (org.springframework.boot.test.web.client.LocalHostUriTemplateHandler)3 Bean (org.springframework.context.annotation.Bean)3 RestTemplateResponseErrorHandler (edu.hawaii.its.api.controller.RestTemplateResponseErrorHandler)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 MockServerRestTemplateCustomizer (org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 RequestExpectationManager (org.springframework.test.web.client.RequestExpectationManager)2 Builder (io.micrometer.core.instrument.Timer.Builder)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1