 Issue 29344967:
  Issue 4031 - Implement tests for libadblockplus-android  (Closed)
    
  
    Issue 29344967:
  Issue 4031 - Implement tests for libadblockplus-android  (Closed) 
  | Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestTest.java | 
| diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestTest.java | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..ad97d009549a137c13e074fba85a4d831e343bac | 
| --- /dev/null | 
| +++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestTest.java | 
| @@ -0,0 +1,113 @@ | 
| +/* | 
| + * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| + * Copyright (C) 2006-2016 Eyeo GmbH | 
| + * | 
| + * Adblock Plus is free software: you can redistribute it and/or modify | 
| + * it under the terms of the GNU General Public License version 3 as | 
| + * published by the Free Software Foundation. | 
| + * | 
| + * Adblock Plus is distributed in the hope that it will be useful, | 
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| + * GNU General Public License for more details. | 
| + * | 
| + * You should have received a copy of the GNU General Public License | 
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| + */ | 
| + | 
| +package org.adblockplus.libadblockplus.tests; | 
| + | 
| +import org.adblockplus.android.AndroidWebRequest; | 
| +import org.adblockplus.libadblockplus.JsValue; | 
| +import org.adblockplus.libadblockplus.ServerResponse; | 
| + | 
| +import org.junit.Test; | 
| + | 
| +public class AndroidWebRequestTest extends BaseJsTest | 
| +{ | 
| + @Override | 
| + protected void setUp() throws Exception | 
| + { | 
| + super.setUp(); | 
| + | 
| + jsEngine.setWebRequest(new AndroidWebRequest()); | 
| + } | 
| + | 
| + @Test | 
| + public void testRealWebRequest() | 
| + { | 
| + // This URL should redirect to easylist-downloads.adblockplus.org and we | 
| + // should get the actual filter list back. | 
| + jsEngine.evaluate( | 
| + "_webRequest.GET('https://easylist-downloads.adblockplus.org/easylist.txt', {}, " + | 
| + "function(result) {foo = result;} )"); | 
| + do | 
| + { | 
| + try | 
| + { | 
| + Thread.sleep(200); | 
| + } | 
| + catch (InterruptedException e) | 
| + { | 
| + throw new RuntimeException(e); | 
| + } | 
| + } while (jsEngine.evaluate("this.foo").isUndefined()); | 
| + | 
| + String response = jsEngine.evaluate("foo.responseText").asString(); | 
| + assertNotNull(response); | 
| + assertEquals( | 
| + ServerResponse.NsStatus.OK.getStatusCode(), | 
| + jsEngine.evaluate("foo.status").asLong()); | 
| + assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); | 
| + assertEquals( | 
| + "[Adblock Plus ", | 
| + jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); | 
| + JsValue jsHeaders = jsEngine.evaluate("foo.responseHeaders"); | 
| + assertNotNull(jsHeaders); | 
| + assertFalse(jsHeaders.isUndefined()); | 
| + assertFalse(jsHeaders.isNull()); | 
| + assertTrue(jsHeaders.isObject()); | 
| + assertEquals( | 
| + "text/plain", | 
| + jsEngine.evaluate("foo.responseHeaders['Content-Type'].substr(0,10)").asString()); | 
| 
Felix Dahlke
2016/09/12 13:52:13
Nit: Missing space before comma and `10`? Same thi
 
Felix Dahlke
2016/09/13 08:30:14
What about this comment?
 
Felix Dahlke
2016/09/13 10:51:44
You don't seem to have noticed this comment, hm?
 
anton
2016/09/13 11:00:22
My bad, sorry. Going to fix it right now
 | 
| + assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefined()); | 
| + } | 
| + | 
| + @Test | 
| + public void testXMLHttpRequest() | 
| + { | 
| + jsEngine.evaluate( | 
| + "var result;\n" + | 
| + "var request = new XMLHttpRequest();\n" + | 
| + "request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.txt');\n" + | 
| + "request.setRequestHeader('X', 'Y');\n" + | 
| + "request.setRequestHeader('X2', 'Y2');\n" + | 
| + "request.overrideMimeType('text/plain');\n" + | 
| + "request.addEventListener('load',function() {result=request.responseText;}, false);\n" + | 
| + "request.addEventListener('error',function() {result='error';}, false);\n" + | 
| + "request.send(null);"); | 
| + | 
| + do | 
| + { | 
| + try | 
| + { | 
| + Thread.sleep(200); | 
| + } | 
| + catch (InterruptedException e) | 
| + { | 
| + throw new RuntimeException(e); | 
| + } | 
| + } while (jsEngine.evaluate("result").isUndefined()); | 
| + | 
| + assertEquals( | 
| + ServerResponse.NsStatus.OK.getStatusCode(), | 
| + jsEngine.evaluate("request.channel.status").asLong()); | 
| + | 
| + assertEquals(200l, jsEngine.evaluate("request.status").asLong()); | 
| + assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)").asString()); | 
| + assertEquals( | 
| + "text/plain", | 
| + jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(0,10)").asString()); | 
| + assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").isNull()); | 
| + } | 
| +} |