| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 package org.adblockplus.libadblockplus.tests; | |
| 19 | |
| 20 import org.adblockplus.libadblockplus.AdblockPlusException; | |
| 21 import org.adblockplus.libadblockplus.HeaderEntry; | |
| 22 import org.adblockplus.libadblockplus.ServerResponse; | |
| 23 import org.adblockplus.libadblockplus.WebRequest; | |
| 24 | |
| 25 import org.junit.Test; | |
| 26 | |
| 27 import java.util.Arrays; | |
| 28 import java.util.List; | |
| 29 | |
| 30 public class MockWebRequestTest extends BaseJsEngineTest | |
| 31 { | |
| 32 | |
| 33 private class LocalMockWebRequest implements WebRequest | |
| 34 { | |
| 35 @Override | |
| 36 public ServerResponse httpGET(String url, List<HeaderEntry> headers) | |
| 37 { | |
| 38 try | |
| 39 { | |
| 40 Thread.sleep(50); | |
| 41 } | |
| 42 catch (InterruptedException e) | |
| 43 { | |
| 44 throw new RuntimeException(e); | |
| 45 } | |
| 46 | |
| 47 ServerResponse result = new ServerResponse(); | |
| 48 result.setStatus(ServerResponse.NsStatus.OK); | |
| 49 result.setResponseStatus(123); | |
| 50 result.setReponseHeaders(Arrays.asList(new HeaderEntry("Foo", "Bar"))); | |
| 51 | |
| 52 result.setResponse( | |
| 53 url + "\n" + | |
| 54 headers.get(0).getKey() + "\n" + | |
| 55 headers.get(0).getValue()); | |
| 56 return result; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 @Override | |
| 61 protected WebRequest createWebRequest() | |
| 62 { | |
| 63 return new LocalMockWebRequest(); | |
| 64 } | |
| 65 | |
| 66 @Test | |
| 67 public void testBadCall() | |
| 68 { | |
| 69 try | |
| 70 { | |
| 71 jsEngine.evaluate("_webRequest.GET()"); | |
| 72 fail(); | |
| 73 } | |
| 74 catch (AdblockPlusException e) | |
| 75 { | |
| 76 // ignored | |
| 77 } | |
| 78 | |
| 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 } | |
| 128 } | |
| 129 | |
| 130 @Test | |
| 131 public void testSuccessfulRequest() throws InterruptedException | |
| 132 { | |
| 133 jsEngine.evaluate( | |
| 134 "let foo = true; _webRequest.GET('http://example.com/', {X: 'Y'}, function
(result) {foo = result;} )"); | |
| 135 assertTrue(jsEngine.evaluate("foo").isBoolean()); | |
| 136 assertTrue(jsEngine.evaluate("foo").asBoolean()); | |
| 137 | |
| 138 Thread.sleep(200); | |
| 139 | |
| 140 assertEquals( | |
| 141 ServerResponse.NsStatus.OK.getStatusCode(), | |
| 142 jsEngine.evaluate("foo.status").asLong()); | |
| 143 assertEquals("http://example.com/\nX\nY", jsEngine.evaluate("foo.responseTex
t").asString()); | |
| 144 assertEquals("{\"Foo\":\"Bar\"}", | |
| 145 jsEngine.evaluate("JSON.stringify(foo.responseHeaders)").asString()); | |
| 146 } | |
| 147 | |
| 148 } | |
| OLD | NEW |