我使用MockRestServiceServer来模拟外部的webservice xml响应。这已经很好了,但是我怎样才能在响应中(而不仅仅是响应体)模拟http头呢?
@MockBean
private RestTemplate restTemplate;
private MockRestServiceServer mockServer;
@Before
public void createServer() throws Exception {
mockServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void test() {
String xml = loadFromFile("productsResponse.xml");
mockServer.expect(MockRestRequestMatchers.anything()).andRespond(MockRestResponseCreators.withSuccess(xml, MediaType.APPLICATION_XML));
}发布于 2017-12-08 14:45:57
只需使用withSuccess方法执行headers方法即可。
mockServer
.expect(...)
.andRespond(withSuccess().headers(...));发布于 2019-10-21 10:16:19
@Gorazd的回答是正确的。给它添加更多的肉:
HttpHeaders headers = new HttpHeaders();
headers.setLocation(new URI(billingConfiguration.getBillingURL()+"/events/123"));
mockServer.expect(ExpectedCount.once(),
requestTo(new URI(billingConfiguration.getBillingURL())))
.andExpect(method(HttpMethod.POST))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON).headers(headers));发布于 2019-11-21 09:46:02
以下代码适用于我:
HttpHeaders mockResponseHeaders = new HttpHeaders();
mockResponseHeaders.set("Authorization", mockAuthToken);
mockServer
.expect(once(), requestTo(testhUrl))
.andExpect(method(HttpMethod.POST))
.andRespond(withSuccess().headers(mockResponseHeaders));https://stackoverflow.com/questions/47716583
复制相似问题