001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit;
039:
040: import java.util.ArrayList;
041: import java.util.Collections;
042: import java.util.List;
043:
044: import org.apache.commons.httpclient.HttpStatus;
045: import org.apache.commons.lang.ArrayUtils;
046:
047: import com.gargoylesoftware.htmlunit.html.HtmlPage;
048:
049: /**
050: * Tests for {@link WebResponseImpl}.
051: *
052: * @version $Revision: 2132 $
053: * @author Marc Guillemot
054: * @author Ahmed Ashour
055: */
056: public class WebResponseImplTest extends WebTestCase {
057:
058: /**
059: * Create an instance
060: * @param name The name of the test
061: */
062: public WebResponseImplTest(final String name) {
063: super (name);
064: }
065:
066: /**
067: * When no encoding header is provided, encoding may be recognized with its Byte Order Mark
068: * @throws Exception if the test fails
069: */
070: public void testRecognizeBOM() throws Exception {
071: testRecognizeBOM("UTF-8", new byte[] { (byte) 0xef,
072: (byte) 0xbb, (byte) 0xbf });
073: testRecognizeBOM("UTF-16BE", new byte[] { (byte) 0xfe,
074: (byte) 0xff });
075: testRecognizeBOM("UTF-16LE", new byte[] { (byte) 0xff,
076: (byte) 0xfe });
077: }
078:
079: private void testRecognizeBOM(final String encoding,
080: final byte[] markerBytes) throws Exception {
081: final WebClient webClient = new WebClient();
082:
083: final MockWebConnection webConnection = new MockWebConnection(
084: webClient);
085:
086: final String html = "<html><head><script src='foo.js'></script></head><body></body></html>";
087:
088: // see http://en.wikipedia.org/wiki/Byte_Order_Mark
089: final String[] expectedAlerts = { "\u6211\u662F\u6211\u7684 "
090: + "\u064A\u0627 \u0623\u0647\u0644\u0627\u064B" };
091: final byte[] script = ("alert('" + expectedAlerts[0] + "');")
092: .getBytes(encoding);
093:
094: webConnection.setDefaultResponse(ArrayUtils.addAll(markerBytes,
095: script), 200, "OK", "text/javascript");
096: webConnection.setResponse(URL_FIRST, html);
097: webClient.setWebConnection(webConnection);
098:
099: final List collectedAlerts = new ArrayList();
100: webClient.setAlertHandler(new CollectingAlertHandler(
101: collectedAlerts));
102:
103: webClient.getPage(URL_FIRST);
104:
105: assertEquals(expectedAlerts, collectedAlerts);
106: }
107:
108: /**
109: * @throws Exception If the test fails
110: */
111: public void testEncoding() throws Exception {
112: final String title = "\u6211\u662F\u6211\u7684FOCUS";
113: final String content = "<html><head>\n" + "<title>" + title
114: + "</title>\n" + "</head>\n" + "<body>\n"
115: + "</body></html>";
116:
117: final WebClient client = new WebClient();
118:
119: final MockWebConnection webConnection = new MockWebConnection(
120: client);
121: webConnection.setResponse(URL_FIRST, content.getBytes("UTF-8"),
122: 200, "OK", "text/html", Collections.EMPTY_LIST);
123: client.setWebConnection(webConnection);
124: final WebRequestSettings settings = new WebRequestSettings(
125: URL_FIRST);
126: settings.setCharset("UTF-8");
127: final HtmlPage page = (HtmlPage) client.getPage(settings);
128: assertEquals(title, page.getTitleText());
129: }
130:
131: /**
132: * @throws Exception If the test fails
133: */
134: public void testQuotedCharset() throws Exception {
135: final String xml = "<books id='myId'>\n" + " <book>\n"
136: + " <title>Immortality</title>\n"
137: + " <author>John Smith</author>\n" + " </book>\n"
138: + "</books>";
139:
140: final List collectedAlerts = new ArrayList();
141: final WebClient client = new WebClient();
142: client.setAlertHandler(new CollectingAlertHandler(
143: collectedAlerts));
144: final MockWebConnection conn = new MockWebConnection(client);
145: conn.setResponse(URL_FIRST, xml, HttpStatus.SC_OK, "OK",
146: "text/xml; charset=\"ISO-8859-1\"",
147: Collections.EMPTY_LIST);
148: client.setWebConnection(conn);
149: client.getPage(URL_FIRST);
150: }
151:
152: }
|