/** * A simple pass-through to {@link #toUriString()}. */ @Override public final String toString() { return toUriString(); }
@Test // SPR-12977 public void fromMethodNameWithBridgedMethod() { UriComponents uriComponents = fromMethodName(PersonCrudController.class, "get", (long) 42).build(); assertThat(uriComponents.toUriString(), is("http://localhost/42")); }
@Test public void fromMethodNameNotMapped() { UriComponents uriComponents = fromMethodName(UnmappedController.class, "unmappedMethod").build(); assertThat(uriComponents.toUriString(), is("http://localhost/")); }
@Test public void fromControllerNotMapped() { UriComponents uriComponents = fromController(UnmappedController.class).build(); assertThat(uriComponents.toUriString(), is("http://localhost/")); }
@Test // SPR-14405 public void fromMethodNameWithOptionalParam() { UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, "methodWithOptionalParam", new Object[] {null}).build(); assertThat(uriComponents.toUriString(), is("http://localhost/something/optional-param")); }
@Test public void fromMethodNameWithMetaAnnotation() { UriComponents uriComponents = fromMethodName(MetaAnnotationController.class, "handleInput").build(); assertThat(uriComponents.toUriString(), is("http://localhost/input")); }
@Test // SPR-17630 public void uirTemplateExpandWithMismatchedCurlyBraces() { assertEquals("/myurl/?q=%7B%7B%7B%7B", UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build().toUriString()); }
@Test public void fromControllerPlain() { UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), Matchers.endsWith("/people")); }
@Test public void fromMethodCallOnSubclass() { UriComponents uriComponents = fromMethodCall(on(ExtendedController.class).myMethod(null)).build(); assertThat(uriComponents.toUriString(), startsWith("http://localhost")); assertThat(uriComponents.toUriString(), endsWith("/extended/else")); }
@Test public void fromMethodCallWithPathVariable() { UriComponents uriComponents = fromMethodCall( on(ControllerWithMethods.class).methodWithPathVariable("1")).build(); assertThat(uriComponents.toUriString(), startsWith("http://localhost")); assertThat(uriComponents.toUriString(), endsWith("/something/1/foo")); }
@Test public void fromMethodCallPlain() { UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).myMethod(null)).build(); assertThat(uriComponents.toUriString(), startsWith("http://localhost")); assertThat(uriComponents.toUriString(), endsWith("/something/else")); }
@Test public void fromMethodNameTypeLevelPathVariable() { this.request.setContextPath("/myapp"); UriComponents uriComponents = fromMethodName( PersonsAddressesController.class, "getAddressesForCountry", "DE").buildAndExpand("1"); assertThat(uriComponents.toUriString(), is("http://localhost/myapp/people/1/addresses/DE")); }
@Test public void usesForwardedHostAsHostIfHeaderIsSet() throws Exception { this.request.addHeader("X-Forwarded-Host", "somethingDifferent"); adaptRequestFromForwardedHeaders(); UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), startsWith("http://somethingDifferent")); }
@Test public void fromControllerUriTemplate() { UriComponents uriComponents = fromController(PersonsAddressesController.class).buildAndExpand(15); assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses")); }
@Test public void fromRequestAtypicalHttpPort() { this.request.setServerPort(8080); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("http://localhost:8080/mvc-showcase", result); }
@Test public void expand() { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com").path("/{foo} {bar}").build(); uriComponents = uriComponents.expand("1 2", "3 4"); assertEquals("/1 2 3 4", uriComponents.getPath()); assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString()); }
@Test public void fromContextPath() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromContextPath(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase", result); }
@Test public void fromRequest() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); }
@Test public void fromRequestAtypicalHttpsPort() { this.request.setScheme("https"); this.request.setServerPort(9043); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("https://localhost:9043/mvc-showcase", result); }
@Test // SPR-16650 public void fromContextPathWithForwardedPrefix() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/prefix"); this.request.setContextPath("/mvc-showcase"); this.request.setRequestURI("/mvc-showcase/simple"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); String result = ServletUriComponentsBuilder.fromContextPath(requestToUse).build().toUriString(); assertEquals("http://localhost/prefix", result); }