1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.encoding;
16
17 import static java.nio.charset.StandardCharsets.ISO_8859_1;
18 import static java.nio.charset.StandardCharsets.UTF_8;
19
20 import java.nio.charset.Charset;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Locale;
23
24 import org.apache.commons.io.ByteOrderMark;
25 import org.apache.commons.lang3.ArrayUtils;
26 import org.htmlunit.WebDriverTestCase;
27 import org.htmlunit.javascript.host.xml.XMLHttpRequest;
28 import org.htmlunit.util.MimeType;
29
30
31
32
33
34
35 public abstract class AbstractXMLHttpRequestEncodingTest extends WebDriverTestCase {
36
37
38 protected static final String BOM_UTF_16LE = "BOMUTF16LE";
39
40 protected static final String BOM_UTF_16BE = "BOMUTF16BE";
41
42 protected static final String BOM_UTF_8 = "BOMUTF8";
43
44
45
46
47 public enum TestCharset {
48
49 UTF8("UTF8", UTF_8),
50
51 ISO88591("ISO88591", ISO_8859_1),
52
53 WINDOWS1250("WINDOWS1250", Charset.forName("windows-1250")),
54
55 GB2312("GB2312", Charset.forName("GB2312"));
56
57 private final String label_;
58 private final Charset charset_;
59
60 TestCharset(final String label, final Charset charset) {
61 label_ = label;
62 charset_ = charset;
63 }
64
65 @Override
66 public String toString() {
67 return label_;
68 }
69
70
71
72
73 public Charset getCharset() {
74 return charset_;
75 }
76 }
77
78
79
80
81 public enum TestMimeType {
82
83 EMPTY("EMPTY", ""),
84
85 XML("XML", MimeType.TEXT_XML),
86
87 PLAIN("PLAIN", MimeType.TEXT_PLAIN);
88
89 private final String label_;
90 private final String mimeType_;
91
92 TestMimeType(final String label, final String mimeType) {
93 label_ = label;
94 mimeType_ = mimeType;
95 }
96
97 @Override
98 public String toString() {
99 return label_;
100 }
101
102
103
104
105 public String getMimeType() {
106 return mimeType_;
107 }
108 }
109
110 protected void setupXmlResponse(final String xml, final String bom, final TestMimeType mimeTypeXml,
111 final TestCharset charsetXmlResponseHeader) {
112 if (BOM_UTF_8.equals(bom)) {
113 final byte[] xmlBytes =
114 ArrayUtils.addAll(ByteOrderMark.UTF_8.getBytes(), xml.getBytes(StandardCharsets.UTF_8));
115 getMockWebConnection().setResponse(URL_SECOND, xmlBytes, 200, "OK", mimeTypeXml.getMimeType(), null);
116 return;
117 }
118 if (BOM_UTF_16BE.equals(bom)) {
119 final byte[] xmlBytes =
120 ArrayUtils.addAll(ByteOrderMark.UTF_16BE.getBytes(), xml.getBytes(StandardCharsets.UTF_16BE));
121 getMockWebConnection().setResponse(URL_SECOND, xmlBytes, 200, "OK", mimeTypeXml.getMimeType(), null);
122 return;
123 }
124 if (BOM_UTF_16LE.equals(bom)) {
125 final byte[] xmlBytes =
126 ArrayUtils.addAll(ByteOrderMark.UTF_16LE.getBytes(), xml.getBytes(StandardCharsets.UTF_16LE));
127 getMockWebConnection().setResponse(URL_SECOND, xmlBytes, 200, "OK", mimeTypeXml.getMimeType(), null);
128 return;
129 }
130 getMockWebConnection().setResponse(URL_SECOND, xml, mimeTypeXml.getMimeType(),
131 charsetXmlResponseHeader == null ? null : charsetXmlResponseHeader.getCharset());
132 }
133
134 protected static String escape(final String str) {
135 final StringBuilder res = new StringBuilder();
136 for (final char c : str.toCharArray()) {
137 res.append("\\u").append(String.format("%04X", (int) c).toLowerCase(Locale.ROOT));
138 }
139 return res.toString();
140 }
141 }