From: <rb...@us...> - 2017-05-14 16:26:00
|
Revision: 14453 http://sourceforge.net/p/htmlunit/code/14453 Author: rbri Date: 2017-05-14 16:25:58 +0000 (Sun, 14 May 2017) Log Message: ----------- Various fixes for javascript encoding detection Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/ScriptElementSupport.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/EncodingSniffer.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-05-14 07:59:08 UTC (rev 14452) +++ trunk/htmlunit/src/changes/changes.xml 2017-05-14 16:25:58 UTC (rev 14453) @@ -8,8 +8,11 @@ <body> <release version="2.27" date="???" description="GAE broken, Bugfixes"> + <action type="fix" dev="rbri"> + Various fixes for javascript encoding detection. + </action> <action type="fix" dev="rbri" issue="1881" due-to="Carsten Steul"> - NPE in StyleSheetList.equivalentValues() + NPE in StyleSheetList.equivalentValues(). </action> <action type="add" dev="asashour" issue="43854916" system="stackoverflow"> HtmlImageInput: add .saveAs(). Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java 2017-05-14 07:59:08 UTC (rev 14452) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java 2017-05-14 16:25:58 UTC (rev 14453) @@ -92,6 +92,7 @@ import com.gargoylesoftware.htmlunit.javascript.host.event.Event2; import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument; import com.gargoylesoftware.htmlunit.protocol.javascript.JavaScriptURLConnection; +import com.gargoylesoftware.htmlunit.util.EncodingSniffer; import com.gargoylesoftware.htmlunit.util.UrlUtils; import com.gargoylesoftware.js.nashorn.api.scripting.ScriptObjectMirror; import com.gargoylesoftware.js.nashorn.internal.objects.Global; @@ -944,12 +945,13 @@ * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * @param srcAttribute the source attribute from the script tag + * @param scriptCharset the charset from the script tag * @return the result of loading the specified external JavaScript file * @throws FailingHttpStatusCodeException if the request's status code indicates a request * failure and the {@link WebClient} was configured to throw exceptions on failing * HTTP status codes */ - JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute) + JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final Charset scriptCharset) throws FailingHttpStatusCodeException { final WebClient client = getWebClient(); @@ -979,7 +981,7 @@ final Object script; try { - script = loadJavaScriptFromUrl(scriptURL); + script = loadJavaScriptFromUrl(scriptURL, scriptCharset); } catch (final IOException e) { client.getJavaScriptErrorListener().loadScriptError(this, scriptURL, e); @@ -1005,6 +1007,7 @@ * there is a problem loading the code from the specified URL. * * @param url the URL of the script + * @param scriptCharset the charset from the script tag * @return the content of the file, or {@code null} if we ran into a compile error * @throws IOException if there is a problem downloading the JavaScript file * @throws FailingHttpStatusCodeException if the request's status code indicates a request @@ -1011,10 +1014,9 @@ * failure and the {@link WebClient} was configured to throw exceptions on failing * HTTP status codes */ - private Object loadJavaScriptFromUrl(final URL url) throws IOException, + private Object loadJavaScriptFromUrl(final URL url, final Charset scriptCharset) throws IOException, FailingHttpStatusCodeException { - final Charset pageEncoding = getCharset(); final WebRequest referringRequest = getWebResponse().getWebRequest(); final WebClient client = getWebClient(); @@ -1066,17 +1068,17 @@ } } - final Charset scriptEncoding; - final Charset contentCharset = response.getContentCharset(); - if (!contentCharset.equals(ISO_8859_1)) { + Charset scriptEncoding = Charset.forName("windows-1252"); + final Charset contentCharset = EncodingSniffer.sniffEncodingFromHttpHeaders(response.getResponseHeaders()); + if (contentCharset == null) { + // use info from script tag or fall back to utf-8 + if (scriptCharset != null && scriptCharset != ISO_8859_1) { + scriptEncoding = scriptCharset; + } + } + else if (contentCharset != ISO_8859_1) { scriptEncoding = contentCharset; } - else if (!pageEncoding.equals(ISO_8859_1)) { - scriptEncoding = pageEncoding; - } - else { - scriptEncoding = ISO_8859_1; - } final String scriptCode = response.getContentAsString(scriptEncoding); if (null != scriptCode) { Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java 2017-05-14 07:59:08 UTC (rev 14452) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java 2017-05-14 16:25:58 UTC (rev 14453) @@ -371,7 +371,8 @@ } try { executed_ = true; - final JavaScriptLoadResult result = page.loadExternalJavaScriptFile(src); + final Charset charset = EncodingSniffer.toCharset(getCharsetAttribute()); + final JavaScriptLoadResult result = page.loadExternalJavaScriptFile(src, charset); if (result == JavaScriptLoadResult.SUCCESS) { executeEvent(Event.TYPE_LOAD); } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/ScriptElementSupport.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/ScriptElementSupport.java 2017-05-14 07:59:08 UTC (rev 14452) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/ScriptElementSupport.java 2017-05-14 16:25:58 UTC (rev 14453) @@ -19,6 +19,8 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_SCRIPT_SUPPORTS_FOR_AND_EVENT_WINDOW; import static com.gargoylesoftware.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED; +import java.nio.charset.Charset; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,6 +37,7 @@ import com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget; import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument; import com.gargoylesoftware.htmlunit.protocol.javascript.JavaScriptURLConnection; +import com.gargoylesoftware.htmlunit.util.EncodingSniffer; import com.gargoylesoftware.htmlunit.xml.XmlPage; import net.sourceforge.htmlunit.corejs.javascript.BaseFunction; @@ -137,7 +140,8 @@ try { final ScriptElement scriptElement = (ScriptElement) element; scriptElement.setExecuted(true); - final JavaScriptLoadResult result = page.loadExternalJavaScriptFile(src); + final Charset charset = EncodingSniffer.toCharset(scriptElement.getCharsetAttribute()); + final JavaScriptLoadResult result = page.loadExternalJavaScriptFile(src, charset); if (result == JavaScriptLoadResult.SUCCESS) { executeEvent(element, Event.TYPE_LOAD); } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/EncodingSniffer.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/EncodingSniffer.java 2017-05-14 07:59:08 UTC (rev 14452) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/EncodingSniffer.java 2017-05-14 16:25:58 UTC (rev 14453) @@ -614,7 +614,7 @@ * @return the encoding sniffed from the specified HTTP headers, or {@code null} if the encoding * could not be determined */ - static Charset sniffEncodingFromHttpHeaders(final List<NameValuePair> headers) { + public static Charset sniffEncodingFromHttpHeaders(final List<NameValuePair> headers) { Charset encoding = null; for (final NameValuePair pair : headers) { final String name = pair.getName(); Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java 2017-05-14 07:59:08 UTC (rev 14452) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlScript2Test.java 2017-05-14 16:25:58 UTC (rev 14453) @@ -21,10 +21,12 @@ import java.nio.charset.Charset; import org.apache.commons.io.ByteOrderMark; +import org.apache.commons.lang3.ArrayUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebDriverException; import com.gargoylesoftware.htmlunit.BrowserRunner; import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; @@ -416,9 +418,18 @@ * @throws Exception if the test fails */ @Test + @Alerts("أهلاً") + public void null_null_null_null() throws Exception { + charset(null, "", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test @Alerts("أهلاً") - public void isoCharsetWithUtfBom() throws Exception { - charsetWithBom(ISO_8859_1, ByteOrderMark.UTF_8); + public void null_null_null_UTF_8() throws Exception { + charset(null, "", null, ByteOrderMark.UTF_8); } /** @@ -425,9 +436,26 @@ * @throws Exception if the test fails */ @Test + public void null_null_null_UTF_16BE() throws Exception { + charset(null, "", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void null_null_UTF_8_null() throws Exception { + charset(null, "", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test @Alerts("أهلاً") - public void utfCharsetWithoutBom() throws Exception { - charsetWithBom(UTF_8, null); + public void null_null_UTF_8_UTF_8() throws Exception { + charset(null, "", UTF_8, ByteOrderMark.UTF_8); } /** @@ -434,23 +462,733 @@ * @throws Exception if the test fails */ @Test + public void null_null_UTF_8_UTF_16BE() throws Exception { + charset(null, "", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void null_null_ISO_8859_1_null() throws Exception { + charset(null, "", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void null_null_ISO_8859_1_UTF_8() throws Exception { + charset(null, "", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void null_null_ISO_8859_1_UTF_16BE() throws Exception { + charset(null, "", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test @Alerts("أهلاً") - public void isoCharsetWithoutBom() throws Exception { - charsetWithBom(ISO_8859_1, null); + public void null_UTF_8_null_null() throws Exception { + charset(null, "; charset=utf-8", null, null); } - private void charsetWithBom(final Charset charsetAttribute, final ByteOrderMark bom) throws Exception { - final String html + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void null_UTF_8_null_UTF_8() throws Exception { + charset(null, "; charset=utf-8", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void null_UTF_8_null_UTF_16BE() throws Exception { + charset(null, "; charset=utf-8", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void null_UTF_8_UTF_8_null() throws Exception { + charset(null, "; charset=utf-8", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void null_UTF_8_UTF_8_UTF_8() throws Exception { + charset(null, "; charset=utf-8", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void null_UTF_8_UTF_8_UTF_16BE() throws Exception { + charset(null, "; charset=utf-8", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void null_UTF_8_ISO_8859_1_null() throws Exception { + charset(null, "; charset=utf-8", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void null_UTF_8_ISO_8859_1_UTF_8() throws Exception { + charset(null, "; charset=utf-8", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void null_UTF_8_ISO_8859_1_UTF_16BE() throws Exception { + charset(null, "; charset=utf-8", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void null_ISO_8859_1_null_null() throws Exception { + charset(null, "; charset=iso-8859-1", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "أهلاً", + IE = "أهلاً") + public void null_ISO_8859_1_null_UTF_8() throws Exception { + charset(null, "; charset=iso-8859-1", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void null_ISO_8859_1_null_UTF_16BE() throws Exception { + charset(null, "", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void null_ISO_8859_1_UTF_8_null() throws Exception { + charset(null, "; charset=iso-8859-1", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "أهلاً", + IE = "أهلاً") + public void null_ISO_8859_1_UTF_8_UTF_8() throws Exception { + charset(null, "; charset=iso-8859-1", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void null_ISO_8859_1_UTF_8_UTF_16BE() throws Exception { + charset(null, "; charset=iso-8859-1", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void null_ISO_8859_1_ISO_8859_1_null() throws Exception { + charset(null, "; charset=iso-8859-1", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void null_ISO_8859_1_ISO_8859_1_UTF_8() throws Exception { + charset(null, "; charset=iso-8859-1", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "?????") + public void null_ISO_8859_1_ISO_8859_1_UTF_16BE() throws Exception { + charset(null, "; charset=iso-8859-1", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_null_null_null() throws Exception { + charset(UTF_8, "", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_null_null_UTF_8() throws Exception { + charset(UTF_8, "", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void UTF_8_null_null_UTF_16BE() throws Exception { + charset(UTF_8, "", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_null_UTF_8_null() throws Exception { + charset(UTF_8, "", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_null_UTF_8_UTF_8() throws Exception { + charset(UTF_8, "", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void UTF_8_null_UTF_8_UTF_16BE() throws Exception { + charset(UTF_8, "", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void UTF_8_null_ISO_8859_1_null() throws Exception { + charset(UTF_8, "", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void UTF_8_null_ISO_8859_1_UTF_8() throws Exception { + charset(UTF_8, "", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void UTF_8_null_ISO_8859_1_UTF_16BE() throws Exception { + charset(UTF_8, "", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_UTF_8_null_null() throws Exception { + charset(UTF_8, "; charset=utf-8", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_UTF_8_null_UTF_8() throws Exception { + charset(UTF_8, "; charset=utf-8", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "أهلاً") + public void UTF_8_UTF_8_null_UTF_16BE() throws Exception { + charset(UTF_8, "; charset=utf-8", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_UTF_8_UTF_8_null() throws Exception { + charset(UTF_8, "; charset=utf-8", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_UTF_8_UTF_8_UTF_8() throws Exception { + charset(UTF_8, "; charset=utf-8", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "أهلاً") + public void UTF_8_UTF_8_UTF_8_UTF_16BE() throws Exception { + charset(UTF_8, "; charset=utf-8", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void UTF_8_UTF_8_ISO_8859_1_null() throws Exception { + charset(UTF_8, "; charset=utf-8", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void UTF_8_UTF_8_ISO_8859_1_UTF_8() throws Exception { + charset(UTF_8, "; charset=utf-8", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "?????") + public void UTF_8_UTF_8_ISO_8859_1_UTF_16BE() throws Exception { + charset(UTF_8, "; charset=utf-8", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_ISO_8859_1_null_null() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "أهلاً", + IE = "أهلاً") + public void UTF_8_ISO_8859_1_null_UTF_8() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void UTF_8_ISO_8859_1_null_UTF_16BE() throws Exception { + charset(UTF_8, "", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void UTF_8_ISO_8859_1_UTF_8_null() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "أهلاً", + IE = "أهلاً") + public void UTF_8_ISO_8859_1_UTF_8_UTF_8() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "أهلاً") + public void UTF_8_ISO_8859_1_UTF_8_UTF_16BE() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void UTF_8_ISO_8859_1_ISO_8859_1_null() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void UTF_8_ISO_8859_1_ISO_8859_1_UTF_8() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "?????") + public void UTF_8_ISO_8859_1_ISO_8859_1_UTF_16BE() throws Exception { + charset(UTF_8, "; charset=iso-8859-1", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_null_null_null() throws Exception { + charset(ISO_8859_1, "", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_null_null_UTF_8() throws Exception { + charset(ISO_8859_1, "", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void ISO_8859_1_null_null_UTF_16BE() throws Exception { + charset(ISO_8859_1, "", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_null_UTF_8_null() throws Exception { + charset(ISO_8859_1, "", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_null_UTF_8_UTF_8() throws Exception { + charset(ISO_8859_1, "", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void ISO_8859_1_null_UTF_8_UTF_16BE() throws Exception { + charset(ISO_8859_1, "", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void ISO_8859_1_null_ISO_8859_1_null() throws Exception { + charset(ISO_8859_1, "", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void ISO_8859_1_null_ISO_8859_1_UTF_8() throws Exception { + charset(ISO_8859_1, "", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void ISO_8859_1_null_ISO_8859_1_UTF_16BE() throws Exception { + charset(ISO_8859_1, "", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_UTF_8_null_null() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_UTF_8_null_UTF_8() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "أهلاً") + public void ISO_8859_1_UTF_8_null_UTF_16BE() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_UTF_8_UTF_8_null() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_UTF_8_UTF_8_UTF_8() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "أهلاً") + public void ISO_8859_1_UTF_8_UTF_8_UTF_16BE() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void ISO_8859_1_UTF_8_ISO_8859_1_null() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void ISO_8859_1_UTF_8_ISO_8859_1_UTF_8() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "?????") + public void ISO_8859_1_UTF_8_ISO_8859_1_UTF_16BE() throws Exception { + charset(ISO_8859_1, "; charset=utf-8", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_ISO_8859_1_null_null() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", null, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "أهلاً", + IE = "أهلاً") + public void ISO_8859_1_ISO_8859_1_null_UTF_8() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", null, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void ISO_8859_1_ISO_8859_1_null_UTF_16BE() throws Exception { + charset(ISO_8859_1, "", null, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("أهلاً") + public void ISO_8859_1_ISO_8859_1_UTF_8_null() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", UTF_8, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "أهلاً", + IE = "أهلاً") + public void ISO_8859_1_ISO_8859_1_UTF_8_UTF_8() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", UTF_8, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "أهلاً") + public void ISO_8859_1_ISO_8859_1_UTF_8_UTF_16BE() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", UTF_8, ByteOrderMark.UTF_16BE); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void ISO_8859_1_ISO_8859_1_ISO_8859_1_null() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", ISO_8859_1, null); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("?????") + public void ISO_8859_1_ISO_8859_1_ISO_8859_1_UTF_8() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", ISO_8859_1, ByteOrderMark.UTF_8); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {}, + IE = "?????") + public void ISO_8859_1_ISO_8859_1_ISO_8859_1_UTF_16BE() throws Exception { + charset(ISO_8859_1, "; charset=iso-8859-1", ISO_8859_1, ByteOrderMark.UTF_16BE); + } + + private void charset(final Charset charsetAttribute, + final String charsetResponseHeader, + final Charset charsetResponseEncoding, + final ByteOrderMark bom) throws Exception { + + // use always a different url to avoid caching effects + final URL cssUrl = new URL(URL_SECOND, "" + System.currentTimeMillis() + ".js"); + + String html = "<html><head>\n" - + " <script src='" + URL_SECOND + "' charset='" + charsetAttribute + "'></script>\n" + + " <script src='" + cssUrl + "'"; + if (charsetAttribute != null) { + html = html + " charset='" + charsetAttribute + "'"; + } + html = html + "></script>\n" + "</head>\n" + "<body></body>\n" + "</html>"; - final String script = - (bom == null ? "" : new String(bom.getBytes())) - + "alert('أهلاً');"; - getMockWebConnection().setResponse(URL_SECOND, script, "application/javascript", UTF_8); - loadPageWithAlerts2(html); + final String js = "alert('أهلاً');"; + byte[] script = null; + if (charsetResponseEncoding == null) { + script = js.getBytes(UTF_8); + } + else { + script = js.getBytes(charsetResponseEncoding); + } + if (bom == null) { + getMockWebConnection().setResponse(cssUrl, script, 200, "OK", + "application/javascript" + charsetResponseHeader, null); + } + else { + script = ArrayUtils.addAll(bom.getBytes(), script); + getMockWebConnection().setResponse(cssUrl, script, 200, "OK", + "application/javascript" + charsetResponseHeader, null); + } + + try { + loadPageWithAlerts2(html); + } + catch (final WebDriverException e) { + if (!e.getCause().getMessage().contains("illegal character") + && !e.getCause().getMessage().contains("is not defined.")) { + throw e; + } + } } } |