Left: | ||
Right: |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 android.os.SystemClock; | |
21 | |
20 import org.adblockplus.libadblockplus.AdblockPlusException; | 22 import org.adblockplus.libadblockplus.AdblockPlusException; |
23 import org.adblockplus.libadblockplus.BaseJsEngineTest; | |
21 import org.adblockplus.libadblockplus.HeaderEntry; | 24 import org.adblockplus.libadblockplus.HeaderEntry; |
22 import org.adblockplus.libadblockplus.ServerResponse; | 25 import org.adblockplus.libadblockplus.ServerResponse; |
23 import org.adblockplus.libadblockplus.WebRequest; | 26 import org.adblockplus.libadblockplus.WebRequest; |
24 | 27 |
25 import org.junit.Test; | 28 import org.junit.Test; |
26 | 29 |
27 import java.util.Arrays; | 30 import java.util.Arrays; |
28 import java.util.List; | 31 import java.util.List; |
29 | 32 |
30 public class MockWebRequestTest extends BaseJsEngineTest | 33 public class MockWebRequestTest extends BaseJsEngineTest |
31 { | 34 { |
35 @Override | |
36 protected void setUp() throws Exception | |
37 { | |
38 setWebRequest(new LocalMockWebRequest()); | |
39 super.setUp(); | |
40 } | |
32 | 41 |
33 private class LocalMockWebRequest implements WebRequest | 42 private class LocalMockWebRequest implements WebRequest |
34 { | 43 { |
35 @Override | 44 @Override |
36 public ServerResponse httpGET(String url, List<HeaderEntry> headers) | 45 public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
37 { | 46 { |
38 try | 47 SystemClock.sleep(50); |
39 { | |
40 Thread.sleep(50); | |
41 } | |
42 catch (InterruptedException e) | |
43 { | |
44 throw new RuntimeException(e); | |
45 } | |
46 | 48 |
47 ServerResponse result = new ServerResponse(); | 49 ServerResponse result = new ServerResponse(); |
48 result.setStatus(ServerResponse.NsStatus.OK); | 50 result.setStatus(ServerResponse.NsStatus.OK); |
49 result.setResponseStatus(123); | 51 result.setResponseStatus(123); |
50 result.setReponseHeaders(Arrays.asList(new HeaderEntry("Foo", "Bar"))); | 52 result.setReponseHeaders(Arrays.asList(new HeaderEntry("Foo", "Bar"))); |
51 | 53 |
52 result.setResponse( | 54 result.setResponse( |
53 url + "\n" + | 55 url + "\n" + |
54 headers.get(0).getKey() + "\n" + | 56 headers.get(0).getKey() + "\n" + |
55 headers.get(0).getValue()); | 57 headers.get(0).getValue()); |
56 return result; | 58 return result; |
57 } | 59 } |
58 } | 60 } |
59 | 61 |
60 @Override | |
61 protected WebRequest createWebRequest() | |
62 { | |
63 return new LocalMockWebRequest(); | |
64 } | |
65 | |
66 @Test | 62 @Test |
67 public void testBadCall() | 63 public void testBadCall() |
68 { | 64 { |
69 try | 65 final String[] sources = |
66 { | |
67 "_webRequest.GET()", "_webRequest.GET('', {}, function(){})", | |
68 "_webRequest.GET({toString: false}, {}, function(){})", | |
69 "_webRequest.GET('http://example.com/', null, function(){})", | |
70 "_webRequest.GET('http://example.com/', {}, null)", | |
71 "_webRequest.GET('http://example.com/', {}, function(){}, 0)" | |
72 }; | |
73 | |
74 for (String source : sources) | |
70 { | 75 { |
71 jsEngine.evaluate("_webRequest.GET()"); | 76 try |
72 fail(); | 77 { |
73 } | 78 jsEngine.evaluate(source); |
74 catch (AdblockPlusException e) | 79 fail(source); |
75 { | 80 } catch (AdblockPlusException e) |
anton
2018/08/24 12:25:04
new line required here
René Jeschke
2018/08/27 20:02:15
Done.
| |
76 // ignored | 81 { |
77 } | 82 // ignored |
78 | 83 } |
79 try | |
80 { | |
81 jsEngine.evaluate("_webRequest.GET('', {}, function(){})"); | |
82 fail(); | |
83 } | |
84 catch (AdblockPlusException e) | |
85 { | |
86 // ignored | |
87 } | |
88 | |
89 try | |
90 { | |
91 jsEngine.evaluate("_webRequest.GET({toString: false}, {}, function(){})"); | |
92 fail(); | |
93 } | |
94 catch (AdblockPlusException e) | |
95 { | |
96 // ignored | |
97 } | |
98 | |
99 try | |
100 { | |
101 jsEngine.evaluate("_webRequest.GET('http://example.com/', null, function() {})"); | |
102 fail(); | |
103 } | |
104 catch (AdblockPlusException e) | |
105 { | |
106 // ignored | |
107 } | |
108 | |
109 try | |
110 { | |
111 jsEngine.evaluate("_webRequest.GET('http://example.com/', {}, null)"); | |
112 fail(); | |
113 } | |
114 catch (AdblockPlusException e) | |
115 { | |
116 // ignored | |
117 } | |
118 | |
119 try | |
120 { | |
121 jsEngine.evaluate("_webRequest.GET('http://example.com/', {}, function(){} , 0)"); | |
122 fail(); | |
123 } | |
124 catch (AdblockPlusException e) | |
125 { | |
126 // ignored | |
127 } | 84 } |
128 } | 85 } |
129 | 86 |
130 @Test | 87 @Test |
131 public void testSuccessfulRequest() throws InterruptedException | 88 public void testSuccessfulRequest() |
132 { | 89 { |
133 jsEngine.evaluate( | 90 jsEngine.evaluate( |
134 "let foo = true; _webRequest.GET('http://example.com/', {X: 'Y'}, function (result) {foo = result;} )"); | 91 "let foo = true; _webRequest.GET('http://example.com/', {X: 'Y'}, function (result) {foo = result;} )"); |
135 assertTrue(jsEngine.evaluate("foo").isBoolean()); | 92 assertTrue(jsEngine.evaluate("foo").isBoolean()); |
136 assertTrue(jsEngine.evaluate("foo").asBoolean()); | 93 assertTrue(jsEngine.evaluate("foo").asBoolean()); |
137 | 94 |
138 Thread.sleep(200); | 95 SystemClock.sleep(200); |
139 | 96 |
140 assertEquals( | 97 assertEquals( |
141 ServerResponse.NsStatus.OK.getStatusCode(), | 98 ServerResponse.NsStatus.OK.getStatusCode(), |
142 jsEngine.evaluate("foo.status").asLong()); | 99 jsEngine.evaluate("foo.status").asLong()); |
143 assertEquals("http://example.com/\nX\nY", jsEngine.evaluate("foo.responseTex t").asString()); | 100 assertEquals("http://example.com/\nX\nY", jsEngine.evaluate("foo.responseTex t").asString()); |
144 assertEquals("{\"Foo\":\"Bar\"}", | 101 assertEquals("{\"Foo\":\"Bar\"}", |
145 jsEngine.evaluate("JSON.stringify(foo.responseHeaders)").asString()); | 102 jsEngine.evaluate("JSON.stringify(foo.responseHeaders)").asString()); |
146 } | 103 } |
147 | |
148 } | 104 } |
OLD | NEW |