From 2d81acbbf287df2ed153886523cddc62a22f0ad1 Mon Sep 17 00:00:00 2001 From: Adriano Ferreira Date: Thu, 6 Jan 2011 20:02:00 -0200 Subject: [PATCH] Adding test for edge cases of RestUtils.decodeQueryString() When writing tests for the fix of decodeQueryString() to handle gracefully edge cases like: &a &a&b it arises the question of what behavior is desirable for these pathological cases (vs the regular p=v pairs). This change just skips them which is consonant to the preexisting code. To be thorough, we add tests for a bunch of edge cases, like: QUERY PARSED PARAMS ? {} ?& {} ?= { "": "" } ?a {} ?p=v&a { "p": "v" } ?p=v&a&p1=v1 { "p": "v", "p1": "v1" } ?a&b {} --- .../rest/util/RestUtilsTests.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/rest/util/RestUtilsTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/rest/util/RestUtilsTests.java index 7acf3006d3a..e5f06b43368 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/rest/util/RestUtilsTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/rest/util/RestUtilsTests.java @@ -60,4 +60,63 @@ public class RestUtilsTests { RestUtils.decodeQueryString(uri, -1, params); assertThat(params.size(), equalTo(0)); } + + @Test + public void testDecodeQueryStringEdgeCases() { + Map params = newHashMap(); + + String uri = "something?"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(0)); + + params.clear(); + uri = "something?&"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(0)); + + params.clear(); + uri = "something?p=v&&p1=v1"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(2)); + assertThat(params.get("p"), equalTo("v")); + assertThat(params.get("p1"), equalTo("v1")); + + params.clear(); + uri = "something?="; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(1)); + assertThat(params.get(""), equalTo("")); + + params.clear(); + uri = "something?&="; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(1)); + assertThat(params.get(""), equalTo("")); + + params.clear(); + uri = "something?a"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(0)); + + params.clear(); + uri = "something?p=v&a"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(1)); + assertThat(params.get("p"), equalTo("v")); + + params.clear(); + uri = "something?p=v&a&p1=v1"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(2)); + assertThat(params.get("p"), equalTo("v")); + assertThat(params.get("p1"), equalTo("v1")); + + params.clear(); + uri = "something?p=v&a&b&p1=v1"; + RestUtils.decodeQueryString(uri, uri.indexOf('?') + 1, params); + assertThat(params.size(), equalTo(2)); + assertThat(params.get("p"), equalTo("v")); + assertThat(params.get("p1"), equalTo("v1")); + } + }