|
15 | 15 | package org.htmlunit;
|
16 | 16 |
|
17 | 17 | import java.io.IOException;
|
| 18 | +import java.io.OutputStream; |
| 19 | +import java.io.OutputStreamWriter; |
18 | 20 | import java.io.Writer;
|
19 | 21 | import java.util.HashMap;
|
20 | 22 | import java.util.Map;
|
@@ -104,6 +106,68 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse
|
104 | 106 | }
|
105 | 107 | }
|
106 | 108 |
|
| 109 | + /** |
| 110 | + * @throws Exception if the test fails |
| 111 | + */ |
| 112 | + @Test |
| 113 | + public void noContentTypeXhtml() throws Exception { |
| 114 | + final Map<String, Class<? extends Servlet>> servlets = new HashMap<>(); |
| 115 | + servlets.put("/test", NoContentTypeXhtmlServlet.class); |
| 116 | + startWebServer("./", null, servlets); |
| 117 | + |
| 118 | + final WebClient client = getWebClient(); |
| 119 | + final XHtmlPage page = client.getPage(URL_FIRST + "test"); |
| 120 | + assertNotNull(page); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Servlet for {@link #noContentTypeLargeXhtmlHeader()}. |
| 125 | + */ |
| 126 | + public static class NoContentTypeXhtmlServlet extends HttpServlet { |
| 127 | + /** {@inheritDoc} */ |
| 128 | + @Override |
| 129 | + protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { |
| 130 | + final Writer writer = response.getWriter(); |
| 131 | + writer.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" |
| 132 | + + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
| 133 | + + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n" |
| 134 | + + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"lt\" lang=\"lt\">\r\n" |
| 135 | + + "<body>Hello World</body>\r\n" |
| 136 | + + "</html>"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @throws Exception if the test fails |
| 142 | + */ |
| 143 | + @Test |
| 144 | + public void noContentTypeXhtmlLeadingBlank() throws Exception { |
| 145 | + final Map<String, Class<? extends Servlet>> servlets = new HashMap<>(); |
| 146 | + servlets.put("/test", NoContentTypeXhtmlLeadingBlankServlet.class); |
| 147 | + startWebServer("./", null, servlets); |
| 148 | + |
| 149 | + final WebClient client = getWebClient(); |
| 150 | + final XHtmlPage page = client.getPage(URL_FIRST + "test"); |
| 151 | + assertNotNull(page); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Servlet for {@link #noContentTypeLargeXhtmlHeader()}. |
| 156 | + */ |
| 157 | + public static class NoContentTypeXhtmlLeadingBlankServlet extends HttpServlet { |
| 158 | + /** {@inheritDoc} */ |
| 159 | + @Override |
| 160 | + protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { |
| 161 | + final Writer writer = response.getWriter(); |
| 162 | + writer.write(" <?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" |
| 163 | + + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
| 164 | + + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n" |
| 165 | + + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"lt\" lang=\"lt\">\r\n" |
| 166 | + + "<body>Hello World</body>\r\n" |
| 167 | + + "</html>"); |
| 168 | + } |
| 169 | + } |
| 170 | + |
107 | 171 | /**
|
108 | 172 | * @throws Exception if the test fails
|
109 | 173 | */
|
@@ -260,4 +324,61 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse
|
260 | 324 | writer.write("<html><head><title>\u00d3</title></head><body></body></html>");
|
261 | 325 | }
|
262 | 326 | }
|
| 327 | + |
| 328 | + /** |
| 329 | + * @throws Exception if the test fails |
| 330 | + */ |
| 331 | + @Test |
| 332 | + public void noContentTypeBomUtf8() throws Exception { |
| 333 | + final Map<String, Class<? extends Servlet>> servlets = new HashMap<>(); |
| 334 | + servlets.put("/test", NoContentTypeBomUtf8Servlet.class); |
| 335 | + startWebServer("./", null, servlets); |
| 336 | + |
| 337 | + final WebClient client = getWebClient(); |
| 338 | + final TextPage page = client.getPage(URL_FIRST + "test"); |
| 339 | + assertNotNull(page); |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * Servlet for {@link #noContentTypeBomUtf8()}. |
| 344 | + */ |
| 345 | + public static class NoContentTypeBomUtf8Servlet extends HttpServlet { |
| 346 | + /** {@inheritDoc} */ |
| 347 | + @Override |
| 348 | + protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { |
| 349 | + final Writer writer = response.getWriter(); |
| 350 | + writer.write("\u00ef\u00bb\u00bf<html><head></head><body></body></html>"); |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + /** |
| 355 | + * @throws Exception if the test fails |
| 356 | + */ |
| 357 | + @Test |
| 358 | + public void noContentTypeBomUtf16() throws Exception { |
| 359 | + final Map<String, Class<? extends Servlet>> servlets = new HashMap<>(); |
| 360 | + servlets.put("/test", NoContentTypeBomUtf16Servlet.class); |
| 361 | + startWebServer("./", null, servlets); |
| 362 | + |
| 363 | + final WebClient client = getWebClient(); |
| 364 | + final TextPage page = client.getPage(URL_FIRST + "test"); |
| 365 | + assertNotNull(page); |
| 366 | + } |
| 367 | + |
| 368 | + /** |
| 369 | + * Servlet for {@link #noContentTypeBomUtf16()}. |
| 370 | + */ |
| 371 | + public static class NoContentTypeBomUtf16Servlet extends HttpServlet { |
| 372 | + /** {@inheritDoc} */ |
| 373 | + @Override |
| 374 | + protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { |
| 375 | + final OutputStream output = response.getOutputStream(); |
| 376 | + output.write('\u00fe'); |
| 377 | + output.write('\u00ff'); |
| 378 | + output.flush(); |
| 379 | + final Writer writer = new OutputStreamWriter(output, "UTF16"); |
| 380 | + writer.write("<html><head></head><body></body></html>"); |
| 381 | + writer.flush(); |
| 382 | + } |
| 383 | + } |
263 | 384 | }
|
0 commit comments