Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/MockWebRequestTest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Patch Set: Updated tests, also moved to libadblockplus-android repo Created June 21, 2016, 10:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 BaseJsTest {
31
32 private class LocalMockWebRequest extends WebRequest {
33 @Override
34 public ServerResponse httpGET(String url, List<HeaderEntry> headers) {
35 try {
36 Thread.sleep(50);
37 } catch (InterruptedException e) {
38 throw new RuntimeException(e);
39 }
40
41 ServerResponse result = new ServerResponse();
42 result.setStatus(ServerResponse.NsStatus.OK);
43 result.setResponseStatus(123);
44 result.setReponseHeaders(Arrays.asList(new HeaderEntry("Foo", "Bar") ));
45
46 result.setResponse(
47 url + "\n" +
48 headers.get(0).getKey() + "\n" +
49 headers.get(0).getValue());
50 return result;
51 }
52 }
53
54 @Override
55 protected void setUp() throws Exception {
56 super.setUp();
57
58 jsEngine.setWebRequest(new LocalMockWebRequest());
59 }
60
61 @Test
62 public void testBadCall() {
63 try {
64 jsEngine.evaluate("_webRequest.GET()");
65 fail();
66 } catch (AdblockPlusException e) {
67 // ignored
68 }
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 }
105
106 @Test
107 public void testSuccessfulRequest() throws InterruptedException {
108 jsEngine.evaluate(
109 "_webRequest.GET('http://example.com/', {X: 'Y'}, function(result) { foo = result;} )");
110 assertTrue(jsEngine.evaluate("this.foo").isUndefined());
111
112 Thread.sleep(200);
113
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 }
121
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld