Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/DefaultWebRequestTest.java |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/DefaultWebRequestTest.java |
@@ -0,0 +1,96 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+package org.adblockplus.libadblockplus.tests; |
+ |
+import org.adblockplus.android.AndroidWebRequest; |
+import org.adblockplus.libadblockplus.ServerResponse; |
+ |
+import org.junit.Test; |
+ |
+public class DefaultWebRequestTest extends BaseJsTest { |
+ |
+ @Override |
+ protected void setUp() throws Exception { |
+ super.setUp(); |
+ jsEngine.setWebRequest(new AndroidWebRequest()); |
+ } |
+ |
+ @Test |
+ public void testRealWebRequest() { |
+ // This URL should redirect to easylist-downloads.adblockplus.org and we |
+ // should get the actual filter list back. |
+ jsEngine.evaluate( |
+ "_webRequest.GET('https://easylist-downloads.adblockplus.org/easylist.txt', {}, " + |
+ "function(result) {foo = result;} )"); |
+ do { |
+ try { |
+ Thread.sleep(200); |
+ } catch (InterruptedException e) { |
+ throw new RuntimeException(e); |
+ } |
+ } while (jsEngine.evaluate("this.foo").isUndefined()); |
+ |
+ String response = jsEngine.evaluate("foo.responseText").asString(); |
+ assertNotNull(response); |
+ assertEquals( |
+ ServerResponse.NsStatus.OK.getStatusCode(), |
+ jsEngine.evaluate("foo.status").asLong()); |
+ assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); |
+ assertEquals( |
+ "[Adblock Plus ", |
+ jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); |
+ assertTrue(jsEngine.evaluate("foo.responseHeaders.length").asLong() > 0); |
+ assertEquals( |
+ "text/plain", |
+ jsEngine.evaluate("foo.responseHeaders['content-type']").asString()); |
+ assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefined()); |
+ } |
+ |
+ @Test |
+ public void testXMLHttpRequest() { |
+ jsEngine.evaluate( |
+ "var result;\n" + |
+ "var request = new XMLHttpRequest();\n" + |
+ "request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt');\n" + |
+ "request.setRequestHeader('X', 'Y');\n" + |
+ "request.setRequestHeader('X2', 'Y2');\n" + |
+ "request.overrideMimeType('text/plain');\n" + |
+ "request.addEventListener('load',function() {result=request.responseText;}, false);\n" + |
+ "request.addEventListener('error',function() {result='error';}, false);\n" + |
+ "request.send(null);"); |
+ |
+ do { |
+ try { |
+ Thread.sleep(200); |
+ } catch (InterruptedException e) { |
+ throw new RuntimeException(e); |
+ } |
+ } while (jsEngine.evaluate("result").isUndefined()); |
+ |
+ assertEquals( |
+ ServerResponse.NsStatus.OK.getStatusCode(), |
+ jsEngine.evaluate("request.channel.status").asLong()); |
+ |
+ assertEquals(200l, jsEngine.evaluate("request.status").asLong()); |
+ assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)").asString()); |
+ assertEquals( |
+ "text/plain", |
+ jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(0,10)").asString()); |
+ assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").isNull()); |
+ } |
+} |