spring ServletCookieValueMethodArgumentResolverTests 源码
spring ServletCookieValueMethodArgumentResolverTests 代码
文件路径:/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolverTests.java
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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 org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test fixture with {@link ServletCookieValueMethodArgumentResolver}.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class ServletCookieValueMethodArgumentResolverTests {
private ServletCookieValueMethodArgumentResolver resolver;
private MockHttpServletRequest request;
private ServletWebRequest webRequest;
private MethodParameter cookieParameter;
private MethodParameter cookieStringParameter;
@BeforeEach
public void setup() throws Exception {
resolver = new ServletCookieValueMethodArgumentResolver(null);
request = new MockHttpServletRequest();
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
Method method = getClass().getMethod("params", Cookie.class, String.class);
cookieParameter = new SynthesizingMethodParameter(method, 0);
cookieStringParameter = new SynthesizingMethodParameter(method, 1);
}
@Test
public void resolveCookieArgument() throws Exception {
Cookie expected = new Cookie("name", "foo");
request.setCookies(expected);
Cookie result = (Cookie) resolver.resolveArgument(cookieParameter, null, webRequest, null);
assertThat(result).as("Invalid result").isEqualTo(expected);
}
@Test
public void resolveCookieStringArgument() throws Exception {
Cookie cookie = new Cookie("name", "foo");
request.setCookies(cookie);
String result = (String) resolver.resolveArgument(cookieStringParameter, null, webRequest, null);
assertThat(result).as("Invalid result").isEqualTo(cookie.getValue());
}
public void params(@CookieValue("name") Cookie cookie,
@CookieValue(name = "name", defaultValue = "bar") String cookieString) {
}
}
相关信息
相关文章
spring AbstractRequestAttributesArgumentResolverTests 源码
spring AbstractServletHandlerMethodTests 源码
spring DeferredResultReturnValueHandlerTests 源码
spring ExceptionHandlerExceptionResolverTests 源码
spring ExtendedServletRequestDataBinderTests 源码
spring HandlerMethodAnnotationDetectionTests 源码
spring HttpEntityMethodProcessorMockTests 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
7、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦