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

The following examples show how to use org.springframework.core.io.buffer.DefaultDataBuffer. 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: 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 #3
Source File: BodyExtractorsTests.java    From spring-analysis-note 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 #4
Source File: DefaultClientResponseTests.java    From spring-analysis-note 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()));
	given(mockExchangeStrategies.messageReaders()).willReturn(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 #5
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 #6
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 #7
Source File: PayloadUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a Payload from the given metadata and data.
 * @param metadata the metadata part for the payload
 * @param data the data part for the payload
 * @return the created Payload
 */
public static Payload createPayload(DataBuffer metadata, DataBuffer data) {
	if (metadata instanceof NettyDataBuffer && data instanceof NettyDataBuffer) {
		return ByteBufPayload.create(
				((NettyDataBuffer) data).getNativeBuffer(),
				((NettyDataBuffer) metadata).getNativeBuffer());
	}
	else if (metadata instanceof DefaultDataBuffer && data instanceof DefaultDataBuffer) {
		return DefaultPayload.create(
				((DefaultDataBuffer) data).getNativeBuffer(),
				((DefaultDataBuffer) metadata).getNativeBuffer());
	}
	else {
		return DefaultPayload.create(data.asByteBuffer(), metadata.asByteBuffer());
	}
}
 
Example #8
Source File: BodyExtractorsTests.java    From java-technology-stack 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 #9
Source File: BodyExtractorsTests.java    From java-technology-stack 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 #10
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 #11
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 #12
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();
}
 
Example #13
Source File: BodyExtractorsTests.java    From java-technology-stack 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 #14
Source File: DefaultClientResponseTests.java    From java-technology-stack 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()));
	when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders);

	Mono<String> resultMono =
			defaultClientResponse.bodyToMono(new ParameterizedTypeReference<String>() {
			});
	assertEquals("foo", resultMono.block());
}
 
Example #15
Source File: DefaultClientResponseTests.java    From java-technology-stack 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);
	mockTextPlainResponse(body);

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

	Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
	Mono<List<String>> result = resultFlux.collectList();
	assertEquals(Collections.singletonList("foo"), result.block());
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
Source File: DefaultClientResponseTests.java    From java-technology-stack 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()));
	when(mockExchangeStrategies.messageReaders()).thenReturn(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 #21
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 #22
Source File: DefaultServerRequestTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyToMonoParameterizedTypeReference() {
	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);

	ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
	Mono<String> resultMono = request.bodyToMono(typeReference);
	assertEquals("foo", resultMono.block());
}
 
Example #23
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 #24
Source File: DefaultServerRequestTests.java    From java-technology-stack 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, "http://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 #25
Source File: DefaultServerRequestTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void bodyToFluxParameterizedTypeReference() {
	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);

	ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
	Flux<String> resultFlux = request.bodyToFlux(typeReference);
	assertEquals(Collections.singletonList("foo"), resultFlux.collectList().block());
}
 
Example #26
Source File: DefaultServerRequestTests.java    From java-technology-stack 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, "http://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 #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: 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 #29
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingDefaultDataBufferFactory_PooledHttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new DefaultDataBufferFactory());

    final PooledHttpData httpData1 =
            PooledHttpData.wrap(Unpooled.wrappedBuffer("abc".getBytes()));

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(DefaultDataBuffer.class);
    assertThat(httpData1.refCnt()).isZero();
    assertThat(buffer.asByteBuffer()).isEqualTo(ByteBuffer.wrap("abc".getBytes()));

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
    assertThat(ByteBufUtil.getBytes(((PooledHttpData) httpData2).content())).isEqualTo("abc".getBytes());
}
 
Example #30
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingDefaultDataBufferFactory_HttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new DefaultDataBufferFactory());

    final HttpData httpData1 = HttpData.ofUtf8("abc");

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(DefaultDataBuffer.class);
    assertThat(buffer.asByteBuffer()).isEqualTo(ByteBuffer.wrap("abc".getBytes()));

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
    assertThat(ByteBufUtil.getBytes(((PooledHttpData) httpData2).content())).isEqualTo("abc".getBytes());
}