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