Java Code Examples for org.springframework.core.codec.StringDecoder

The following examples show how to use org.springframework.core.codec.StringDecoder. These examples are extracted from open source projects. 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
private MessageMappingMessageHandler initMesssageHandler() {

		List<Decoder<?>> decoders = Collections.singletonList(StringDecoder.allMimeTypes());
		List<Encoder<?>> encoders = Collections.singletonList(CharSequenceEncoder.allMimeTypes());

		ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
		this.returnValueHandler = new TestEncoderMethodReturnValueHandler(encoders, registry);

		PropertySource<?> source = new MapPropertySource("test", Collections.singletonMap("path", "path123"));

		StaticApplicationContext context = new StaticApplicationContext();
		context.getEnvironment().getPropertySources().addFirst(source);
		context.registerSingleton("testController", TestController.class);
		context.refresh();

		MessageMappingMessageHandler messageHandler = new MessageMappingMessageHandler();
		messageHandler.getReturnValueHandlerConfigurer().addCustomHandler(this.returnValueHandler);
		messageHandler.setApplicationContext(context);
		messageHandler.setEmbeddedValueResolver(new EmbeddedValueResolver(context.getBeanFactory()));
		messageHandler.setDecoders(decoders);
		messageHandler.afterPropertiesSet();

		return messageHandler;
	}
 
Example 2
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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 3
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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()));
	given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);

	Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
	Mono<List<String>> result = resultFlux.collectList();
	assertEquals(Collections.singletonList("foo"), result.block());
}
 
Example 4
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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()));
	given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);

	Flux<String> resultFlux =
			defaultClientResponse.bodyToFlux(new ParameterizedTypeReference<String>() {
			});
	Mono<List<String>> result = resultFlux.collectList();
	assertEquals(Collections.singletonList("foo"), result.block());
}
 
Example 5
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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 6
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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 7
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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()));
	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 8
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: 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 9
Source Project: spring-analysis-note   Source File: BaseDefaultCodecs.java    License: MIT License 6 votes vote down vote up
/**
 * Return readers that support specific types.
 */
final List<HttpMessageReader<?>> getTypedReaders() {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageReader<?>> readers = new ArrayList<>();
	readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
	readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
	readers.add(new DecoderHttpMessageReader<>(new DataBufferDecoder()));
	readers.add(new ResourceHttpMessageReader());
	readers.add(new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
	if (protobufPresent) {
		Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder();
		readers.add(new DecoderHttpMessageReader<>(decoder));
	}

	FormHttpMessageReader formReader = new FormHttpMessageReader();
	formReader.setEnableLoggingRequestDetails(this.enableLoggingRequestDetails);
	readers.add(formReader);

	extendTypedReaders(readers);

	return readers;
}
 
Example 10
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 11
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 12
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 13
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 14
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 15
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 16
Source Project: java-technology-stack   Source File: DefaultClientResponseTests.java    License: 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 17
Source Project: java-technology-stack   Source File: BaseDefaultCodecs.java    License: MIT License 6 votes vote down vote up
/**
 * Return readers that support specific types.
 */
final List<HttpMessageReader<?>> getTypedReaders() {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageReader<?>> readers = new ArrayList<>();
	readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
	readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
	readers.add(new DecoderHttpMessageReader<>(new DataBufferDecoder()));
	readers.add(new DecoderHttpMessageReader<>(new ResourceDecoder()));
	readers.add(new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
	if (protobufPresent) {
		Decoder<?> decoder = this.protobufDecoder != null ? this.protobufDecoder : new ProtobufDecoder();
		readers.add(new DecoderHttpMessageReader<>(decoder));
	}

	FormHttpMessageReader formReader = new FormHttpMessageReader();
	formReader.setEnableLoggingRequestDetails(this.enableLoggingRequestDetails);
	readers.add(formReader);

	extendTypedReaders(readers);

	return readers;
}
 
Example 18
Source Project: spring-analysis-note   Source File: RSocketBufferLeakTests.java    License: MIT License 5 votes vote down vote up
@Bean
public RSocketStrategies rsocketStrategies() {
	return RSocketStrategies.builder()
			.decoder(StringDecoder.allMimeTypes())
			.encoder(CharSequenceEncoder.allMimeTypes())
			.dataBufferFactory(new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
			.build();
}
 
Example 19
@Bean
public RSocketStrategies rsocketStrategies() {
	return RSocketStrategies.builder()
			.decoder(StringDecoder.allMimeTypes())
			.encoder(CharSequenceEncoder.allMimeTypes())
			.dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
			.build();
}
 
Example 20
@Before
public void setUp() {
	RSocketStrategies strategies = RSocketStrategies.builder()
			.decoder(StringDecoder.allMimeTypes())
			.encoder(CharSequenceEncoder.allMimeTypes())
			.build();
	this.rsocket = new TestRSocket();
	this.requester = RSocketRequester.wrap(this.rsocket, MimeTypeUtils.TEXT_PLAIN, strategies);
}
 
Example 21
@Bean
public RSocketStrategies rsocketStrategies() {
	return RSocketStrategies.builder()
			.decoder(StringDecoder.allMimeTypes())
			.encoder(CharSequenceEncoder.allMimeTypes())
			.dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT))
			.build();
}
 
Example 22
Source Project: spring-analysis-note   Source File: BodyExtractorsTests.java    License: MIT License 5 votes vote down vote up
@Before
public void createContext() {
	final List<HttpMessageReader<?>> messageReaders = new ArrayList<>();
	messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
	messageReaders.add(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	messageReaders.add(new DecoderHttpMessageReader<>(new Jaxb2XmlDecoder()));
	messageReaders.add(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
	messageReaders.add(new FormHttpMessageReader());
	SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
	messageReaders.add(partReader);
	messageReaders.add(new MultipartHttpMessageReader(partReader));

	messageReaders.add(new FormHttpMessageReader());

	this.context = new BodyExtractor.Context() {
		@Override
		public List<HttpMessageReader<?>> messageReaders() {
			return messageReaders;
		}

		@Override
		public Optional<ServerHttpResponse> serverResponse() {
			return serverResponse;
		}

		@Override
		public Map<String, Object> hints() {
			return hints;
		}
	};
	this.hints = new HashMap<String, Object>();
}
 
Example 23
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: MIT License 5 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);
	mockTextPlainResponse(body);

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

	Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
	assertEquals("foo", resultMono.block());
}
 
Example 24
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: MIT License 5 votes vote down vote up
@Test
public void toEntityWithUnknownStatusCode() throws Exception {
	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);
	given(mockResponse.getHeaders()).willReturn(httpHeaders);
	given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
	given(mockResponse.getRawStatusCode()).willReturn(999);
	given(mockResponse.getBody()).willReturn(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());
	try {
		result.getStatusCode();
		fail("Expected IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// do nothing
	}
	assertEquals(999, result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example 25
Source Project: spring-analysis-note   Source File: DefaultClientResponseTests.java    License: MIT License 5 votes vote down vote up
@Test
public void toEntityListWithUnknownStatusCode() throws Exception {
	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);
	given(mockResponse.getHeaders()).willReturn(httpHeaders);
	given(mockResponse.getStatusCode()).willThrow(new IllegalArgumentException("999"));
	given(mockResponse.getRawStatusCode()).willReturn(999);
	given(mockResponse.getBody()).willReturn(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());
	try {
		result.getStatusCode();
		fail("Expected IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// do nothing
	}
	assertEquals(999, result.getStatusCodeValue());
	assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
 
Example 26
@Before
public void createRequest() {
	MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com/path").build();
	MockServerWebExchange webExchange = MockServerWebExchange.from(request);
	webExchange.getAttributes().put("exchange", "bar");

	this.request = new DefaultServerRequest(webExchange,
			Collections.singletonList(
					new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
}
 
Example 27
@Override
protected void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
	configurer.registerDefaults(false);
	configurer.customCodecs().decoder(StringDecoder.textPlainOnly());
	configurer.customCodecs().decoder(new Jaxb2XmlDecoder());
	configurer.customCodecs().encoder(CharSequenceEncoder.textPlainOnly());
	configurer.customCodecs().encoder(new Jaxb2XmlEncoder());
}
 
Example 28
Source Project: spring-analysis-note   Source File: BaseDefaultCodecs.java    License: MIT License 5 votes vote down vote up
/**
 * Return readers that need to be at the end, after all others.
 */
final List<HttpMessageReader<?>> getCatchAllReaders() {
	if (!this.registerDefaults) {
		return Collections.emptyList();
	}
	List<HttpMessageReader<?>> result = new ArrayList<>();
	result.add(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
	return result;
}
 
Example 29
Source Project: spring-analysis-note   Source File: ServerCodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) {
	assertEquals(StringDecoder.class, decoder.getClass());
	assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
	assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM));

	Flux<String> flux = (Flux<String>) decoder.decode(
			Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))),
			ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap());

	assertEquals(Arrays.asList("line1", "line2"), flux.collectList().block(Duration.ZERO));
}
 
Example 30
Source Project: spring-analysis-note   Source File: ClientCodecConfigurerTests.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) {
	assertEquals(StringDecoder.class, decoder.getClass());
	assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
	assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM));

	Flux<String> decoded = (Flux<String>) decoder.decode(
			Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))),
			ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap());

	assertEquals(Arrays.asList("line1", "line2"), decoded.collectList().block(Duration.ZERO));
}