RequestUrlParamsEncoder implements RequestParamsEncoder<RequestT> { @Override public String encode(RequestT request) { Map<String, String> params = paramsExtractor.extract(request); if (params.isEmpty()) { return ""; } StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { if (sb.length() > 0) { sb.append("&"); } String name = entry.getKey(); String value = entry.getValue(); if (name == null) { throw new IllegalArgumentException("Request parameter name cannot be null"); } if (value != null) { if (!isValid(name, value)) { throw new IllegalArgumentException( "Invalid url-encoded request parameter name-value pair: " + name + "=" + value); } sb.append(name).append("=").append(value); } } return sb.toString(); } RequestUrlParamsEncoder(
      RequestParamsExtractor<RequestT> paramsExtractor, boolean validateExtractedParameters); @Override String encode(RequestT request);  }
@Test public void testEncodeValidationSuccess() throws Exception { @SuppressWarnings("unchecked") RequestParamsExtractor<Integer> extractor = getMockExtractor(2, ImmutableMap.of("param1", "value+1", "param2", "value+2+%26")); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, true); String encodedParams = encoder.encode(2); assertEquals("param1=value+1&param2=value+2+%26", encodedParams); }
@Test(expected = IllegalArgumentException.class) public void testEncodeValidationFail() throws Exception { RequestParamsExtractor<Integer> extractor = getMockExtractor(1, ImmutableMap.of("param1", "value+1", "param2", "value+2+&")); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, true); encoder.encode(1); }
@Test public void testEncodeNoValidationSuccess() throws Exception { RequestParamsExtractor<Integer> extractor = getMockExtractor(1, ImmutableMap.of("param1", "value+1", "param2", "value+2+&")); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, false); String encodedParams = encoder.encode(1); assertEquals("param1=value+1&param2=value+2+&", encodedParams); }
@Test(expected = IllegalArgumentException.class) public void testEncodeNullName() throws Exception { RequestParamsExtractor<Integer> extractor = getMockExtractor(1, Collections.singletonMap((String) null, "value1")); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, false); encoder.encode(1); }
@Test public void testEncodeNullValue() throws Exception { RequestParamsExtractor<Integer> extractor = getMockExtractor(1, Collections.singletonMap("param1", (String) null)); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, false); String encodedParams = encoder.encode(1); assertEquals("", encodedParams); }
@Test public void testEncodeEmptyValue() throws Exception { RequestParamsExtractor<Integer> extractor = getMockExtractor(1, Collections.singletonMap("param1", "")); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, false); String encodedParams = encoder.encode(1); assertEquals("param1=", encodedParams); }
@Test public void testEncodeNullAndEmptyParams() throws Exception { RequestParamsExtractor<Integer> extractor = getMockExtractor(1, Collections.<String, String>emptyMap()); RequestUrlParamsEncoder<Integer> encoder = new RequestUrlParamsEncoder<>(extractor, false); String encodedParams = encoder.encode(1); assertEquals("", encodedParams); extractor = getMockExtractor(1, null); encoder = new RequestUrlParamsEncoder<>(extractor, false); NullPointerException exception = null; try { encoder.encode(1); } catch (NullPointerException e) { exception = e; } assertNotNull(exception); }