1
2
3
4
5
6
7
8
9
10
11
12
13
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
30
31
32
33
34
35 public class StringWebResponseTest extends SimpleWebTestCase {
36
37
38
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
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
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
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
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 }