Skip to content

Commit 93b8601

Browse files
Add tests for StandardHtmlEncodingDetector content-encoding, EncodingResult fields, and markLimit (#2917)
1 parent 1cd1e25 commit 93b8601

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tika-encoding-detectors/tika-encoding-detector-html/src/test/java/org/apache/tika/parser/html/StandardHtmlEncodingDetectorTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,58 @@ public void withCharsetInContentType() throws IOException {
297297
assertWindows1252("<meta http-equiv='content-type' content='charset=utf-8'>");
298298
}
299299

300+
@Test
301+
public void withCharsetInContentEncoding() throws IOException {
302+
// When Content-Type is absent, the charset should be read from Content-Encoding
303+
Metadata meta = new Metadata();
304+
meta.set(Metadata.CONTENT_ENCODING, "UTF-8");
305+
StandardHtmlEncodingDetector detector = new StandardHtmlEncodingDetector();
306+
TikaInputStream tis = TikaInputStream.get("".getBytes(StandardCharsets.UTF_8));
307+
List<EncodingResult> results = detector.detect(tis, meta, new ParseContext());
308+
assertEquals(1, results.size());
309+
assertEquals(StandardCharsets.UTF_8, results.get(0).getCharset());
310+
assertEquals(1.0f, results.get(0).getConfidence(), 0.0f);
311+
assertEquals("UTF-8", results.get(0).getLabel());
312+
}
313+
314+
@Test
315+
public void encodingResultFields() throws IOException {
316+
// A declarative charset yields confidence 1.0, the charset name as the label,
317+
// and a DECLARATIVE result type
318+
StandardHtmlEncodingDetector detector = new StandardHtmlEncodingDetector();
319+
TikaInputStream tis = TikaInputStream.get(
320+
"<meta charset='UTF-8'>".getBytes(StandardCharsets.UTF_8));
321+
List<EncodingResult> results = detector.detect(tis, metadata, new ParseContext());
322+
assertEquals(1, results.size());
323+
EncodingResult result = results.get(0);
324+
assertEquals(StandardCharsets.UTF_8, result.getCharset());
325+
assertEquals(1.0f, result.getConfidence(), 0.0f);
326+
assertEquals("UTF-8", result.getLabel());
327+
assertEquals(EncodingResult.ResultType.DECLARATIVE, result.getResultType());
328+
}
329+
330+
@Test
331+
public void defaultMarkLimit() throws IOException {
332+
// The default mark limit is 64 KiB (65536 bytes)
333+
StandardHtmlEncodingDetector detector = new StandardHtmlEncodingDetector();
334+
assertEquals(65536, detector.getMarkLimit());
335+
}
336+
337+
@Test
338+
public void customMarkLimit() throws IOException {
339+
// setMarkLimit should change the limit returned by getMarkLimit
340+
StandardHtmlEncodingDetector detector = new StandardHtmlEncodingDetector();
341+
detector.setMarkLimit(100);
342+
assertEquals(100, detector.getMarkLimit());
343+
344+
// The meta tag starts at byte 80, within the 100-byte limit, but the charset value
345+
// is truncated at byte 100 before it can be parsed, so no charset is detected
346+
StringBuilder sb = new StringBuilder();
347+
for (int i = 0; i < 80; i++) sb.append("x");
348+
sb.append("<meta charset='WINDOWS-1252'>");
349+
assertCharset(sb.toString(), null, detector);
350+
}
351+
300352
@Test
301353
public void throwResistance() throws IOException {
302354
// The preprocessing should return right after having found the charset

0 commit comments

Comments
 (0)