diff --git a/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/AbstractEnableWebMvcExtensionTest.java b/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/AbstractEnableWebMvcExtensionTest.java index 4bcabca22..1fc028e2e 100644 --- a/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/AbstractEnableWebMvcExtensionTest.java +++ b/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/AbstractEnableWebMvcExtensionTest.java @@ -43,6 +43,7 @@ import java.util.Collection; import static io.microsphere.spring.beans.BeanUtils.isBeanPresent; +import static io.microsphere.util.ArrayUtils.isNotEmpty; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -89,7 +90,8 @@ public void setup() { this.registerWebEndpointMappings = enableWebMvcExtension.registerWebEndpointMappings(); this.interceptHandlerMethods = enableWebMvcExtension.interceptHandlerMethods(); this.publishEvents = enableWebMvcExtension.publishEvents(); - this.registerHandlerInterceptors = enableWebMvcExtension.registerHandlerInterceptors(); + this.registerHandlerInterceptors = enableWebMvcExtension.registerHandlerInterceptors() ? true : + isNotEmpty(enableWebMvcExtension.handlerInterceptors()); this.storeRequestBodyArgument = enableWebMvcExtension.storeRequestBodyArgument(); this.storeResponseBodyReturnValue = enableWebMvcExtension.storeResponseBodyReturnValue(); } diff --git a/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/EnableWebMvcExtensionInterceptorsTest.java b/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/EnableWebMvcExtensionInterceptorsTest.java new file mode 100644 index 000000000..c109f6cc3 --- /dev/null +++ b/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/annotation/EnableWebMvcExtensionInterceptorsTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.microsphere.spring.webmvc.annotation; + +import io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor; +import org.junit.Test; +import org.springframework.test.context.ContextConfiguration; + +import static io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor.MOCK_TOKEN_VALUE; +import static io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor.TOKEN_HEADER_NAME; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * {@link EnableWebMvcExtension} Test with interceptors + * + * @author Mercy + * @see EnableWebMvcExtension + * @since 1.0.0 + */ +@ContextConfiguration(classes = { + EnableWebMvcExtensionInterceptorsTest.class +}) +@EnableWebMvcExtension(handlerInterceptors = { + IdempotentAnnotatedMethodHandlerInterceptor.class +}) +public class EnableWebMvcExtensionInterceptorsTest extends AbstractEnableWebMvcExtensionTest { + + @Test + @Override + public void test() throws Exception { + this.mockMvc.perform(get("/echo/hello").header(TOKEN_HEADER_NAME, MOCK_TOKEN_VALUE)) + .andExpect(status().isOk()) + .andExpect(content().json("[ECHO] : hello")); + } +} diff --git a/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/interceptor/IdempotentAnnotatedMethodHandlerInterceptor.java b/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/interceptor/IdempotentAnnotatedMethodHandlerInterceptor.java index 61d3325e3..9761fa9e7 100644 --- a/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/interceptor/IdempotentAnnotatedMethodHandlerInterceptor.java +++ b/microsphere-spring-webmvc/src/test/java/io/microsphere/spring/webmvc/interceptor/IdempotentAnnotatedMethodHandlerInterceptor.java @@ -16,6 +16,7 @@ */ package io.microsphere.spring.webmvc.interceptor; +import io.microsphere.logging.Logger; import io.microsphere.spring.web.event.HandlerMethodArgumentsResolvedEvent; import io.microsphere.spring.webmvc.IdempotentException; import io.microsphere.spring.webmvc.annotation.Idempotent; @@ -29,6 +30,11 @@ import javax.servlet.http.HttpSession; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.Objects; +import java.util.UUID; + +import static io.microsphere.logging.LoggerFactory.getLogger; +import static java.util.Arrays.asList; /** * {@link AnnotatedMethodHandlerInterceptor} for {@link Idempotent} annotation @@ -36,15 +42,18 @@ * @author Mercy * @since 1.0.0 */ -public class IdempotentAnnotatedMethodHandlerInterceptor extends AnnotatedMethodHandlerInterceptor implements ApplicationListener { +public class IdempotentAnnotatedMethodHandlerInterceptor extends AnnotatedMethodHandlerInterceptor + implements ApplicationListener { + + private static final Logger logger = getLogger(IdempotentAnnotatedMethodHandlerInterceptor.class); + + public static final String TOKEN_HEADER_NAME = "_token_"; + + public static final String MOCK_TOKEN_VALUE = UUID.randomUUID().toString(); @Override protected boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Idempotent idempotent) throws Exception { - - System.out.println(handlerMethod); - System.out.println(idempotent); - return true; } @@ -54,19 +63,14 @@ protected boolean preHandle(HttpServletRequest request, HttpServletResponse resp public void onApplicationEvent(HandlerMethodArgumentsResolvedEvent event) { Method method = event.getMethod(); Object[] args = event.getArguments(); - System.out.println("method : " + method + " , args : " + Arrays.asList(args)); WebRequest webRequest = event.getWebRequest(); + logger.trace("The method : {} , args : {} , webRequest : {}", method, asList(args), webRequest); if (webRequest instanceof ServletWebRequest) { ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest; HttpServletRequest request = servletWebRequest.getNativeRequest(HttpServletRequest.class); - // HttpSession based on Spring Redis - // Spring Session - HttpSession httpSession = request.getSession(); - String token = request.getHeader("token"); - Object tokenValue = httpSession.getAttribute(token); - if (tokenValue != null) { - // - throw new IdempotentException(""); + String token = request.getHeader(TOKEN_HEADER_NAME); + if (!Objects.equals(MOCK_TOKEN_VALUE, token)) { + throw new IdempotentException("Illegal token"); } } }