MockHttpServletRequestBuilder - 如何更改 HttpServletRequest 的 remoteHost 的 remoteAddress?
MockHttpServletRequestBuilder - how to change remoteAddress of remoteHost of HttpServletRequest?
我正在尝试为集成测试创建模拟请求 (@SpringBootTest)。
//given
MockHttpServletRequestBuilder requestBuilder = get("/users/register/user1");
我要查看的是这个请求的remote。在我的控制器中,我从 HttpServletRequest
获取此信息
HttpServletRequest request;
request.getRemoteHost();
request.getRemoteAddr();
不幸的是现在 getRemoteHost()
将永远 return localhost.
我想在我的模拟请求中将其更改为其他内容,例如:
remoteHost: localhost
--> mockhostdomain
remoteAddr: 127.0.0.1
--> 10.32.120.7
(anything different)
我找不到合适的方法。 有可能吗?
我终于在这里找到了解决方案:
https://techotom.wordpress.com/2014/11/12/mocking-remoteaddr-with-spring-mvc/
基本上用这个方法我们可以改变请求的每一个参数。
所以首先我们必须定义我们的方法来改变我们想要在请求中的内容:
private static RequestPostProcessor remoteHost(final String remoteHost){
return request -> {
request.setRemoteAddr(remoteHost);
return request;
};
与在 MockHttpServletRequestBuilder
对象上使用方法 with(...)
相比,我们必须注入此方法结果。
MockHttpServletRequestBuilder requestBuilder = get("/user/prop").
.with(remoteHost("mockhostdomain.com"));
使用 mockmvc 你可以这样做:
Map<String, String> req = new HashMap<>();
// req.put("")
ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
java.lang.String requestJson = ow.writeValueAsString(req);
String responseString = this.mockMvc.perform(post
("/authorization/activated")
.with(request->{request.setRemoteAddr("192.168.0.2");return request;})
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson)
.header("Authorization", bear)
.header("X-Device-Id","7fb0c4e49aec4c5a9a089d0c84f7078b"))
.andReturn().getResponse().getContentAsString();
System.out.println("[POST result]:" + responseString);
我正在尝试为集成测试创建模拟请求 (@SpringBootTest)。
//given
MockHttpServletRequestBuilder requestBuilder = get("/users/register/user1");
我要查看的是这个请求的remote。在我的控制器中,我从 HttpServletRequest
获取此信息HttpServletRequest request;
request.getRemoteHost();
request.getRemoteAddr();
不幸的是现在 getRemoteHost()
将永远 return localhost.
我想在我的模拟请求中将其更改为其他内容,例如:
remoteHost:
localhost
-->mockhostdomain
remoteAddr:
127.0.0.1
-->10.32.120.7
(anything different)
我找不到合适的方法。 有可能吗?
我终于在这里找到了解决方案:
https://techotom.wordpress.com/2014/11/12/mocking-remoteaddr-with-spring-mvc/
基本上用这个方法我们可以改变请求的每一个参数。
所以首先我们必须定义我们的方法来改变我们想要在请求中的内容:
private static RequestPostProcessor remoteHost(final String remoteHost){
return request -> {
request.setRemoteAddr(remoteHost);
return request;
};
与在 MockHttpServletRequestBuilder
对象上使用方法 with(...)
相比,我们必须注入此方法结果。
MockHttpServletRequestBuilder requestBuilder = get("/user/prop").
.with(remoteHost("mockhostdomain.com"));
使用 mockmvc 你可以这样做:
Map<String, String> req = new HashMap<>();
// req.put("")
ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
java.lang.String requestJson = ow.writeValueAsString(req);
String responseString = this.mockMvc.perform(post
("/authorization/activated")
.with(request->{request.setRemoteAddr("192.168.0.2");return request;})
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson)
.header("Authorization", bear)
.header("X-Device-Id","7fb0c4e49aec4c5a9a089d0c84f7078b"))
.andReturn().getResponse().getContentAsString();
System.out.println("[POST result]:" + responseString);