Search in sources :

Example 71 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-boot by spring-projects.

the class CloudFoundryEndpointHandlerMappingTests method registersCloudFoundryDiscoveryEndpoint.

@Test
public void registersCloudFoundryDiscoveryEndpoint() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(Collections.<NamedMvcEndpoint>emptySet(), null, null);
    handlerMapping.setPrefix("/test");
    handlerMapping.setApplicationContext(context);
    handlerMapping.afterPropertiesSet();
    HandlerExecutionChain handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/test"));
    HandlerMethod handlerMethod = (HandlerMethod) handler.getHandler();
    assertThat(handlerMethod.getBean()).isInstanceOf(CloudFoundryDiscoveryMvcEndpoint.class);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 72 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-boot by spring-projects.

the class CloudFoundrySecurityInterceptorTests method preHandleSuccessfulWithRestrictedAccess.

@Test
public void preHandleSuccessfulWithRestrictedAccess() throws Exception {
    this.endpoint = new TestMvcEndpoint(new TestEndpoint("info"));
    this.handlerMethod = new HandlerMethod(this.endpoint, "invoke");
    String accessToken = mockAccessToken();
    this.request.addHeader("Authorization", "Bearer " + accessToken);
    BDDMockito.given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(AccessLevel.RESTRICTED);
    boolean preHandle = this.interceptor.preHandle(this.request, this.response, this.handlerMethod);
    ArgumentCaptor<Token> tokenArgumentCaptor = ArgumentCaptor.forClass(Token.class);
    verify(this.tokenValidator).validate(tokenArgumentCaptor.capture());
    Token token = tokenArgumentCaptor.getValue();
    assertThat(token.toString()).isEqualTo(accessToken);
    assertThat(preHandle).isTrue();
    assertThat(this.response.getStatus()).isEqualTo(HttpStatus.OK.value());
    assertThat(this.request.getAttribute("cloudFoundryAccessLevel")).isEqualTo(AccessLevel.RESTRICTED);
}
Also used : HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 73 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-boot by spring-projects.

the class CloudFoundrySecurityInterceptorTests method preHandleWhenAccessIsNotAllowedShouldReturnFalse.

@Test
public void preHandleWhenAccessIsNotAllowedShouldReturnFalse() throws Exception {
    this.endpoint = new TestMvcEndpoint(new TestEndpoint("env"));
    this.handlerMethod = new HandlerMethod(this.endpoint, "invoke");
    String accessToken = mockAccessToken();
    this.request.addHeader("Authorization", "bearer " + accessToken);
    BDDMockito.given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(AccessLevel.RESTRICTED);
    boolean preHandle = this.interceptor.preHandle(this.request, this.response, this.handlerMethod);
    assertThat(preHandle).isFalse();
    assertThat(this.response.getStatus()).isEqualTo(Reason.ACCESS_DENIED.getStatus().value());
}
Also used : HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 74 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class ModelFactoryOrderingTests method runTest.

private void runTest(Object controller) throws Exception {
    HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
    resolvers.addResolver(new ModelAttributeMethodProcessor(false));
    resolvers.addResolver(new ModelMethodProcessor());
    WebDataBinderFactory dataBinderFactory = new DefaultDataBinderFactory(null);
    Class<?> type = controller.getClass();
    Set<Method> methods = MethodIntrospector.selectMethods(type, METHOD_FILTER);
    List<InvocableHandlerMethod> modelMethods = new ArrayList<>();
    for (Method method : methods) {
        InvocableHandlerMethod modelMethod = new InvocableHandlerMethod(controller, method);
        modelMethod.setHandlerMethodArgumentResolvers(resolvers);
        modelMethod.setDataBinderFactory(dataBinderFactory);
        modelMethods.add(modelMethod);
    }
    Collections.shuffle(modelMethods);
    SessionAttributesHandler sessionHandler = new SessionAttributesHandler(type, this.sessionAttributeStore);
    ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler);
    factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle"));
    if (logger.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        for (String name : getInvokedMethods()) {
            sb.append(" >> ").append(name);
        }
        logger.debug(sb);
    }
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) ArrayList(java.util.ArrayList) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethodArgumentResolverComposite(org.springframework.web.method.support.HandlerMethodArgumentResolverComposite) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory)

Example 75 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class ModelFactoryTests method modelAttributeMethod.

@Test
public void modelAttributeMethod() throws Exception {
    ModelFactory modelFactory = createModelFactory("modelAttr", Model.class);
    HandlerMethod handlerMethod = createHandlerMethod("handle");
    modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
    assertEquals(Boolean.TRUE, this.mavContainer.getModel().get("modelAttr"));
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Aggregations

HandlerMethod (org.springframework.web.method.HandlerMethod)131 Test (org.junit.Test)81 Method (java.lang.reflect.Method)42 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)34 ArrayList (java.util.ArrayList)26 ModelAndView (org.springframework.web.servlet.ModelAndView)24 MethodParameter (org.springframework.core.MethodParameter)20 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)19 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)19 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)18 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)18 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 HttpMethod (org.springframework.http.HttpMethod)7 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Map (java.util.Map)6 List (java.util.List)5