OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.libadblockplus.tests; |
| 19 |
| 20 import org.adblockplus.android.AndroidWebRequest; |
| 21 import org.adblockplus.libadblockplus.JsValue; |
| 22 import org.adblockplus.libadblockplus.ServerResponse; |
| 23 |
| 24 import org.junit.Test; |
| 25 |
| 26 public class AndroidWebRequestTest extends BaseJsTest { |
| 27 |
| 28 @Override |
| 29 protected void setUp() throws Exception { |
| 30 super.setUp(); |
| 31 |
| 32 jsEngine.setWebRequest(new AndroidWebRequest()); |
| 33 } |
| 34 |
| 35 @Test |
| 36 public void testRealWebRequest() { |
| 37 // This URL should redirect to easylist-downloads.adblockplus.org and we |
| 38 // should get the actual filter list back. |
| 39 jsEngine.evaluate( |
| 40 "_webRequest.GET('https://easylist-downloads.adblockplus.org/easylis
t.txt', {}, " + |
| 41 "function(result) {foo = result;} )"); |
| 42 do { |
| 43 try { |
| 44 Thread.sleep(200); |
| 45 } catch (InterruptedException e) { |
| 46 throw new RuntimeException(e); |
| 47 } |
| 48 } while (jsEngine.evaluate("this.foo").isUndefined()); |
| 49 |
| 50 String response = jsEngine.evaluate("foo.responseText").asString(); |
| 51 assertNotNull(response); |
| 52 assertEquals( |
| 53 ServerResponse.NsStatus.OK.getStatusCode(), |
| 54 jsEngine.evaluate("foo.status").asLong()); |
| 55 assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); |
| 56 assertEquals( |
| 57 "[Adblock Plus ", |
| 58 jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); |
| 59 JsValue jsHeaders = jsEngine.evaluate("foo.responseHeaders"); |
| 60 assertNotNull(jsHeaders); |
| 61 assertFalse(jsHeaders.isUndefined()); |
| 62 assertFalse(jsHeaders.isNull()); |
| 63 assertTrue(jsHeaders.isObject()); |
| 64 assertEquals( |
| 65 "text/plain", |
| 66 jsEngine.evaluate("foo.responseHeaders['Content-Type'].substr(0,10)"
).asString()); |
| 67 assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefi
ned()); |
| 68 } |
| 69 |
| 70 @Test |
| 71 public void testXMLHttpRequest() { |
| 72 jsEngine.evaluate( |
| 73 "var result;\n" + |
| 74 "var request = new XMLHttpRequest();\n" + |
| 75 "request.open('GET', 'https://easylist-downloads.adblockplus.org/eas
ylist.txt');\n" + |
| 76 "request.setRequestHeader('X', 'Y');\n" + |
| 77 "request.setRequestHeader('X2', 'Y2');\n" + |
| 78 "request.overrideMimeType('text/plain');\n" + |
| 79 "request.addEventListener('load',function() {result=request.response
Text;}, false);\n" + |
| 80 "request.addEventListener('error',function() {result='error';}, fals
e);\n" + |
| 81 "request.send(null);"); |
| 82 |
| 83 do { |
| 84 try { |
| 85 Thread.sleep(200); |
| 86 } catch (InterruptedException e) { |
| 87 throw new RuntimeException(e); |
| 88 } |
| 89 } while (jsEngine.evaluate("result").isUndefined()); |
| 90 |
| 91 assertEquals( |
| 92 ServerResponse.NsStatus.OK.getStatusCode(), |
| 93 jsEngine.evaluate("request.channel.status").asLong()); |
| 94 |
| 95 assertEquals(200l, jsEngine.evaluate("request.status").asLong()); |
| 96 assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)")
.asString()); |
| 97 assertEquals( |
| 98 "text/plain", |
| 99 jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(
0,10)").asString()); |
| 100 assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").is
Null()); |
| 101 } |
| 102 } |
OLD | NEW |