gpt4 book ai didi

java - 从预定义数据构建 HttpServletRequest

转载 作者:可可西里 更新时间:2023-11-01 17:10:07 28 4
gpt4 key购买 nike

为了测试,我想从一些预定义的数据构建 HttpServletRequest 对象,例如:

GET / HTTP/1.1
User-Agent: Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.289 Version/
12.01
Host: www.foo.com
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png,
image/webp, image/jpeg, image/gif, image/x-xbitmap;q=0.1
Accept-Language: ru-RU,ru;q=0.9,en;q=0.8
Accept-Encoding: gzip, deflate
Pragma: no-cache
Cache-Control: no-cache
Connection: Keep-Alive

同时设置 url 和客户端地址。有什么简单的方法吗?

最佳答案

基里尔!我认为你的情况最好的方法是使用 Mockito框架。例如,您可以模拟 HttpServletRequest 接口(interface)。并使用:

   HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
Mockito.when(httpRequest.getHeader("Host")).thenReturn("http://www.foo.com");
Mockito.when(httpRequest.getHeader("Referer")).thenReturn("blalba");
Mockito.when(httpRequest.getHeader("User-Agent")).thenReturn("Opera");
Mockito.when(httpRequest.getRemoteAddr()).thenReturn("127.0.0.1");

关于java - 从预定义数据构建 HttpServletRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22848510/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com