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.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.htmlunit.html.HtmlPage;
25 import org.htmlunit.junit.BrowserRunner;
26 import org.htmlunit.util.MimeType;
27 import org.htmlunit.util.mocks.WebResponseMock;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30
31
32
33
34
35
36
37 @RunWith(BrowserRunner.class)
38 public class WebResponse2Test extends SimpleWebTestCase {
39
40
41
42
43 @Test
44 public void charsetInMetaTag() throws Exception {
45 final String html = DOCTYPE_HTML
46 + "<html>\n"
47 + "<head><meta content='text/html; charset=utf-8' http-equiv='Content-Type'/></head>\n"
48 + "<body>foo</body>\n"
49 + "</html>";
50 final HtmlPage page = loadPage(html);
51 assertSame(UTF_8, page.getWebResponse().getContentCharset());
52 assertEquals(html, page.getWebResponse().getContentAsString());
53 }
54
55
56
57
58 @Test
59 public void getHeaderContentCharset() {
60 testHeaderContentCharset(StandardCharsets.UTF_8, MimeType.TEXT_HTML + ";charset=utf-8");
61
62 testHeaderContentCharset(null, " \t ;charset=utf-8");
63 testHeaderContentCharset(null, ";charset=utf-8");
64 testHeaderContentCharset(null, "charset=utf-8");
65 }
66
67 private static void testHeaderContentCharset(final Charset expected, final String contentTypeHeader) {
68 final Map<String, String> headers = new HashMap<>();
69 headers.put(HttpHeader.CONTENT_TYPE, contentTypeHeader);
70
71 final WebResponseMock response = new WebResponseMock(null, headers);
72 assertEquals(expected, response.getHeaderContentCharset());
73 }
74 }