StringDecoder shouldn't chop off strings randomly
Issue: SPR-16337
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -28,8 +31,7 @@ import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
@@ -38,7 +40,7 @@ import static org.junit.Assert.assertTrue;
|
||||
*/
|
||||
public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private StringDecoder decoder = StringDecoder.allMimeTypes(true);
|
||||
private StringDecoder decoder = StringDecoder.allMimeTypes();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -56,36 +58,88 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() throws InterruptedException {
|
||||
this.decoder = StringDecoder.allMimeTypes(false);
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
|
||||
public void decodeMultibyteCharacter() {
|
||||
String s = "üéø";
|
||||
Flux<DataBuffer> source = toSingleByteDataBuffers(s);
|
||||
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class),
|
||||
null, Collections.emptyMap());
|
||||
StepVerifier.create(output)
|
||||
.expectNext(s)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
private Flux<DataBuffer> toSingleByteDataBuffers(String s) {
|
||||
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
List<DataBuffer> dataBuffers = new ArrayList<>();
|
||||
for (byte b : bytes) {
|
||||
dataBuffers.add(this.bufferFactory.wrap(new byte[]{b}));
|
||||
}
|
||||
return Flux.fromIterable(dataBuffers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeNewLine() {
|
||||
Flux<DataBuffer> source = Flux.just(
|
||||
stringBuffer("\r\nabc\n"),
|
||||
stringBuffer("def"),
|
||||
stringBuffer("ghi\r\n\n"),
|
||||
stringBuffer("jkl"),
|
||||
stringBuffer("mno\npqr\n"),
|
||||
stringBuffer("stu"),
|
||||
stringBuffer("vw"),
|
||||
stringBuffer("xyz")
|
||||
);
|
||||
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class),
|
||||
null, Collections.emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.expectNext("foo", "bar", "baz")
|
||||
.expectNext("")
|
||||
.expectNext("abc")
|
||||
.expectNext("defghi")
|
||||
.expectNext("")
|
||||
.expectNext("jklmno")
|
||||
.expectNext("pqr")
|
||||
.expectNext("stuvwxyz")
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeNewLine() throws InterruptedException {
|
||||
DataBuffer fooBar = stringBuffer("\nfoo\r\nbar\r");
|
||||
DataBuffer baz = stringBuffer("\nbaz");
|
||||
Flux<DataBuffer> source = Flux.just(fooBar, baz);
|
||||
Flux<String> output = decoder.decode(source, ResolvableType.forClass(String.class),
|
||||
public void decodeNewLineIncludeDelimiters() {
|
||||
|
||||
decoder = StringDecoder.allMimeTypes(StringDecoder.DEFAULT_DELIMITERS, false);
|
||||
|
||||
Flux<DataBuffer> source = Flux.just(
|
||||
stringBuffer("\r\nabc\n"),
|
||||
stringBuffer("def"),
|
||||
stringBuffer("ghi\r\n\n"),
|
||||
stringBuffer("jkl"),
|
||||
stringBuffer("mno\npqr\n"),
|
||||
stringBuffer("stu"),
|
||||
stringBuffer("vw"),
|
||||
stringBuffer("xyz")
|
||||
);
|
||||
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class),
|
||||
null, Collections.emptyMap());
|
||||
|
||||
StepVerifier.create(output)
|
||||
.expectNext("\n", "foo\r", "\n", "bar\r", "\n", "baz")
|
||||
.expectNext("\r\n")
|
||||
.expectNext("abc\n")
|
||||
.expectNext("defghi\r\n")
|
||||
.expectNext("\n")
|
||||
.expectNext("jklmno\n")
|
||||
.expectNext("pqr\n")
|
||||
.expectNext("stuvwxyz")
|
||||
.expectComplete()
|
||||
.verify();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeEmptyFlux() throws InterruptedException {
|
||||
public void decodeEmptyFlux() {
|
||||
Flux<DataBuffer> source = Flux.empty();
|
||||
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class),
|
||||
null, Collections.emptyMap());
|
||||
@@ -98,7 +152,7 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeEmptyString() throws InterruptedException {
|
||||
public void decodeEmptyDataBuffer() {
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer(""));
|
||||
Flux<String> output = this.decoder.decode(source,
|
||||
ResolvableType.forClass(String.class), null, Collections.emptyMap());
|
||||
@@ -110,8 +164,7 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeToMono() throws InterruptedException {
|
||||
this.decoder = StringDecoder.allMimeTypes(false);
|
||||
public void decodeToMono() {
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
|
||||
Mono<String> output = this.decoder.decodeToMono(source,
|
||||
ResolvableType.forClass(String.class), null, Collections.emptyMap());
|
||||
|
||||
@@ -492,5 +492,28 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
|
||||
release(composite);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByte() {
|
||||
DataBuffer buffer = stringBuffer("abc");
|
||||
|
||||
assertEquals('a', buffer.getByte(0));
|
||||
assertEquals('b', buffer.getByte(1));
|
||||
assertEquals('c', buffer.getByte(2));
|
||||
try {
|
||||
buffer.getByte(-1);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
}
|
||||
catch (IndexOutOfBoundsException ignored) {
|
||||
}
|
||||
|
||||
try {
|
||||
buffer.getByte(3);
|
||||
fail("IndexOutOfBoundsException expected");
|
||||
}
|
||||
catch (IndexOutOfBoundsException ignored) {
|
||||
}
|
||||
|
||||
release(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -335,5 +335,4 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
|
||||
release(result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user