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: fix typo ".. file[s]" Created Sept. 15, 2016, 10:44 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
33 private class LocalMockWebRequest extends 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 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
79 }
80
81 try
82 {
83 jsEngine.evaluate("_webRequest.GET('', {}, function(){})");
84 fail();
85 }
86 catch (AdblockPlusException e)
87 {
88 // ignored
89 }
90
91 try
92 {
93 jsEngine.evaluate("_webRequest.GET({toString: false}, {}, function(){})");
94 fail();
95 }
96 catch (AdblockPlusException e)
97 {
98 // ignored
99 }
100
101 try
102 {
103 jsEngine.evaluate("_webRequest.GET('http://example.com/', null, function() {})");
104 fail();
105 }
106 catch (AdblockPlusException e)
107 {
108 // ignored
109 }
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
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld