Search in sources :

Example 1 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-infrastructure by TAKETODAY.

the class ErrorPageFilterIntegrationTests method doTest.

private void doTest(AnnotationConfigServletWebServerApplicationContext context, String resourcePath, HttpStatus status) throws Exception {
    int port = context.getWebServer().getPort();
    RestTemplate template = new RestTemplate();
    ResponseEntity<String> entity = template.getForEntity(new URI("http://localhost:" + port + resourcePath), String.class);
    assertThat(entity.getBody()).isEqualTo("Hello World");
    assertThat(entity.getStatusCode()).isEqualTo(status);
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) URI(java.net.URI)

Example 2 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-infrastructure by TAKETODAY.

the class ServletComponentScanIntegrationTests method indexedComponentsAreRegistered.

@ParameterizedTest(name = "{0}")
@MethodSource("testConfiguration")
void indexedComponentsAreRegistered(String serverName, Class<?> configuration) throws IOException {
    writeIndex(this.temp);
    this.context = new AnnotationConfigServletWebServerApplicationContext();
    try (URLClassLoader classLoader = new URLClassLoader(new URL[] { this.temp.toURI().toURL() }, getClass().getClassLoader())) {
        this.context.setClassLoader(classLoader);
        this.context.register(configuration);
        new ServerPortInfoApplicationContextInitializer().initialize(this.context);
        this.context.refresh();
        String port = this.context.getEnvironment().getProperty("local.server.port");
        String response = new RestTemplate().getForObject("http://localhost:" + port + "/test", String.class);
        assertThat(response).isEqualTo("alpha bravo charlie");
    }
}
Also used : ServerPortInfoApplicationContextInitializer(cn.taketoday.framework.web.context.ServerPortInfoApplicationContextInitializer) AnnotationConfigServletWebServerApplicationContext(cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) URLClassLoader(java.net.URLClassLoader) RestTemplate(cn.taketoday.web.client.RestTemplate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-infrastructure by TAKETODAY.

the class TomcatServletWebServerFactoryTests method nonExistentUploadDirectoryIsCreatedUponMultipartUpload.

@Test
void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() throws IOException, URISyntaxException {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
    AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
    factory.addInitializers((servletContext) -> {
        servletContextReference.set(servletContext);
        Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {

            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                req.getParts();
            }
        });
        servlet.addMapping("/upload");
        servlet.setMultipartConfig(new MultipartConfigElement((String) null));
    });
    this.webServer = factory.getWebServer();
    this.webServer.start();
    File temp = (File) servletContextReference.get().getAttribute(ServletContext.TEMPDIR);
    FileSystemUtils.deleteRecursively(temp);
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = HttpHeaders.create();
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    ResponseEntity<String> response = restTemplate.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) HttpEntity(cn.taketoday.http.HttpEntity) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) RestTemplate(cn.taketoday.web.client.RestTemplate) ServletContext(jakarta.servlet.ServletContext) File(java.io.File) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 4 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-infrastructure by TAKETODAY.

the class RestTemplateBuilderTests method messageConvertersShouldApply.

@Test
void messageConvertersShouldApply() {
    RestTemplate template = this.builder.messageConverters(this.messageConverter).build();
    assertThat(template.getMessageConverters()).containsOnly(this.messageConverter);
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Example 5 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-infrastructure by TAKETODAY.

the class RestTemplateBuilderTests method defaultMessageConvertersShouldClearExisting.

@Test
void defaultMessageConvertersShouldClearExisting() {
    RestTemplate template = new RestTemplate(Collections.singletonList(new StringHttpMessageConverter()));
    this.builder.additionalMessageConverters(this.messageConverter).defaultMessageConverters().configure(template);
    assertThat(template.getMessageConverters()).hasSameSizeAs(new RestTemplate().getMessageConverters());
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

RestTemplate (cn.taketoday.web.client.RestTemplate)100 Test (org.junit.jupiter.api.Test)66 URI (java.net.URI)30 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)14 ClientHttpRequest (cn.taketoday.http.client.ClientHttpRequest)12 SimpleClientHttpRequestFactory (cn.taketoday.http.client.SimpleClientHttpRequestFactory)12 BufferingClientHttpRequestFactory (cn.taketoday.http.client.BufferingClientHttpRequestFactory)10 ClientHttpRequestInterceptor (cn.taketoday.http.client.ClientHttpRequestInterceptor)10 HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)10 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)10 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)10 UriTemplateHandler (cn.taketoday.web.util.UriTemplateHandler)10 HttpHeaders (cn.taketoday.http.HttpHeaders)8 ClientHttpRequestFactory (cn.taketoday.http.client.ClientHttpRequestFactory)8 InterceptingClientHttpRequestFactory (cn.taketoday.http.client.InterceptingClientHttpRequestFactory)8 OkHttp3ClientHttpRequestFactory (cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory)8 ResponseErrorHandler (cn.taketoday.web.client.ResponseErrorHandler)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 InOrder (org.mockito.InOrder)8 HttpMethod (cn.taketoday.http.HttpMethod)6