org.springframework.core.io.buffer.DefaultDataBufferFactory Java Examples

The following examples show how to use org.springframework.core.io.buffer.DefaultDataBufferFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultClientResponseTests.java    From spring-analysis-note with MIT License 8 votes vote down vote up
@Test
public void toEntityList() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);

	ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
	assertEquals(Collections.singletonList("foo"), result.getBody());
	assertEquals(HttpStatus.OK, result.getStatusCode());
	assertEquals(HttpStatus.OK.value(), result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example #2
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bodyToFlux() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	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);

	Flux<String> resultFlux = request.bodyToFlux(String.class);
	assertEquals(Collections.singletonList("foo"), resultFlux.collectList().block());
}
 
Example #3
Source File: DefaultServerRequestTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyToMono() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.TEXT_PLAIN);
	MockServerHttpRequest mockRequest = MockServerHttpRequest
			.method(HttpMethod.GET, "http://example.com?foo=bar")
			.headers(httpHeaders)
			.body(body);
	DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);

	Mono<String> resultMono = request.bodyToMono(String.class);
	assertEquals("foo", resultMono.block());
}
 
Example #4
Source File: DefaultClientResponseTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bodyToMonoTypeReference() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);

	Mono<String> resultMono =
			defaultClientResponse.bodyToMono(new ParameterizedTypeReference<String>() {
			});
	assertEquals("foo", resultMono.block());
}
 
Example #5
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toMonoParameterizedTypeReference() {
	BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor =
			BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {});

	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
	Mono<Map<String, String>> result = extractor.extract(request, this.context);

	Map<String, String > expected = new LinkedHashMap<>();
	expected.put("username", "foo");
	expected.put("password", "bar");
	StepVerifier.create(result)
			.expectNext(expected)
			.expectComplete()
			.verify();
}
 
Example #6
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toMonoWithHints() {
	BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
	this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);

	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/")
			.contentType(MediaType.APPLICATION_JSON)
			.body(body);

	Mono<User> result = extractor.extract(request, this.context);

	StepVerifier.create(result)
			.consumeNextWith(user -> {
				assertEquals("foo", user.getUsername());
				assertNull(user.getPassword());
			})
			.expectComplete()
			.verify();
}
 
Example #7
Source File: DefaultClientResponseTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toEntityTypeReference() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);

	ResponseEntity<String> result = defaultClientResponse.toEntity(
			new ParameterizedTypeReference<String>() {
			}).block();
	assertEquals("foo", result.getBody());
	assertEquals(HttpStatus.OK, result.getStatusCode());
	assertEquals(HttpStatus.OK.value(), result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example #8
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toDataBuffers() {
	BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();

	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
	Flux<DataBuffer> result = extractor.extract(request, this.context);

	StepVerifier.create(result)
			.expectNext(dataBuffer)
			.expectComplete()
			.verify();
}
 
Example #9
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toFormData() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	String text = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
	DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/")
			.contentType(MediaType.APPLICATION_FORM_URLENCODED)
			.body(body);

	Mono<MultiValueMap<String, String>> result = BodyExtractors.toFormData().extract(request, this.context);

	StepVerifier.create(result)
			.consumeNextWith(form -> {
				assertEquals("Invalid result", 3, form.size());
				assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
				List<String> values = form.get("name 2");
				assertEquals("Invalid result", 2, values.size());
				assertEquals("Invalid result", "value 2+1", values.get(0));
				assertEquals("Invalid result", "value 2+2", values.get(1));
				assertNull("Invalid result", form.getFirst("name 3"));
			})
			.expectComplete()
			.verify();
}
 
Example #10
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toMonoVoidAsClientShouldConsumeAndCancel() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	TestPublisher<DataBuffer> body = TestPublisher.create();

	BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.setBody(body.flux());

	StepVerifier.create(extractor.extract(response, this.context))
			.then(() -> {
				body.assertWasSubscribed();
				body.emit(dataBuffer);
			})
			.verifyComplete();

	body.assertCancelled();
}
 
Example #11
Source File: DefaultServerRequestTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyToMonoDecodingException() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.APPLICATION_JSON);
	MockServerHttpRequest mockRequest = MockServerHttpRequest
			.method(HttpMethod.POST, "http://example.com/invalid")
			.headers(httpHeaders)
			.body(body);
	DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);

	Mono<Map<String, String>> resultMono = request.bodyToMono(
			new ParameterizedTypeReference<Map<String, String>>() {});
	StepVerifier.create(resultMono)
			.expectError(ServerWebInputException.class)
			.verify();
}
 
Example #12
Source File: DefaultClientResponseTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toEntityListTypeReference() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);

	ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(
			new ParameterizedTypeReference<String>() {
			}).block();
	assertEquals(Collections.singletonList("foo"), result.getBody());
	assertEquals(HttpStatus.OK, result.getStatusCode());
	assertEquals(HttpStatus.OK.value(), result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example #13
Source File: ExchangeFilterFunctionsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void limitResponseSize() {
	DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
	DataBuffer b1 = dataBuffer("foo", bufferFactory);
	DataBuffer b2 = dataBuffer("bar", bufferFactory);
	DataBuffer b3 = dataBuffer("baz", bufferFactory);

	ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
	ClientResponse response = ClientResponse.create(HttpStatus.OK).body(Flux.just(b1, b2, b3)).build();

	Mono<ClientResponse> result = ExchangeFilterFunctions.limitResponseSize(5)
			.filter(request, req -> Mono.just(response));

	StepVerifier.create(result.flatMapMany(res -> res.body(BodyExtractors.toDataBuffers())))
			.consumeNextWith(buffer -> assertEquals("foo", string(buffer)))
			.consumeNextWith(buffer -> assertEquals("ba", string(buffer)))
			.expectComplete()
			.verify();

}
 
Example #14
Source File: ExchangeFilterFunctionsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void limitResponseSize() {
	DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
	DataBuffer b1 = dataBuffer("foo", bufferFactory);
	DataBuffer b2 = dataBuffer("bar", bufferFactory);
	DataBuffer b3 = dataBuffer("baz", bufferFactory);

	ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
	ClientResponse response = ClientResponse.create(HttpStatus.OK).body(Flux.just(b1, b2, b3)).build();

	Mono<ClientResponse> result = ExchangeFilterFunctions.limitResponseSize(5)
			.filter(request, req -> Mono.just(response));

	StepVerifier.create(result.flatMapMany(res -> res.body(BodyExtractors.toDataBuffers())))
			.consumeNextWith(buffer -> assertEquals("foo", string(buffer)))
			.consumeNextWith(buffer -> assertEquals("ba", string(buffer)))
			.expectComplete()
			.verify();

}
 
Example #15
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bodyToMono() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	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.bodyToMono(String.class);
	assertEquals("foo", resultMono.block());
}
 
Example #16
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void bodyUnacceptable() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	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();
}
 
Example #17
Source File: BodyInsertersTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void ofDataBuffers() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromDataBuffers(body);

	MockServerHttpResponse response = new MockServerHttpResponse();
	Mono<Void> result = inserter.insert(response, this.context);
	StepVerifier.create(result).expectComplete().verify();

	StepVerifier.create(response.getBody())
			.expectNext(dataBuffer)
			.expectComplete()
			.verify();
}
 
Example #18
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void formData() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo=bar&baz=qux".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
	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, String>> resultData = request.formData();
	StepVerifier.create(resultData)
			.consumeNextWith(formData -> {
				assertEquals(2, formData.size());
				assertEquals("bar", formData.getFirst("foo"));
				assertEquals("qux", formData.getFirst("baz"));
			})
			.verifyComplete();
}
 
Example #19
Source File: BodyInsertersTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void ofDataBuffers() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromDataBuffers(body);

	MockServerHttpResponse response = new MockServerHttpResponse();
	Mono<Void> result = inserter.insert(response, this.context);
	StepVerifier.create(result).expectComplete().verify();

	StepVerifier.create(response.getBody())
			.expectNext(dataBuffer)
			.expectComplete()
			.verify();
}
 
Example #20
Source File: DefaultClientResponseTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toEntityList() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders);

	ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
	assertEquals(Collections.singletonList("foo"), result.getBody());
	assertEquals(HttpStatus.OK, result.getStatusCode());
	assertEquals(HttpStatus.OK.value(), result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example #21
Source File: DefaultClientResponseTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toEntityTypeReference() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders);

	ResponseEntity<String> result = defaultClientResponse.toEntity(
			new ParameterizedTypeReference<String>() {
			}).block();
	assertEquals("foo", result.getBody());
	assertEquals(HttpStatus.OK, result.getStatusCode());
	assertEquals(HttpStatus.OK.value(), result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example #22
Source File: DefaultClientResponseTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toEntity() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders);

	ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
	assertEquals("foo", result.getBody());
	assertEquals(HttpStatus.OK, result.getStatusCode());
	assertEquals(HttpStatus.OK.value(), result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example #23
Source File: DefaultClientResponseTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyToFluxTypeReference() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);
	mockTextPlainResponse(body);

	List<HttpMessageReader<?>> messageReaders = Collections
			.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders);

	Flux<String> resultFlux =
			defaultClientResponse.bodyToFlux(new ParameterizedTypeReference<String>() {
			});
	Mono<List<String>> result = resultFlux.collectList();
	assertEquals(Collections.singletonList("foo"), result.block());
}
 
Example #24
Source File: HttpFileTransferIT.java    From vertx-spring-boot with Apache License 2.0 6 votes vote down vote up
private static Flux<DataBuffer> readFile(Vertx vertx, Path path) {
    AsyncFile file = vertx.fileSystem().openBlocking(path.toString(), new OpenOptions());

    Flux<Buffer> buffers = Flux.create(sink -> {
        file.pause();
        file.endHandler(v -> sink.complete());
        file.exceptionHandler(sink::error);
        file.handler(sink::next);
        sink.onRequest(file::fetch);
    });

    DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();

    return Flux.from(buffers)
        .map(Buffer::getBytes)
        .map(dataBufferFactory::wrap);
}
 
Example #25
Source File: WebFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testWebFilter() throws Exception {

	WebFilter filter = (exchange, chain) -> {
		DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
		buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
		return exchange.getResponse().writeWith(Mono.just(buffer));
	};

	WebTestClient client = WebTestClient.bindToWebHandler(exchange -> Mono.empty())
			.webFilter(filter)
			.build();

	client.get().uri("/")
			.exchange()
			.expectStatus().isOk()
			.expectBody(String.class).isEqualTo("It works!");
}
 
Example #26
Source File: BodyExtractorsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toMono() {
	BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);

	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
	Mono<String> result = extractor.extract(request, this.context);

	StepVerifier.create(result)
			.expectNext("foo")
			.expectComplete()
			.verify();
}
 
Example #27
Source File: DefaultServerRequestTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void formData() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo=bar&baz=qux".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
	MockServerHttpRequest mockRequest = MockServerHttpRequest
			.method(HttpMethod.GET, "http://example.com")
			.headers(httpHeaders)
			.body(body);
	DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());

	Mono<MultiValueMap<String, String>> resultData = request.formData();
	StepVerifier.create(resultData)
			.consumeNextWith(formData -> {
				assertEquals(2, formData.size());
				assertEquals("bar", formData.getFirst("foo"));
				assertEquals("qux", formData.getFirst("baz"));
			})
			.verifyComplete();
}
 
Example #28
Source File: BodyExtractorsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toMonoVoidAsClientShouldConsumeAndCancel() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	TestPublisher<DataBuffer> body = TestPublisher.create();

	BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.setBody(body.flux());

	StepVerifier.create(extractor.extract(response, this.context))
			.then(() -> {
				body.assertWasSubscribed();
				body.emit(dataBuffer);
			})
			.verifyComplete();

	body.assertCancelled();
}
 
Example #29
Source File: BodyExtractorsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toFlux() {
	BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);

	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	DefaultDataBuffer dataBuffer =
			factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
	Flux<String> result = extractor.extract(request, this.context);

	StepVerifier.create(result)
			.expectNext("foo")
			.expectComplete()
			.verify();
}
 
Example #30
Source File: BodyExtractorsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toFormData() {
	DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
	String text = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
	DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
	Flux<DataBuffer> body = Flux.just(dataBuffer);

	MockServerHttpRequest request = MockServerHttpRequest.post("/")
			.contentType(MediaType.APPLICATION_FORM_URLENCODED)
			.body(body);

	Mono<MultiValueMap<String, String>> result = BodyExtractors.toFormData().extract(request, this.context);

	StepVerifier.create(result)
			.consumeNextWith(form -> {
				assertEquals("Invalid result", 3, form.size());
				assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
				List<String> values = form.get("name 2");
				assertEquals("Invalid result", 2, values.size());
				assertEquals("Invalid result", "value 2+1", values.get(0));
				assertEquals("Invalid result", "value 2+2", values.get(1));
				assertNull("Invalid result", form.getFirst("name 3"));
			})
			.expectComplete()
			.verify();
}