UriTemplate extends Template { public static UriTemplate create(String template, Charset charset) { return new UriTemplate(template, true, charset); } private  UriTemplate(String template, boolean encodeSlash, Charset charset); static UriTemplate create(String template, Charset charset); static UriTemplate create(String template, boolean encodeSlash, Charset charset); static UriTemplate append(UriTemplate uriTemplate, String fragment);  }
@Test public void emptyRelativeTemplate() { String template = "/"; UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.expand(Collections.emptyMap())).isEqualToIgnoringCase("/"); }
@Test(expected = IllegalArgumentException.class) public void nullTemplate() { UriTemplate.create(null, Util.UTF_8); }
@Test public void emptyTemplate() { UriTemplate.create("", Util.UTF_8); }
@Test public void simpleTemplate() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); List<String> uriTemplateVariables = uriTemplate.getVariables(); assertThat(uriTemplateVariables).contains("bar").hasSize(1); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("bar", "bar"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate).isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test public void simpleTemplateMultipleExpressions() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); List<String> uriTemplateVariables = uriTemplate.getVariables(); assertThat(uriTemplateVariables).contains("foo", "bar").hasSize(2); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("foo", "first"); variables.put("bar", "second"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test public void simpleTemplateMultipleSequentialExpressions() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); List<String> uriTemplateVariables = uriTemplate.getVariables(); assertThat(uriTemplateVariables).contains("foo", "bar", "baz").hasSize(3); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("foo", "first"); variables.put("bar", "second"); variables.put("baz", "third"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test public void simpleTemplateUnresolvedVariablesAreRemoved() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.getVariables()).contains("foo", "name").hasSize(2); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("name", "Albert"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate).isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test public void missingVariablesOnPathAreRemoved() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.getVariables()).contains("foo", "name").hasSize(2); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("name", "Albert"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test public void simpleTemplateWithRegularExpressions() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.getVariables()).contains("foo", "bar").hasSize(2); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("foo", 1234); variables.put("bar", "stuff"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate).isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test(expected = IllegalArgumentException.class) public void simpleTemplateWithRegularExpressionsValidation() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.getVariables()).contains("foo", "bar").hasSize(2); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("foo", "abcd"); variables.put("bar", "stuff"); uriTemplate.expand(variables); fail("Should not be able to expand, pattern does not match"); }
@Test public void nestedExpressionsAreLiterals() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.getVariables()).contains("baz").hasSize(1); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("baz", "stuff"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: assertThat(URI.create(expandedTemplate)) .isNotNull(); }
@Test public void literalTemplate() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String expandedTemplate = uriTemplate.expand(Collections.emptyMap()); assertThat(expandedTemplate).isEqualToIgnoringCase(template); assertThat(URI.create(expandedTemplate)).isNotNull(); }
@Test(expected = IllegalArgumentException.class) public void rejectEmptyExpressions() { String template = "https: UriTemplate.create(template, Util.UTF_8); fail("Should not accept empty expressions"); }
@Test public void testToString() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.toString()).isEqualToIgnoringCase(template); }
@Test public void encodeVariables() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("first", "John Jacob"); variables.put("last", "Jingleheimer Schmidt"); String expandedTemplate = uriTemplate.expand(variables); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: }
@Test public void encodeLiterals() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String expandedTemplate = uriTemplate.expand(Collections.emptyMap()); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: }
@Test public void ensurePlusIsSupportedOnPath() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String expanded = uriTemplate.expand(Collections.emptyMap()); assertThat(expanded).isEqualToIgnoringCase("https: }
@Test public void ensurePlusInEncodedAs2BOnQuery() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); Map<String, Object> parameters = Collections.singletonMap("type", "sam+adams"); String expanded = uriTemplate.expand(parameters); assertThat(expanded).isEqualToIgnoringCase("https: }
@Test public void incompleteTemplateIsALiteral() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); assertThat(uriTemplate.expand(Collections.emptyMap())) .isEqualToIgnoringCase("https: }
@Test(expected = IllegalArgumentException.class) public void substituteNullMap() { UriTemplate.create("stuff", Util.UTF_8).expand(null); }
@Test public void skipAlreadyEncodedLiteral() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String expandedTemplate = uriTemplate.expand(Collections.emptyMap()); assertThat(expandedTemplate) .isEqualToIgnoringCase("https: }
@Test public void skipAlreadyEncodedVariable() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String encodedVariable = UriUtils.encode("Johnny Appleseed", Util.UTF_8); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("foo", encodedVariable); assertThat(uriTemplate.expand(variables)) .isEqualToIgnoringCase("https: }
@Test public void skipSlashes() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, false, Util.UTF_8); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("path", "me/you/first"); String encoded = uriTemplate.expand(variables); assertThat(encoded).isEqualToIgnoringCase("https: }
@Test public void encodeSlashes() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); Map<String, Object> variables = new LinkedHashMap<>(); variables.put("path", "me/you/first"); String encoded = uriTemplate.expand(variables); assertThat(encoded).isEqualToIgnoringCase("https: }
@Test public void testLiteralTemplateWithQueryString() { String template = "https: UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String expanded = uriTemplate.expand(Collections.emptyMap()); assertThat(expanded).isEqualToIgnoringCase("https: }
@Test public void encodeReserved() { String template = "/get?url={url}"; UriTemplate uriTemplate = UriTemplate.create(template, Util.UTF_8); String expanded = uriTemplate.expand(Collections.singletonMap("url", "https: assertThat(expanded).isEqualToIgnoringCase("/get?url=https%3A%2F%2Fwww.google.com"); }