Search in sources :

Example 81 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromControllerUriTemplate.

@Test
public void testFromControllerUriTemplate() {
    UriComponents uriComponents = fromController(PersonsAddressesController.class).buildAndExpand(15);
    assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses"));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 82 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromMethodNameTypeLevelPathVariable.

@Test
public void testFromMethodNameTypeLevelPathVariable() throws Exception {
    this.request.setContextPath("/myapp");
    UriComponents uriComponents = fromMethodName(PersonsAddressesController.class, "getAddressesForCountry", "DE").buildAndExpand("1");
    assertThat(uriComponents.toUriString(), is("http://localhost/myapp/people/1/addresses/DE"));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 83 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromMethodNameWithCustomBaseUrlViaStaticCall.

@Test
public void testFromMethodNameWithCustomBaseUrlViaStaticCall() throws Exception {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    UriComponents uriComponents = fromMethodName(builder, ControllerWithMethods.class, "methodWithPathVariable", new Object[] { "1" }).build();
    assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Example 84 with UriComponents

use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromMethodNameWithMetaAnnotation.

@Test
public void testFromMethodNameWithMetaAnnotation() throws Exception {
    UriComponents uriComponents = fromMethodName(MetaAnnotationController.class, "handleInput").build();
    assertThat(uriComponents.toUriString(), is("http://localhost/input"));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 85 with UriComponents

use of org.springframework.web.util.UriComponents in project libresonic by Libresonic.

the class JWTAuthenticationProvider method roughlyEqual.

private static boolean roughlyEqual(String expectedRaw, String requestedPathRaw) {
    logger.debug("Comparing expected [{}] vs requested [{}]", expectedRaw, requestedPathRaw);
    if (StringUtils.isEmpty(expectedRaw)) {
        logger.debug("False: empty expected");
        return false;
    }
    try {
        UriComponents expected = UriComponentsBuilder.fromUriString(expectedRaw).build();
        UriComponents requested = UriComponentsBuilder.fromUriString(requestedPathRaw).build();
        if (!Objects.equals(expected.getPath(), requested.getPath())) {
            logger.debug("False: expected path [{}] does not match requested path [{}]", expected.getPath(), requested.getPath());
            return false;
        }
        MapDifference<String, List<String>> difference = Maps.difference(expected.getQueryParams(), requested.getQueryParams());
        if (difference.entriesDiffering().size() != 0 || difference.entriesOnlyOnLeft().size() != 0 || difference.entriesOnlyOnRight().size() != 1 || difference.entriesOnlyOnRight().get(JWTSecurityService.JWT_PARAM_NAME) == null) {
            logger.debug("False: expected query params [{}] do not match requested query params [{}]", expected.getQueryParams(), requested.getQueryParams());
            return false;
        }
    } catch (Exception e) {
        logger.warn("Exception encountered while comparing paths", e);
        return false;
    }
    return true;
}
Also used : UriComponents(org.springframework.web.util.UriComponents) ArrayList(java.util.ArrayList) List(java.util.List) InsufficientAuthenticationException(org.springframework.security.authentication.InsufficientAuthenticationException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)133 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)26 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)7 URI (java.net.URI)5 HashMap (java.util.HashMap)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)4 MethodParameter (org.springframework.core.MethodParameter)3 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2