Search in sources :

Example 6 with POST

use of cn.taketoday.http.HttpMethod.POST in project today-framework by TAKETODAY.

the class RestTemplateTests method clientHttpRequestInitializerAndRequestInterceptorAreBothApplied.

@Test
void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
    ClientHttpRequestInitializer initializer = request -> request.getHeaders().add("MyHeader", "MyInitializerValue");
    ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
        request.getHeaders().add("MyHeader", "MyInterceptorValue");
        return execution.execute(request, body);
    };
    template.setHttpRequestInitializers(Collections.singletonList(initializer));
    template.setInterceptors(interceptor);
    MediaType contentType = MediaType.TEXT_PLAIN;
    given(converter.canWrite(String.class, contentType)).willReturn(true);
    HttpHeaders requestHeaders = HttpHeaders.create();
    mockSentRequest(POST, "https://example.com", requestHeaders);
    mockResponseStatus(HttpStatus.OK);
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.setContentType(contentType);
    HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
    template.exchange("https://example.com", POST, entity, Void.class);
    assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
    verify(response).close();
}
Also used : ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MediaType.parseMediaType(cn.taketoday.http.MediaType.parseMediaType) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PUT(cn.taketoday.http.HttpMethod.PUT) DELETE(cn.taketoday.http.HttpMethod.DELETE) HttpHeaders(cn.taketoday.http.HttpHeaders) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) ByteArrayInputStream(java.io.ByteArrayInputStream) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) DefaultUriBuilderFactory(cn.taketoday.web.util.DefaultUriBuilderFactory) ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) URI(java.net.URI) PATCH(cn.taketoday.http.HttpMethod.PATCH) EnumSet(java.util.EnumSet) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) Set(java.util.Set) ResponseEntity(cn.taketoday.http.ResponseEntity) ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) StreamUtils(cn.taketoday.util.StreamUtils) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) Entry(java.util.Map.Entry) HttpStatus(cn.taketoday.http.HttpStatus) MockResponse(okhttp3.mockwebserver.MockResponse) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HashMap(java.util.HashMap) HttpMethod(cn.taketoday.http.HttpMethod) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TypeReference(cn.taketoday.core.TypeReference) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest) IOException(java.io.IOException) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) HttpInputMessage(cn.taketoday.http.HttpInputMessage) Mockito.verify(org.mockito.Mockito.verify) HEAD(cn.taketoday.http.HttpMethod.HEAD) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) MediaType.parseMediaType(cn.taketoday.http.MediaType.parseMediaType) MediaType(cn.taketoday.http.MediaType) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 7 with POST

use of cn.taketoday.http.HttpMethod.POST in project today-framework by TAKETODAY.

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) DELETE(cn.taketoday.http.HttpMethod.DELETE) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) BDDMockito.given(org.mockito.BDDMockito.given) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) URI(java.net.URI) TypeReference(cn.taketoday.core.TypeReference) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) BodyInserter(cn.taketoday.web.reactive.function.BodyInserter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with POST

use of cn.taketoday.http.HttpMethod.POST in project today-infrastructure by TAKETODAY.

the class RestTemplateTests method clientHttpRequestInitializerAndRequestInterceptorAreBothApplied.

@Test
void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
    ClientHttpRequestInitializer initializer = request -> request.getHeaders().add("MyHeader", "MyInitializerValue");
    ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
        request.getHeaders().add("MyHeader", "MyInterceptorValue");
        return execution.execute(request, body);
    };
    template.setHttpRequestInitializers(Collections.singletonList(initializer));
    template.setInterceptors(interceptor);
    MediaType contentType = MediaType.TEXT_PLAIN;
    given(converter.canWrite(String.class, contentType)).willReturn(true);
    HttpHeaders requestHeaders = HttpHeaders.create();
    mockSentRequest(POST, "https://example.com", requestHeaders);
    mockResponseStatus(HttpStatus.OK);
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.setContentType(contentType);
    HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
    template.exchange("https://example.com", POST, entity, Void.class);
    assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
    verify(response).close();
}
Also used : ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MediaType.parseMediaType(cn.taketoday.http.MediaType.parseMediaType) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PUT(cn.taketoday.http.HttpMethod.PUT) DELETE(cn.taketoday.http.HttpMethod.DELETE) HttpHeaders(cn.taketoday.http.HttpHeaders) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) ByteArrayInputStream(java.io.ByteArrayInputStream) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) DefaultUriBuilderFactory(cn.taketoday.web.util.DefaultUriBuilderFactory) ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) URI(java.net.URI) PATCH(cn.taketoday.http.HttpMethod.PATCH) EnumSet(java.util.EnumSet) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) Set(java.util.Set) ResponseEntity(cn.taketoday.http.ResponseEntity) ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) StreamUtils(cn.taketoday.util.StreamUtils) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) Entry(java.util.Map.Entry) HttpStatus(cn.taketoday.http.HttpStatus) MockResponse(okhttp3.mockwebserver.MockResponse) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HashMap(java.util.HashMap) HttpMethod(cn.taketoday.http.HttpMethod) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TypeReference(cn.taketoday.core.TypeReference) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest) IOException(java.io.IOException) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) HttpInputMessage(cn.taketoday.http.HttpInputMessage) Mockito.verify(org.mockito.Mockito.verify) HEAD(cn.taketoday.http.HttpMethod.HEAD) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) MediaType.parseMediaType(cn.taketoday.http.MediaType.parseMediaType) MediaType(cn.taketoday.http.MediaType) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 9 with POST

use of cn.taketoday.http.HttpMethod.POST in project today-infrastructure by TAKETODAY.

the class DefaultClientRequestBuilderTests method bodyInserter.

@Test
public void bodyInserter() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) DELETE(cn.taketoday.http.HttpMethod.DELETE) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) BDDMockito.given(org.mockito.BDDMockito.given) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) URI(java.net.URI) TypeReference(cn.taketoday.core.TypeReference) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) BodyInserter(cn.taketoday.web.reactive.function.BodyInserter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with POST

use of cn.taketoday.http.HttpMethod.POST in project today-infrastructure by TAKETODAY.

the class DefaultClientRequestBuilderTests method fromCopiesBody.

@Test
public void fromCopiesBody() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest other = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    ClientRequest result = ClientRequest.from(other).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(POST, "/");
    result.writeTo(request, strategies).block();
    String copiedBody = request.getBodyAsString().block();
    assertThat(copiedBody).isEqualTo("foo");
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) DELETE(cn.taketoday.http.HttpMethod.DELETE) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) BDDMockito.given(org.mockito.BDDMockito.given) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) URI(java.net.URI) TypeReference(cn.taketoday.core.TypeReference) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) BodyInserter(cn.taketoday.web.reactive.function.BodyInserter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

TypeReference (cn.taketoday.core.TypeReference)10 DELETE (cn.taketoday.http.HttpMethod.DELETE)10 GET (cn.taketoday.http.HttpMethod.GET)10 OPTIONS (cn.taketoday.http.HttpMethod.OPTIONS)10 POST (cn.taketoday.http.HttpMethod.POST)10 URI (java.net.URI)10 List (java.util.List)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Test (org.junit.jupiter.api.Test)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 HttpEntity (cn.taketoday.http.HttpEntity)6 HttpHeaders (cn.taketoday.http.HttpHeaders)6 HttpInputMessage (cn.taketoday.http.HttpInputMessage)6 HttpMethod (cn.taketoday.http.HttpMethod)6 HEAD (cn.taketoday.http.HttpMethod.HEAD)6 PATCH (cn.taketoday.http.HttpMethod.PATCH)6 PUT (cn.taketoday.http.HttpMethod.PUT)6 HttpStatus (cn.taketoday.http.HttpStatus)6 MediaType (cn.taketoday.http.MediaType)6