use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class DefaultServerRequestBuilderTests method from.
@Test
public void from() {
MockServerHttpRequest request = MockServerHttpRequest.post("https://example.com").header("foo", "bar").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
ServerRequest other = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
other.attributes().put("attr1", "value1");
Flux<DataBuffer> body = Flux.just("baz").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
ServerRequest result = ServerRequest.from(other).method(HttpMethod.HEAD).headers(httpHeaders -> httpHeaders.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).attribute("attr2", "value2").attributes(attributes -> attributes.put("attr3", "value3")).body(body).build();
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
assertThat(result.headers().asHttpHeaders()).hasSize(1);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
assertThat(result.cookies()).hasSize(1);
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
assertThat(result.attributes()).containsOnlyKeys(ServerWebExchange.LOG_ID_ATTRIBUTE, "attr1", "attr2", "attr3");
assertThat(result.attributes()).contains(entry("attr1", "value1"), entry("attr2", "value2"), entry("attr3", "value3"));
StepVerifier.create(result.bodyToFlux(String.class)).expectNext("baz").verifyComplete();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class DefaultServerRequestTests method body.
@Test
public void body() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com?foo=bar").headers(httpHeaders).body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);
Mono<String> resultMono = request.body(toMono(String.class));
assertThat(resultMono.block()).isEqualTo("foo");
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class DefaultServerRequestTests method bodyUnacceptable.
@Test
public void bodyUnacceptable() {
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com?foo=bar").headers(httpHeaders).body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Flux<String> resultFlux = request.bodyToFlux(String.class);
StepVerifier.create(resultFlux).expectError(UnsupportedMediaTypeStatusException.class).verify();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class DefaultServerRequestTests method multipartData.
@Test
public void multipartData() {
String data = "--12345\r\n" + "Content-Disposition: form-data; name=\"foo\"\r\n" + "\r\n" + "bar\r\n" + "--12345\r\n" + "Content-Disposition: form-data; name=\"baz\"\r\n" + "\r\n" + "qux\r\n" + "--12345--\r\n";
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=12345");
MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, "https://example.com").headers(httpHeaders).body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<MultiValueMap<String, Part>> resultData = request.multipartData();
StepVerifier.create(resultData).consumeNextWith(formData -> {
assertThat(formData.size()).isEqualTo(2);
Part part = formData.getFirst("foo");
boolean condition1 = part instanceof FormFieldPart;
assertThat(condition1).isTrue();
FormFieldPart formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("bar");
part = formData.getFirst("baz");
boolean condition = part instanceof FormFieldPart;
assertThat(condition).isTrue();
formFieldPart = (FormFieldPart) part;
assertThat(formFieldPart.value()).isEqualTo("qux");
}).verifyComplete();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class ProtobufEncoder method encodeValue.
private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, boolean delimited) {
DataBuffer buffer = bufferFactory.allocateBuffer();
boolean release = true;
try {
if (delimited) {
message.writeDelimitedTo(buffer.asOutputStream());
} else {
message.writeTo(buffer.asOutputStream());
}
release = false;
return buffer;
} catch (IOException ex) {
throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex);
} finally {
if (release) {
DataBufferUtils.release(buffer);
}
}
}
Aggregations