@SmallTest
 public void testMatchDuplicate() throws Exception {
   UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
   Map<String, String> result = template.match("/order/cheeseburger/cheeseburger/cheeseburger");
   Map<String, String> expected = Collections.singletonMap("c", "cheeseburger");
   assertEquals("Invalid match", expected, result);
 }
 @SmallTest
 public void testExpandVarArgs() throws Exception {
   UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
   URI result = template.expand("1", "42");
   assertEquals(
       "Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
 }
  @SmallTest
  public void testFragments() throws Exception {
    UriTemplate template = new UriTemplate("/search#{fragment}");
    assertTrue(template.matches("/search#foo"));

    template = new UriTemplate("/search?query={query}#{fragment}");
    assertTrue(template.matches("/search?query=foo#bar"));
  }
 @SmallTest
 public void testMatchMultipleInOneSegment() throws Exception {
   UriTemplate template = new UriTemplate("/{foo}-{bar}");
   Map<String, String> result = template.match("/12-34");
   Map<String, String> expected = new HashMap<String, String>(2);
   expected.put("foo", "12");
   expected.put("bar", "34");
   assertEquals("Invalid match", expected, result);
 }
 @SmallTest
 public void testMatches() throws Exception {
   UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
   assertTrue(
       "UriTemplate does not match", template.matches("http://example.com/hotels/1/bookings/42"));
   assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/bookings"));
   assertFalse("UriTemplate matches", template.matches(""));
   assertFalse("UriTemplate matches", template.matches(null));
 }
 @SmallTest
 public void testExpandEncoded() throws Exception {
   UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
   URI result = template.expand("Z\u00fcrich");
   assertEquals(
       "Invalid expanded template",
       new URI("http://example.com/hotel%20list/Z%C3%BCrich"),
       result);
 }
 @SmallTest
 public void testExpandMapNonString() throws Exception {
   Map<String, Integer> uriVariables = new HashMap<String, Integer>(2);
   uriVariables.put("booking", 42);
   uriVariables.put("hotel", 1);
   UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
   URI result = template.expand(uriVariables);
   assertEquals(
       "Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
 }
 @SmallTest
 public void testExpandMapEncoded() throws Exception {
   Map<String, String> uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich");
   UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
   URI result = template.expand(uriVariables);
   assertEquals(
       "Invalid expanded template",
       new URI("http://example.com/hotel%20list/Z%C3%BCrich"),
       result);
 }
  @SmallTest
  public void testMatch() throws Exception {
    Map<String, String> expected = new HashMap<String, String>(2);
    expected.put("booking", "42");
    expected.put("hotel", "1");

    UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
    Map<String, String> result = template.match("http://example.com/hotels/1/bookings/42");
    assertEquals("Invalid match", expected, result);
  }
 @SmallTest
 public void testExpandMapDuplicateVariables() throws Exception {
   UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
   assertEquals(
       "Invalid variable names", Arrays.asList("c", "c", "c"), template.getVariableNames());
   URI result = template.expand(Collections.singletonMap("c", "cheeseburger"));
   assertEquals(
       "Invalid expanded template",
       new URI("/order/cheeseburger/cheeseburger/cheeseburger"),
       result);
 }
  /**
   * Generates a {@link UriTemplate} instance from the builder.
   *
   * @return
   * @since 2.0
   */
  public UriTemplate build() throws MalformedUriTemplateException {
    UriTemplate template = new UriTemplate(this.components);
    if (this.values != null) {
      template.set(values);
    }

    if (defaultDateFormat != null) {
      template.defaultDateFormat = defaultDateFormat;
    }
    return template;
  }
 public void testEscapeAmpersandInRequestParameterValueForGet() {
   UriTemplate template =
       new UriTemplate("http://localhost:8080/app/{dir}/{file}?param1={param1}&param2={param2}");
   HashMap<String, String> vars = new HashMap<String, String>();
   vars.put("dir", "tmp");
   vars.put("file", "test.html");
   vars.put("param1", "foo");
   vars.put("param2", "foo&bar");
   URI uri = template.expand(vars);
   assertEquals(
       "http://localhost:8080/app/tmp/test.html?param1=foo&param2=foo%26bar", uri.toString());
 }
 @SmallTest
 public void testExpandVarArgsNotEnoughVariables() throws Exception {
   boolean success = false;
   try {
     UriTemplate template =
         new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
     template.expand("1");
   } catch (IllegalArgumentException e) {
     success = true;
   }
   assertTrue("expected IllegalArgumentException", success);
 }
 @SmallTest
 public void expandMapUnboundVariables() throws Exception {
   boolean success = false;
   try {
     Map<String, String> uriVariables = new HashMap<String, String>(2);
     uriVariables.put("booking", "42");
     uriVariables.put("bar", "1");
     UriTemplate template =
         new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
     template.expand(uriVariables);
   } catch (IllegalArgumentException e) {
     success = true;
   }
   assertTrue("expected IllegalArgumentException", success);
 }
  private URI _build(boolean encode, Object... values) {
    if (values == null || values.length == 0) {
      return createURI(create());
    }

    if (ssp != null) {
      throw new IllegalArgumentException("Schema specific part is opaque");
    }

    encodeMatrix();
    encodeQuery();

    String uri =
        UriTemplate.createURI(
            scheme,
            authority,
            userInfo,
            host,
            (port != -1) ? String.valueOf(port) : null,
            path.toString(),
            query.toString(),
            fragment,
            values,
            encode);
    return createURI(uri);
  }
Example #16
0
  private URI _buildFromMap(boolean encode, Map<String, ? extends Object> values) {
    if (ssp != null) throw new IllegalArgumentException("Schema specific part is opaque");

    encodeMatrix();
    encodeQuery();

    String uri =
        UriTemplate.createURI(
            scheme,
            authority,
            userInfo,
            host,
            (port != -1) ? String.valueOf(port) : null,
            path.toString(),
            query.toString(),
            fragment,
            values,
            encode);
    return createURI(uri);
  }
 @SmallTest
 public void testExpandWithAtSign() {
   UriTemplate template = new UriTemplate("http://localhost/query={query}");
   URI uri = template.expand("foo@bar");
   assertEquals("http://localhost/query=foo@bar", uri.toString());
 }
 /**
  * Create a new UriTemplateBuilder.
  *
  * @param template
  */
 UriTemplateBuilder(UriTemplate template) throws MalformedUriTemplateException {
   this(template.getTemplate());
   this.values = template.getValues();
   this.defaultDateFormat = template.defaultDateFormat;
 }
 @SmallTest
 public void testMatchesCustomRegex() throws Exception {
   UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel:\\d+}");
   assertTrue("UriTemplate does not match", template.matches("http://example.com/hotels/42"));
   assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/foo"));
 }
 @SmallTest
 public void testQueryVariables() throws Exception {
   UriTemplate template = new UriTemplate("/search?q={query}");
   assertTrue(template.matches("/search?q=foo"));
 }
 @SmallTest
 public void testExpandWithDollar() {
   UriTemplate template = new UriTemplate("/{a}");
   URI uri = template.expand("$replacement");
   assertEquals("/$replacement", uri.toString());
 }
 @SmallTest
 public void testGetVariableNames() throws Exception {
   UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
   List<String> variableNames = template.getVariableNames();
   assertEquals("Invalid variable names", Arrays.asList("hotel", "booking"), variableNames);
 }