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;
16  
17  import static java.nio.charset.StandardCharsets.UTF_8;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.nio.charset.Charset;
23  
24  import org.apache.commons.io.IOUtils;
25  import org.apache.commons.io.input.BOMInputStream;
26  import org.junit.Test;
27  
28  /**
29   * Unit tests for {@link StringWebResponse}.
30   *
31   * @author Marc Guillemot
32   * @author Carsten Steul
33   * @author Ronald Brill
34   */
35  public class StringWebResponseTest extends SimpleWebTestCase {
36  
37      /**
38       * Regression test for bug 2998004.
39       */
40      @Test
41      public void charset() {
42          final StringWebResponse webResponse = new StringWebResponse("hello", UTF_8, URL_FIRST);
43          assertSame(UTF_8, webResponse.getContentCharset());
44      }
45  
46      /**
47       * @throws Exception if the test fails
48       */
49      @Test
50      public void charsetImplicit() {
51          final StringWebResponse webResponse = new StringWebResponse("hello", URL_FIRST);
52          assertSame(UTF_8, webResponse.getContentCharset());
53      }
54  
55      /**
56       * Regression test for bug #1660.
57       */
58      @Test
59      public void charsetInContent() {
60          final String content = DOCTYPE_HTML
61                  + "<html><head>\n"
62                  + "<meta http-equiv='Content-Type' content='text/html; charset=windows-1250' />\n"
63                  + "</head><body>\u010C\u00CDSLO</body></html>";
64          final StringWebResponse webResponse = new StringWebResponse(content, UTF_8, URL_FIRST);
65  
66          assertSame(UTF_8, webResponse.getContentCharset());
67          assertEquals(content, webResponse.getContentAsString());
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      public void charsetInContentImplicit() {
75          final String content = DOCTYPE_HTML
76                  + "<html><head>\n"
77                  + "<meta http-equiv='Content-Type' content='text/html; charset=windows-1250' />\n"
78                  + "</head><body>\u010C\u00CDSLO</body></html>";
79          final StringWebResponse webResponse = new StringWebResponse(content, URL_FIRST);
80  
81          assertSame(UTF_8, webResponse.getContentCharset());
82          assertEquals(content, webResponse.getContentAsString());
83      }
84  
85      /**
86       * @throws IOException in case of error
87       */
88      @Test
89      public void inputStream() throws IOException {
90          final String content = DOCTYPE_HTML
91                  + "<html><head>\n"
92                  + "<meta http-equiv='Content-Type' content='text/html; charset=windows-1250' />\n"
93                  + "</head><body>\u010C\u00CDSLO</body></html>";
94          final StringWebResponse webResponse = new StringWebResponse(content, URL_FIRST);
95  
96          Charset charset = webResponse.getContentCharset();
97          try (InputStream is = webResponse.getContentAsStreamWithBomIfApplicable()) {
98              if (is instanceof BOMInputStream) {
99                  final String bomCharsetName = ((BOMInputStream) is).getBOMCharsetName();
100                 if (bomCharsetName != null) {
101                     charset = Charset.forName(bomCharsetName);
102                 }
103             }
104 
105             try (InputStreamReader reader = new InputStreamReader(is, charset)) {
106                 final String read = IOUtils.toString(reader);
107 
108                 assertSame(UTF_8, charset);
109                 assertEquals(content, read);
110             }
111         }
112     }
113 }