View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Base class for for several {@link XMLHttpRequest} encoding tests.
32   *
33   * @author Ronald Brill
34   */
35  public abstract class AbstractXMLHttpRequestEncodingTest extends WebDriverTestCase {
36  
37      /** "BOMUTF16LE". */
38      protected static final String BOM_UTF_16LE = "BOMUTF16LE";
39      /** "BOMUTF16BE". */
40      protected static final String BOM_UTF_16BE = "BOMUTF16BE";
41      /** "BOMUTF8". */
42      protected static final String BOM_UTF_8 = "BOMUTF8";
43  
44      /**
45       * Enum for data driven tests.
46       */
47      public enum TestCharset {
48          /** utf 8. */
49          UTF8("UTF8", UTF_8),
50          /** iso 8859 1. */
51          ISO88591("ISO88591", ISO_8859_1),
52          /** windows-1250. */
53          WINDOWS1250("WINDOWS1250", Charset.forName("windows-1250")),
54          /** gb 2312. */
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           * @return the {@link Charset}
72           */
73          public Charset getCharset() {
74              return charset_;
75          }
76      }
77  
78      /**
79       * Enum for data driven tests.
80       */
81      public enum TestMimeType {
82          /** empty. */
83          EMPTY("EMPTY", ""),
84          /** xml. */
85          XML("XML", MimeType.TEXT_XML),
86          /** plain. */
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          * @return the mime type
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 }