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