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