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.ServerResponse; |
| 22 |
| 23 import org.junit.Test; |
| 24 |
| 25 public class DefaultWebRequestTest extends BaseJsTest { |
| 26 |
| 27 @Override |
| 28 protected void setUp() throws Exception { |
| 29 super.setUp(); |
| 30 jsEngine.setWebRequest(new AndroidWebRequest()); |
| 31 } |
| 32 |
| 33 @Test |
| 34 public void testRealWebRequest() { |
| 35 // This URL should redirect to easylist-downloads.adblockplus.org and we |
| 36 // should get the actual filter list back. |
| 37 jsEngine.evaluate( |
| 38 "_webRequest.GET('https://easylist-downloads.adblockplus.org/easylis
t.txt', {}, " + |
| 39 "function(result) {foo = result;} )"); |
| 40 do { |
| 41 try { |
| 42 Thread.sleep(200); |
| 43 } catch (InterruptedException e) { |
| 44 throw new RuntimeException(e); |
| 45 } |
| 46 } while (jsEngine.evaluate("this.foo").isUndefined()); |
| 47 |
| 48 String response = jsEngine.evaluate("foo.responseText").asString(); |
| 49 assertNotNull(response); |
| 50 assertEquals( |
| 51 ServerResponse.NsStatus.OK.getStatusCode(), |
| 52 jsEngine.evaluate("foo.status").asLong()); |
| 53 assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); |
| 54 assertEquals( |
| 55 "[Adblock Plus ", |
| 56 jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); |
| 57 assertTrue(jsEngine.evaluate("foo.responseHeaders.length").asLong() > 0)
; |
| 58 assertEquals( |
| 59 "text/plain", |
| 60 jsEngine.evaluate("foo.responseHeaders['content-type']").asString())
; |
| 61 assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefi
ned()); |
| 62 } |
| 63 |
| 64 @Test |
| 65 public void testXMLHttpRequest() { |
| 66 jsEngine.evaluate( |
| 67 "var result;\n" + |
| 68 "var request = new XMLHttpRequest();\n" + |
| 69 "request.open('GET', 'https://easylist-downloads.adblockplus.org/eas
ylist.txt');\n" + |
| 70 "request.setRequestHeader('X', 'Y');\n" + |
| 71 "request.setRequestHeader('X2', 'Y2');\n" + |
| 72 "request.overrideMimeType('text/plain');\n" + |
| 73 "request.addEventListener('load',function() {result=request.response
Text;}, false);\n" + |
| 74 "request.addEventListener('error',function() {result='error';}, fals
e);\n" + |
| 75 "request.send(null);"); |
| 76 |
| 77 do { |
| 78 try { |
| 79 Thread.sleep(200); |
| 80 } catch (InterruptedException e) { |
| 81 throw new RuntimeException(e); |
| 82 } |
| 83 } while (jsEngine.evaluate("result").isUndefined()); |
| 84 |
| 85 assertEquals( |
| 86 ServerResponse.NsStatus.OK.getStatusCode(), |
| 87 jsEngine.evaluate("request.channel.status").asLong()); |
| 88 |
| 89 assertEquals(200l, jsEngine.evaluate("request.status").asLong()); |
| 90 assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)")
.asString()); |
| 91 assertEquals( |
| 92 "text/plain", |
| 93 jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(
0,10)").asString()); |
| 94 assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").is
Null()); |
| 95 } |
| 96 } |
OLD | NEW |