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            {}
This commit is contained in:
Adriano Ferreira 2011-01-06 20:02:00 -02:00 committed by kimchy
parent 154dcf007e
commit 2d81acbbf2
1 changed files with 59 additions and 0 deletions

View File

@ -60,4 +60,63 @@ public class RestUtilsTests {
RestUtils.decodeQueryString(uri, -1, params); RestUtils.decodeQueryString(uri, -1, params);
assertThat(params.size(), equalTo(0)); assertThat(params.size(), equalTo(0));
} }
@Test
public void testDecodeQueryStringEdgeCases() {
Map<String, String> 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"));
}
} }