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 { |
| 31 super.setUp(); |
| 32 |
| 33 jsEngine.setWebRequest(new AndroidWebRequest()); |
| 34 } |
| 35 |
| 36 @Test |
| 37 public void testRealWebRequest() |
| 38 { |
| 39 // This URL should redirect to easylist-downloads.adblockplus.org and we |
| 40 // should get the actual filter list back. |
| 41 jsEngine.evaluate( |
| 42 "_webRequest.GET('https://easylist-downloads.adblockplus.org/easylist.txt'
, {}, " + |
| 43 "function(result) {foo = result;} )"); |
| 44 do |
| 45 { |
| 46 try |
| 47 { |
| 48 Thread.sleep(200); |
| 49 } |
| 50 catch (InterruptedException e) |
| 51 { |
| 52 throw new RuntimeException(e); |
| 53 } |
| 54 } while (jsEngine.evaluate("this.foo").isUndefined()); |
| 55 |
| 56 String response = jsEngine.evaluate("foo.responseText").asString(); |
| 57 assertNotNull(response); |
| 58 assertEquals( |
| 59 ServerResponse.NsStatus.OK.getStatusCode(), |
| 60 jsEngine.evaluate("foo.status").asLong()); |
| 61 assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); |
| 62 assertEquals( |
| 63 "[Adblock Plus ", |
| 64 jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); |
| 65 JsValue jsHeaders = jsEngine.evaluate("foo.responseHeaders"); |
| 66 assertNotNull(jsHeaders); |
| 67 assertFalse(jsHeaders.isUndefined()); |
| 68 assertFalse(jsHeaders.isNull()); |
| 69 assertTrue(jsHeaders.isObject()); |
| 70 assertEquals( |
| 71 "text/plain", |
| 72 jsEngine.evaluate("foo.responseHeaders['Content-Type'].substr(0, 10)").asS
tring()); |
| 73 assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefined(
)); |
| 74 } |
| 75 |
| 76 @Test |
| 77 public void testXMLHttpRequest() |
| 78 { |
| 79 jsEngine.evaluate( |
| 80 "var result;\n" + |
| 81 "var request = new XMLHttpRequest();\n" + |
| 82 "request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.
txt');\n" + |
| 83 "request.setRequestHeader('X', 'Y');\n" + |
| 84 "request.setRequestHeader('X2', 'Y2');\n" + |
| 85 "request.overrideMimeType('text/plain');\n" + |
| 86 "request.addEventListener('load',function() {result=request.responseText;}
, false);\n" + |
| 87 "request.addEventListener('error',function() {result='error';}, false);\n"
+ |
| 88 "request.send(null);"); |
| 89 |
| 90 do |
| 91 { |
| 92 try |
| 93 { |
| 94 Thread.sleep(200); |
| 95 } |
| 96 catch (InterruptedException e) |
| 97 { |
| 98 throw new RuntimeException(e); |
| 99 } |
| 100 } while (jsEngine.evaluate("result").isUndefined()); |
| 101 |
| 102 assertEquals( |
| 103 ServerResponse.NsStatus.OK.getStatusCode(), |
| 104 jsEngine.evaluate("request.channel.status").asLong()); |
| 105 |
| 106 assertEquals(200l, jsEngine.evaluate("request.status").asLong()); |
| 107 assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)").asS
tring()); |
| 108 assertEquals( |
| 109 "text/plain", |
| 110 jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(0, 10)
").asString()); |
| 111 assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").isNull
()); |
| 112 } |
| 113 } |
OLD | NEW |