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