Skip to content

Instantly share code, notes, and snippets.

@smaspe
Created October 22, 2015 18:16
Show Gist options
  • Save smaspe/eea0a46004723f433e90 to your computer and use it in GitHub Desktop.
Save smaspe/eea0a46004723f433e90 to your computer and use it in GitHub Desktop.
Tests on Uri query parameters
import android.net.Uri;
import android.test.InstrumentationTestCase;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class UriTest extends InstrumentationTestCase {
public void testQueryParameters() throws UnsupportedEncodingException {
Uri uri = Uri.parse("http://domain.com/test?single_value=1");
assertTrue(uri.getQueryParameterNames().contains("single_value"));
uri = Uri.parse("http://domain.com/test?array_value=1,2,3");
assertTrue(uri.getQueryParameterNames().contains("array_value"));
assertEquals("1,2,3", uri.getQueryParameter("array_value"));
assertEquals(1, uri.getQueryParameters("array_value").size());
uri = Uri.parse("http://domain.com/test?array_value=1&array_value=2");
assertTrue(uri.getQueryParameterNames().contains("array_value"));
assertEquals("1", uri.getQueryParameter("array_value"));
assertEquals(2, uri.getQueryParameters("array_value").size());
assertEquals("1", uri.getQueryParameters("array_value").get(0));
String arrayKey = "array_value[]";
uri = Uri.parse("http://domain.com/test?" + arrayKey + "=1,2,3");
assertTrue(uri.getQueryParameterNames().contains("array_value[]"));
assertFalse(uri.getQueryParameterNames().contains("array_value"));
assertEquals(null, uri.getQueryParameter("array_value[]"));
assertEquals(null, uri.getQueryParameter("array_value"));
assertEquals(0, uri.getQueryParameters("array_value[]").size());
assertEquals(0, uri.getQueryParameters("array_value").size());
uri = Uri.parse("http://domain.com/test?" + arrayKey + "=1&" + arrayKey + "=2");
assertTrue(uri.getQueryParameterNames().contains("array_value[]"));
assertFalse(uri.getQueryParameterNames().contains("array_value"));
assertEquals(null, uri.getQueryParameter("array_value"));
assertEquals(0, uri.getQueryParameters("array_value").size());
assertEquals(0, uri.getQueryParameters("array_value[]").size());
arrayKey = URLEncoder.encode("array_value[]", "UTF-8");
uri = Uri.parse("http://domain.com/test?" + arrayKey + "=1,2,3");
assertTrue(uri.getQueryParameterNames().contains("array_value[]"));
assertFalse(uri.getQueryParameterNames().contains("array_value"));
assertEquals("1,2,3", uri.getQueryParameter("array_value[]"));
assertEquals(null, uri.getQueryParameter("array_value"));
assertEquals(1, uri.getQueryParameters("array_value[]").size());
assertEquals(0, uri.getQueryParameters("array_value").size());
uri = Uri.parse("http://domain.com/test?" + arrayKey + "=1&" + arrayKey + "=2");
assertTrue(uri.getQueryParameterNames().contains("array_value[]"));
assertFalse(uri.getQueryParameterNames().contains("array_value"));
assertEquals(null, uri.getQueryParameter("array_value"));
assertEquals(0, uri.getQueryParameters("array_value").size());
assertEquals(2, uri.getQueryParameters("array_value[]").size());
assertEquals("1", uri.getQueryParameters("array_value[]").get(0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment