Skip to content

Commit 55044ff

Browse files
committed
test(appsec): migrate MultipartContentDecoderTest from Groovy/Spock to Java/JUnit 5
1 parent e74778b commit 55044ff

2 files changed

Lines changed: 116 additions & 115 deletions

File tree

internal-api/src/test/groovy/datadog/trace/api/http/MultipartContentDecoderTest.groovy

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package datadog.trace.api.http;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import java.nio.charset.Charset;
7+
import java.nio.charset.StandardCharsets;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.CsvSource;
11+
import org.junit.jupiter.params.provider.NullAndEmptySource;
12+
import org.junit.jupiter.params.provider.ValueSource;
13+
14+
public class MultipartContentDecoderTest {
15+
16+
@Test
17+
void decodeBytesUsesDeclaredUtf8Charset() {
18+
String text = "héllo wörld";
19+
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
20+
assertEquals(
21+
text,
22+
MultipartContentDecoder.decodeBytes(bytes, bytes.length, "text/plain; charset=UTF-8"));
23+
}
24+
25+
@Test
26+
void decodeBytesUsesDeclaredIso88591Charset() {
27+
String text = "café";
28+
byte[] bytes = text.getBytes(StandardCharsets.ISO_8859_1);
29+
assertEquals(
30+
text,
31+
MultipartContentDecoder.decodeBytes(bytes, bytes.length, "text/plain; charset=ISO-8859-1"));
32+
}
33+
34+
@Test
35+
void decodeBytesDefaultsToMachineDefaultWhenNoCharset() {
36+
String text = "hello world";
37+
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
38+
assertEquals(text, MultipartContentDecoder.decodeBytes(bytes, bytes.length, "text/plain"));
39+
}
40+
41+
@Test
42+
void decodeBytesDefaultsToMachineDefaultWhenNullContentType() {
43+
String text = "hello world";
44+
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
45+
assertEquals(text, MultipartContentDecoder.decodeBytes(bytes, bytes.length, null));
46+
}
47+
48+
@Test
49+
void decodeBytesRespectsLengthParameter() {
50+
byte[] bytes = "hello world".getBytes(StandardCharsets.UTF_8);
51+
assertEquals("hello", MultipartContentDecoder.decodeBytes(bytes, 5, null));
52+
}
53+
54+
@Test
55+
void decodeBytesReturnsEmptyStringForZeroLength() {
56+
assertEquals("", MultipartContentDecoder.decodeBytes(new byte[16], 0, null));
57+
}
58+
59+
@Test
60+
void decodeBytesUsesMachineDefaultFallbackWhenBytesCannotBeDecoded() {
61+
byte[] bytes = "café".getBytes(StandardCharsets.ISO_8859_1);
62+
String expected = new String(bytes, Charset.defaultCharset());
63+
assertEquals(expected, MultipartContentDecoder.decodeBytes(bytes, bytes.length, null));
64+
}
65+
66+
@Test
67+
void decodeBytesUsesMachineDefaultFallbackWhenDeclaredCharsetCannotDecodeBytes() {
68+
byte[] bytes = "café".getBytes(StandardCharsets.ISO_8859_1);
69+
String expected = new String(bytes, Charset.defaultCharset());
70+
assertEquals(
71+
expected,
72+
MultipartContentDecoder.decodeBytes(bytes, bytes.length, "text/plain; charset=UTF-8"));
73+
}
74+
75+
@ParameterizedTest
76+
@NullAndEmptySource
77+
void extractCharsetReturnsNullForNullOrEmptyContentType(String contentType) {
78+
assertNull(MultipartContentDecoder.extractCharset(contentType));
79+
}
80+
81+
@ParameterizedTest
82+
@ValueSource(strings = {"text/plain", "image/jpeg", "application/octet-stream"})
83+
void extractCharsetReturnsNullForContentTypeWithoutCharset(String contentType) {
84+
assertNull(MultipartContentDecoder.extractCharset(contentType));
85+
}
86+
87+
@Test
88+
void extractCharsetReturnsNullForInvalidCharsetName() {
89+
assertNull(MultipartContentDecoder.extractCharset("text/plain; charset=NOTACHARSET"));
90+
}
91+
92+
@ParameterizedTest
93+
@ValueSource(
94+
strings = {
95+
"text/plain; CHARSET=UTF-8",
96+
"text/plain; Charset=UTF-8",
97+
"text/plain; charset=utf-8"
98+
})
99+
void extractCharsetIsCaseInsensitive(String contentType) {
100+
assertEquals("UTF-8", MultipartContentDecoder.extractCharset(contentType).name());
101+
}
102+
103+
@ParameterizedTest
104+
@CsvSource({"text/plain; charset=UTF-8, UTF-8", "text/xml; charset=ISO-8859-1, ISO-8859-1"})
105+
void extractCharsetFromStandardContentType(String contentType, String expectedCharset) {
106+
assertEquals(expectedCharset, MultipartContentDecoder.extractCharset(contentType).name());
107+
}
108+
109+
@Test
110+
void extractCharsetHandlesAdditionalParameters() {
111+
assertEquals(
112+
"UTF-8",
113+
MultipartContentDecoder.extractCharset("text/plain; charset=UTF-8; boundary=something")
114+
.name());
115+
}
116+
}

0 commit comments

Comments
 (0)